Varnish 实现Web站点加速

2022-12-28 14:23:41 浏览数 (1)

Varnish 是一款高性能的开源HTTP加速器。

编译安装Varnish

1.安装依赖包

代码语言:javascript复制
#yum install -y libtool ncurses-devel pcre-devel libxslt libedit python-imaging python-docutils

yum install -y pcre-devel python-docutils libedit-dev* 

2.编译安装Varnish

代码语言:javascript复制
wget http://varnish-cache.org/_downloads/varnish-6.0.0.tgz

tar -xzvf varnish-6.0.0.tgz

cd varnish-6.0.0/

./configure --prefix=/usr/local/varnish6

make && make install

ln -s /usr/local/varnish6/sbin/* /usr/sbin/
ln -s /usr/local/varnish6/bin/* /usr/local/bin/

cp -a /usr/local/varnish6/share/doc/varnish/example.vcl /usr/local/varnish6/default.vcl

Varnish实现负载均衡并实现页面缓存

1.编辑Varnish主配置文件,在相应的区域追加写入以下标★语句

代码语言:javascript复制
vim /usr/local/varnish/default.vcl

15 # Default backend definition. Set this to point to your content server.
16 backend default {
17     .host = "127.0.0.1";
18     .port = "8080";
19 }
20 
★ backend web1 {                                          #均衡web主机1
★         .host="192.168.1.13";
★         .port="80";                                     #指定端口
★ .probe = {                                              #开启健康检查
★         .url = "/";                                     #请求的URL路径
★         .interval = 5s;                                 #查询间隔时间
★         .timeout = 1s;                                  #超时时间
★         .window = 5;                                    #滑动窗
★         .threshold = 3;                                 #上次检查.window数量的多少
★         }
★ }
★ backend web2 {
★         .host="192.168.1.14";                           #均衡web主机2
★         .port="80";                                     #指定端口
★ .probe = {                                              #开启健康检查
★         .url = "/";                                     #请求的URL路径
★         .interval = 5s;                                 #查询间隔时间
★         .timeout = 1s;                                  #超时时间
★         .window = 5;                                    #滑动窗
★         .threshold = 3;                                 #上次检查.window数量的多少,
★         }
★ }
★ 
★ import directors;                                       #加载directors模块
★ 
★ sub vcl_init {                                          #缓存及加速-03单-高性能缓存服务器
★ 
★ new bar = directors.round_robin();
★ bar.add_backend(web1);
★ bar.add_backend(web2);
★ 
★ }
★ 
★ sub vcl_recv {
★ 
★ set req.backend_hint = bar.backend();			#指定backend
★ 
★ }

2.检查配置文件,并启动Varnish

代码语言:javascript复制
varnishd -C -f /usr/local/varnish6/default.vcl		#检查语法

varnishd -f /usr/local/varnish6/default.vcl		#启动

pkill varnishd						#关闭Varnish

varnishlog						#查看Varnish日志

netstat -anpt | grep varnishd				#检查是否启动

3.验证环节

代码语言:javascript复制
elinks http://127.0.0.1/				#varnish服务器会根据算法分配流量

0 人点赞