一,什么是varnish@H_403_3@
Varnish是一款高性能的开源HTTP加速器,挪威最大的在线报纸 Verdens Gang 使用3台Varnish代替了原来的12台Squid,性能比以前更好。
Varnish 的作者Poul-Henning Kamp是FreeBSD的内核开发者之一,他认为现在的计算机比起1975年已经复杂许多。在1975年时,储存媒介只有两种:内存与硬盘。但现在计算机系统的内存除了主存外,还包括了cpu内的L1、L2,甚至有L3快取。硬盘上也有自己的快取装置,因此Squid Cache自行处理物件替换的架构不可能得知这些情况而做到最佳化,但操作系统可以得知这些情况,所以这部份的工作应该交给操作系统处理,这就是 Varnish cache设计架构。
Varnish的理念是好的,但是Varnish还没有稳定到可以完全替代Squid的程度,现在就抛弃Squid选择Varnish是不明智的。@H_403_3@
@H_403_3@
二,安装varnish@H_403_3@
http://sourceforge.net/projects/varnish/files/
tar zxvf varnish-2.1.tar.gz
cd varnish-2.1
./configure --prefix=/usr/local/varnish
make && make install@H_403_3@
vi /usr/local/varnish/vcl.conf@H_403_3@
backend testserver { set backend.host = "127.0.0.1"; set backend.port = "10000"; } backend imageserver { set backend.host = "127.0.0.1"; set backend.port = "10001"; } acl purge { "localhost"; "127.0.0.1"; "192.168.1.0"/24; } sub vcl_recv { if (req.request == "PURGE") { if (!client.ip ~ purge) { error 405 "Not allowed."; } lookup; } if (req.http.host ~ "^localhost") { set req.backend = testserver; } elseif (req.http.host ~ "^127.0.0.1") { set req.backend = imageserver; } else{ error 404 "test Cache Server"; } if (req.request != "GET" && req.request != "HEAD") { pipe; } if (req.http.Expect) { pipe; } if (req.http.Authenticate || req.http.Cookie || req.url ~ "\.(PHP|cgi)($|\?)") { pass; } if (req.request == "GET" && req.url ~ "\.(txt|js|jpg|gif|png|bmp|css)$") { lookup; } lookup; } sub vcl_hit { if (req.request == "PURGE") { set obj.ttl = 0s; error 200 "Purged."; } } sub vcl_miss { if (req.request == "PURGE") { error 404 "Not in cache."; } } sub vcl_fetch { if (obj.http.Pragma ~ "no-cache" || obj.http.Cache-Control ~ "no-cache" || obj.http.Cache-Control ~ "private") { pass; } if (req.request == "GET" && req.url ~ "\.(txt|js|jpg|gif|png|bmp|css)$") { set obj.ttl = 3600s; } else { set obj.ttl = 30d; } } sub vcl_hash { set req.hash += req.url; if (req.http.host) { set req.hash += req.http.host; }else{ set req.hash += server.ip; } hash; } sub vcl_pipe { pipe; } sub vcl_pass { pass; }
简单说明一下:@H_403_3@
1,设置反向请求的IP,127.0.0.1:10000和127.0.0.1:10001@H_403_3@
2,允许缓存管理,localhost,127.0.0.1,192.168.0.*@H_403_3@
3,varnish对匹配localhost请求跳到testserver,对匹配127.0.0.1的请求跳 到imageserver,如果都没有,显示test Cache Server@H_403_3@
4,Varnish对HTTP协议中的GET、HEAD请求进行缓存,对POST请求透过,让其直接访问后端Web服务器。放过PHP,cgi不对其进行缓存,只缓存,静态文件@H_403_3@
5,对以.txt.js.jpg.gif.png.bmp.css结尾的URL缓存时间设置1小时,对其他的URL缓存时间设置为30天。@H_403_3@
四,优化Linux内核参数@H_403_3@
vi /etc/sysctl.conf@H_403_3@
加入@H_403_3@
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 5000 65000@H_403_3@
ldconfig进行重新加载@H_403_3@
五,启动varnish和varnishncsa,并查看@H_403_3@
ulimit -SHn 51200
/usr/local/varnish/sbin/varnishd -n /var/vcache -f /usr/local/varnish/vcl.conf -a :8080 -s file,/var/vcache/varnish_cache.data,1G -g users -u zhangy -w 30000,51200,10 -T 127.0.0.1:3500 -p client_http11=on
/usr/local/varnish/bin/varnishncsa -n /var/vcache -w /var/vlogs/varnish.log &@H_403_3@
解释一下8080是varnish监听的端口,例如127.0.0.1:8080会反向请求127.0.0.1:10001图片服务器,127.0.0.1:3500这个是管理缓存的ip@H_403_3@
[zhangy@BlackGhost ~]$ ps -e|grep varnish
3970 ? 00:00:00 varnishd
3971 ? 00:00:00 varnishd
3977 pts/0 00:00:00 varnishncsa@H_403_3@
六、通过Varnish管理端口,使用正则表达式批量清除缓存:@H_403_3@
1,例:清除类似http://127.0.0.1:8080/00/01的URL地址:@H_403_3@
[root@BlackGhost varnish]# /usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 url.purge /00/@H_403_3@
2,例:清除类似http://127.0.0.1:8080/00/0144的URL地址:@H_403_3@
[root@BlackGhost varnish]# /usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 url.purge \d*$@H_403_3@
3,例:清除类似http://127.0.0.1:8080/00/01/RwGowEtWvcQAAAAAAAAWHH0Rklg81.gif的URL地址@H_403_3@
[root@BlackGhost varnish]# /usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 url.purge gif$@H_403_3@
4,清除所有@H_403_3@
[root@BlackGhost varnish]# /usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 url.purge *$@H_403_3@
5,怎么测试你知道有没有清除缓存@H_403_3@
在地址栏输入http://127.0.0.1:8080/00/01/RwGowEtWvcQAAAAAAAAWHH0Rklg81.gif
然后涮新几次你发现以下内容@H_403_3@
清除缓存
[root@BlackGhost varnish]# /usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 url.purge gif$@H_403_3@
然后在请求http://127.0.0.1:8080/00/01/RwGowEtWvcQAAAAAAAAWHH0Rklg81.gif@H_403_3@
说明:请求数变了,缓存数没变,后台请求数变了,说明刚才请缓存是有效果的。@H_403_3@
七,进行缓存测试,以及部分参数说明@H_403_3@
上图中199688表示这个缓存命中次数,就是缓了多少次@H_403_3@
上图中第一个2表示没有缓存数,第一次访问都不会被缓存的,memcache也是@H_403_3@
上图中第二个2表示后台处理数,没缓存当然要交到后台处理@H_403_3@ 原文链接:https://www.f2er.com/redis/447333.html