自从动态生成缩略图上线后,NFS客户端日志时常报无法响应的错误,怀疑是NFS机IO过大,所以想到使用Varnish给NFS机减压,将缩略图存放到内存中,减少读的压力,然后就有了篇博文。 简介: Varnish是一款高性能的开源HTTP加速器,网上评价比Squid更强,安装配置都很快速,方便。
一、安装:
# wget http://repo.varnish-cache.org/source/varnish-2.1.5.tar.gz
# tar zxvf varnish-2.1.5.tar.gz cd varnish-2.1.5
# ./configure –prefix=/usr/local/varnish
# make && make install
二、配置文件:
# cp /usr/local/varnish/etc/varnish/default.vcl /usr/local/varnish/etc/varnish/default.vcl.bak
# vi /usr/local/varnish/etc/varnish/default.vcl
# 反向代理请求的后端Web服务器ip和端口
backend default {
.host = “192.168.1.1”;
.port = “80”;
}sub vcl_recv {
if (req.request != “GET” && req.request != “HEAD”) {
return(pipe);
} elseif(req.url ~ “\.(jpg|gif|png)$”) {
return (lookup);
} else {
return(pass);
}
}sub vcl_pipe {
return (pipe);
}sub vcl_pass {
return (pass);
}sub vcl_hash {
set req.hash += req.url;
if (req.http.host) {
set req.hash += req.http.host;
} else {
set req.hash += server.ip;
}
return (hash);
}sub vcl_hit {
if (!obj.cacheable) {
return (pass);
}
return (deliver);
}sub vcl_miss {
return (fetch);
}sub vcl_fetch {
if (beresp.http.Pragma ~ “no-cache” ||
beresp.http.Cache-Control ~ “no-cache” ||
beresp.http.Cache-Control ~ “private”) {
return (pass);
}
# 清除cookie,设置过期时间为30天
if (req.request == “GET” && req.url ~ “\.(jpg|gif|png)$”) {
unset req.http.cookie;
set beresp.ttl = 30d;
}
return (deliver);
}sub vcl_deliver {
# 增加标识(可使用firebug查看)x-hits,表示是否命中
set resp.http.x-hits = obj.hits ;
if (obj.hits > 0) {
set resp.http.X-Cache = “h”;
} else {
set resp.http.X-Cache = “m”;
}
}
三、启动:
# /usr/local/varnish/sbin/varnishd -f /usr/local/varnish/etc/varnish/default.vcl -n /var/log/varnish -s malloc,15G -w 300,51200,20 -T 127.0.0.1:2000 -a 0.0.0.0:8080
说明:
-f 配置文件路径
-n 主目录
-s 分配 15G 内存
-w 线程最小与最大数
-T 管理IP和端口,可以使用telnet 127.0.0.1 2000 进行管理varnish
-a 监听 8080 端口
四、写入日志文件:
# /usr/local/varnish/bin/varnishncsa -n /var/log/varnish -w /var/log/varnish/varnish.log
五、查看狀態:
# /usr/local/varnish/bin/varnishstat -n /var/log/varnish
六、關閉Varnish:
# pkill varnishd
七、清除緩存:
1、清除指定域名:
/usr/local/varnish/bin/varnishadm -T 127.0.0.1:2000 purge “req.http.host ~ statics.xxx.com.tw”
2、清除image目录下所有缓存:
/usr/local/varnish/bin/varnishadm -T 127.0.0.1:2000 url.purge /image/
3、清除所有缓存:
/usr/local/varnish/bin/varnishadm -T 127.0.0.1:2000 url.purge *$
八、测试:
ab测试:
ab -c 100 -n 1000 http://x.x.x.x/129974432673506402_128x92.jpg
未使用Varnish前:
Server Software: nginx
Server Hostname: x.x.x.x
Server Port: 8000Document Path: 129974432673506402_128x92.jpg
Document Length: 2443 bytesConcurrency Level: 100
Time taken for tests: 0.667390 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 2730000 bytes
HTML transferred: 2443000 bytes
Requests per second: 1498.37 [#/sec] (mean)
Time per request: 66.739 [ms] (mean)
Time per request: 0.667 [ms] (mean, across all concurrent requests)
Transfer rate: 3994.67 [Kbytes/sec] receivedConnection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.6 0 4
Processing: 2 61 23.2 57 248
Waiting: 1 60 23.2 57 247
Total: 2 61 23.1 58 248Percentage of the requests served within a certain time (ms)
50% 58
66% 65
75% 73
80% 77
90% 89
95% 100
98% 116
99% 139
100% 248 (longest request)
使用Varnish后:
Server Software: nginx
Server Hostname: p2.591.com.tw
Server Port: 8000Document Path: 129974432673506402_128x92.jpg
Document Length: 2443 bytesConcurrency Level: 100
Time taken for tests: 0.271769 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 2795675 bytes
HTML transferred: 2445443 bytes
Requests per second: 3679.60 [#/sec] (mean)
Time per request: 27.177 [ms] (mean)
Time per request: 0.272 [ms] (mean, across all concurrent requests)
Transfer rate: 10045.30 [Kbytes/sec] receivedConnection Times (ms)
min mean[+/-sd] median max
Connect: 0 6 2.3 6 12
Processing: 1 7 2.3 7 17
Waiting: 1 6 2.1 7 15
Total: 3 13 1.7 14 23Percentage of the requests served within a certain time (ms)
50% 14
66% 14
75% 14
80% 14
90% 15
95% 15
98% 16
99% 17
100% 23 (longest request)
总结:
Varnish上线后,观察一段时间后,发现NFS报错的原因,尚未改善,现在把生成图片部分,换到NFS上,NFS报错出现的比较少,下一步,看换成千兆Switch后,再观察看看。
參考:
Varnish Cache + Riak
(http://blog.dloh.org/2011/01/varnish-cache-riak.html )
varnish cache 反向代理服务器和http加速器的安装和配置
(http://blog.51yip.com/cache/618.html)
Varnish手冊
(http://www.varnish-cache.org/docs/2.1/tutorial/vcl.html)
varnish安装过程详解
(http://thinkgroupon.com/redirect.php?tid=3872&goto=lastpost)
使用Varnish代替Squid做网站缓存加速器的详细解决方案[原创]
(http://blog.s135.com/post/313/)
Varnish使用小结
(http://iyubo.blogbus.com/logs/35085709.html)
原文链接:https://www.cnblogs.com/littlehb/archive/2012/02/20/2359431.html
原创文章,作者:优速盾-小U,如若转载,请注明出处:https://www.cdnb.net/bbs/archives/17583