varnish加速网站
1、安装varnish, 安装epel源
yum install epel-release -y
yum install varnish
14.3.1 vcl置文件:
ls /etc/varnish/default.vcl
varnish主配置文件
vim /etc/varnish/varnish.params
启varnish服务:
systemctl rstart varnish
14.4.1 实战:配置 varnish服务器 本机ip为63
vi /etc/varnish/default.vcl 改: 7 backend default { 8 .host = "127.0.0.1"; 9 .port = "80"; 10 } 为: backend web1 { .host = "192.168.1.64"; .port = "80"; }
###查看缓存命中情况
在:
90 # sub vcl_deliver {
91 # return (deliver);
92 # }
追加:
sub vcl_deliver { if (obj.hits>0) { set resp.http.X-Cache = "HIT from xuegod cache"; } else { set resp.http.X-Cache = "MISS from xuegod cache"; } return (deliver); } 或 sub vcl_deliver { #定义缓存响应头部 if (obj.hits>0) { set resp.http.X-Cache = "HIT via " + server.ip; } else { set resp.http.X-Cache = "MISS from " + server.ip; } }
注意:obj.hits>0 大于号前后无空格
14.4.2 配置varnish服务端口
vi /etc/varnish/varnish.params 改: 66 VARNISH_LISTEN_PORT=6081 为: VARNISH_LISTEN_PORT=80
14.4.3 启动varnish服务器
systemctl restart varnish
命中测试
[root@web63 ~]# curl -I 192.168.1.63
HTTP/1.1 200 OK
Date: Wed, 21 Nov 2018 13:36:26 GMT
Server: Apache/2.4.6 (CentOS) PHP/5.4.16
X-Powered-By: PHP/5.4.16
Content-Type: text/html; charset=UTF-8
X-Varnish: 2
Age: 0
Via: 1.1 varnish-v4
X-Cache: MISS from 192.168.1.63 #没有命中
Connection: keep-alive
[root@web63 ~]# curl -I 192.168.1.63
HTTP/1.1 200 OK
Date: Wed, 21 Nov 2018 13:36:26 GMT
Server: Apache/2.4.6 (CentOS) PHP/5.4.16
X-Powered-By: PHP/5.4.16
Content-Type: text/html; charset=UTF-8
X-Varnish: 32770 3
Age: 14
Via: 1.1 varnish-v4
X-Cache: HIT via 192.168.1.63 #命中
Content-Length: 48050
Connection: keep-alive
14.5.2 配置xuegod62为web2服务器
yum install httpd -y echo 192.168.1.62 > /var/www/html/index.html hostnamectl set-hostname xuegod62.cn [root@xuegod62 ~]# vim /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.1.62 xuegod62.cn systemctl start httpd 64web上也写入测试页面
echo 192.168.1.64 > /var/www/html/index.html
配置xuegod63 上的varnish服务器:
vim /etc/varnish/default.vcl 改: backend web1 { .host = "192.168.1.64"; .port = "80"; } 为: backend web1 { .host = "192.168.1.64"; .port = "80"; } backend web2 { .host = "192.168.1.62"; .port = "80"; } [root@xuegod63 ~]# vim /etc/varnish/default.vcl #在sub vcl_deliver 处定义以下内容: sub vcl_recv { if (req.http.host ~ "(?i)^(www.)?xuegod.cn$") { set req.http.host = "www.xuegod.cn"; set req.backend_hint = web1; } elsif (req.http.host ~ "(?i)^bbs.xuegod.cn$") { set req.backend_hint = web2; return(hash); } }
systemctl reload varnish
在xuegod62上测试:
[root@xuegod62 ~]# vim /etc/hosts #添加hosts文件
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.1.62 xuegod62.cn 192.168.1.63 www.xuegod.cn 192.168.1.63 bbs.xuegod.cn [root@xuegod62 ~]# yum install elinks -y [root@xuegod62 ~]# elinks www.xuegod.cn --dump 192.168.1.64 [root@xuegod62 ~]# elinks bbs.xuegod.cn --dump 192.168.1.62
varnish 参数说明
/usr/local/sbin/varnishd -f /usr/local/etc/varnish/default.vcl -a :6081 -P /var/run/varnish.pid -s malloc,256m
执行完毕后,Varnish将进入后台运行,同时返回命令行状态。需要注意的是,Varnish运行时会同时启动两个进程,一个主进程,一个是子进程,如果子进程出现问题,主进程将重新生成一个子进程。
Varnishd 的启动选项
-f : 指定配置文件位置
-a : varnish监听的本地地址和端口。
-P :PID文件位置,用来关闭Varnish
-s :cache配置。默认使用256M内存

浙公网安备 33010602011771号