首页 > 代码库 > Varnish详解
Varnish详解
Varnish cache,或称Varnish,是一套高效能的反向代理,缓存器(reverseproxy server)。
varnish主要运行两个进程:Management进程和Child进程(也叫Cache进程)。
安装varnish,管网下载rpm包,varnish-3.0.5-1.el6.x86_64.rpm varnish-docs-3.0.5-1.el6.x86_64.rpm varnish-libs-3.0.5-1.el6.x86_64.rpm这三个包都下载下来安装
# yum -y install *.rpm
安装完成后,可以查看下各安装包所生成的文件
# rpm -ql varnish
查看帮助文档用
# man varnishd
varnish的配置文件
#vim /etc/varnish/default.vcl
Varnish的启动脚本位于/etc/rc.d/init.d/varnish,里面的参数是由/etc/sysconfig/varnish来定义的。
# service varnish start 启动 varnish
# ss -tnlp 查看80,6082端口是否已被监听
VCL状态引擎,用于让管理员定义缓存策略,vcl中有很多内置函数可用来实现字符串的修改
编辑配置文件,要想是修改的内容永久有效,修改default.vcl配置文件,但我们这里为了演示手动修改并启用配置文件,重新cp了配置文件,并加载新定义的配置文件
# cp /etc/varnish/default.vcl/etc/varnish/text.vcl # vim /etc/varnish/text.vcl
用varnishadm接口手动是配置文件生效
# varnishadm -h 查看varnishadm的用法 # varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082
用help可查看所有命令
help[command] ping[timestamp] authresponse quit banner status start stop vcl.load <configname><filename> vcl.inline<configname> <quoted_VCLstring> vcl.use <configname> vcl.discard<configname> vcl.list vcl.show<configname> param.show[-l] [<param>] param.set<param> <value> panic.show panic.clear storage.list backend.list backend.set_healthmatcher state ban.url<regexp> ban<field> <operator> <arg> [&& <field><oper> <arg>]... ban.list
varnish> vcl.list 查看列表 200 active 0 boot available 0 text varnish> vcl.load text1 text.vcl 编译配置文件 200 VCLcompiled. varnish> vcl.use text1 200
下面以例子来演示vcl的各种语法和各内置函数的使用方法
# vim /etc/varnish/text.vcl 加入如下行
subvcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT"; 如果是首次访问该页面,此时无缓存,显示MISS,以后再访问时会被缓存命中,显示HIT
} else {
set resp.http.X-Cache ="MISS";
}
}
保存,退出
在varnishadm命令接口重新载入编译配置文件
varnish> vcl.load text2 text.vcl
varnish> vcl.use text2
在浏览器输入varnish所在主机的IP,这里是172.16.17.3,打开主页面后,打开开发者工具查看
首次访问MISS
Server:Apache/2.2.15 (CentOS) Via:1.1 varnish X-Cache:MISS X-Varnish:2013467702
再次强制刷新HIT
Server:Apache/2.2.15 (CentOS) Via:1.1 varnish X-Cache:HIT X-Varnish:2013467704 2013467703
sub vcl_recv { 指定某个网页不缓存
if (req.url ~"^/test.html$") {
return(pass);
}
}
sub vcl_fetch { 定义缓存时长
if (req.request == "GET"&& req.url ~ "\.(html|jpg|jpeg)$") {
set beresp.ttl = 3600s;
}
}
对后端主机的健康状态检测
# vim /etc/varnish/text.vcl
在backend default { 中加入
.probe= { .url = "/index.html"; .interval = 3s; 探测请求的发送周期,默认为5秒 .window = 5; 探测次数,默认是8 .threshold = 2; 至少探测判断几次认为其状态健康,默认是3 } varnish> backend.list 查看后端主机的信息健康状态 200 Backendname Refs Admin Probe default(172.16.17.12,,80) 12 probe Healthy 5/5
Varnish中可以使用director指令将一个或多个近似的后端主机定义为一个逻辑组
# vim/etc/varnish/text.vcl backendweb1 { .host = "172.16.17.12"; .port = "80"; .probe = { .url = "/index.html"; .interval = 2s; .window = 6; .threshold = 2; } } backendweb2 { .host = "172.16.17.11"; .port = "80"; .probe = { .url = "/index.html"; .interval = 2s; .window = 6; .threshold = 2; } } directorwebservers round-robin { { .backend = web1; } { .backend = web2; } } acl purgers { "127.0.0.1"; "172.16.0.0"/16; } subvcl_recv { set req.backend = webservers; if (req.restarts == 0) { if (req.http.x-forwarded-for) { set req.http.X-Forwarded-For = req.http.X-Forwarded-For +", " + client.ip; } else { set req.http.X-Forwarded-For =client.ip; } } if (req.url ~ "^/test.html$") { return(pass); } if (req.request =="PURGE") { if (client.ip !~ purgers) { error 405 "Methodnot allowed"; } return (lookup); } return (pass); } subvcl_hit { if (req.request == "PURGE"){ purge; error 200 "Purged"; } return (deliver); } sub vcl_miss { if (req.request == "PURGEE"){ purge; error 404 "Not incache"; } return (fetch); } subvcl_deliver { if (obj.hits > 0) { setresp.http.X-Cache = "HIT via" + " " + server.hostname; } else { setresp.http.X-Cache = "MISS via" + " " + server.hostname; } return (deliver); }
Varnish详解