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); }