2023-05-12 开启多语言插件支持……

用Varnish搭建Cache服务器

Linux 苏 demo 2707℃ 0评论

版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
http://www.blogbus.com/iyubo-logs/30103691.html

前段时间图片服务器的连接数已经接近4W了(Windows服务器),为了减轻图片服务器的压力,尝试着用Varnish安装了一台Cache服务器用于图片的Cache,上线以后使用稳定,表现良好。先写下安装配置文档,还在精进中……欢迎大家互相交流

安装环境
操作系统: CentOS release 5.2 (Final)
Kernel: 2.6.18-92.1.6.el5PAE

软件列表
varnish-1.1.2.tar.gz

软件存放位置
/data/software

安装过程
#/usr/sbin/groupadd www -g 48
#/usr/sbin/useradd -u 48 -g www www
#mkdir -p /var/vcache
#chmod +w /var/vcache
#chown -R www:www /var/vcache
#mkdir -p /var/log/varnish
#chmod +w /var/log/varnish
#chown -R www:www /var/log/varnish
#cd /data/software
#tar zxvf varnish-1.1.2.tar.gz
#cd varnish-1.1.2
#./configure –prefix=/usr/local/varnish
#make && make install

编辑Varnish配置文件
#vi /usr/local/varnish/vcl.conf

backend webserver {
set backend.host = “10.10.10.8”;
set backend.port = “80”;
}

acl purge {
“localhost”;
“127.0.0.1”;
“10.10.10.0”/24;
}

sub vcl_recv {
remove req.http.X-Forwarded-For;
set    req.http.X-Forwarded-For = client.ip;
if (req.request == “PURGE”) {
if (!client.ip ~ purge) {
error 405 “Not allowed.”;
}
lookup;
}

if (req.http.host ~ “(a|b|c).test.com”) {
set req.backend = webserver;
if (req.url ~ “\.(png|gif|jpg|swf|css|js)$”) {
lookup;
}
else {
pass;
}
}

else {
error 404 “Test Cache Server”;
pipe;
}
}

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 {
set req.http.connection = “close”;
#pipe;
}

sub vcl_hit {
if (!obj.cacheable) {
pass;
}
if (req.request == “PURGE”) {
set obj.ttl = 0s;
error 200 “Purged.”;
}
deliver;
}

sub vcl_miss {
if (req.request == “PURGE”) {
error 404 “Not in cache.”;
}
}

sub vcl_fetch {
set obj.ttl = 180s;
#set    obj.http.X-Varnish-IP = server.ip;
set    obj.http.Varnish = “Tested by Kevin”;
}

启动Varnish
#/usr/local/varnish/sbin/varnishd -n /var/vcache -f /usr/local/varnish/vcl.conf -a 0.0.0.0:80 -s file,/var/vcache/varnish_cache.data,4G -u apache -w 30000,51200,10 -T 127.0.0.1:3500 -p client_http11=on -p thread_pools=4

启动日志记录
#/usr/local/varnish/bin/varnishncsa -n /var/vcache -w /var/log/varnish/varnish.log &

补充几条相关命令
查看Varnish状态
/usr/local/varnish/bin/varnishstat -n /var/vcache/

查看访问最多的Referer
/usr/local/varnish/bin/varnishtop -n /var/vcache/ -i rxheader -I Referer

查看访问最多的URL
/usr/local/varnish/bin/varnishtop -n /var/vcache/ -i rxurl

参考文档
使用Varnish代替Squid做网站缓存加速器的详细解决方案[原创]

遇到的问题(欢迎讨论)
记录日志时总是到达2G大小以后就无法记录了
vcl.conf的使用还需要精进

打赏

转载请注明:苏demo的别样人生 » 用Varnish搭建Cache服务器

   如果本篇文章对您有帮助,欢迎向博主进行赞助,赞助时请写上您的用户名。
支付宝直接捐助帐号oracle_lee@qq.com 感谢支持!
喜欢 (0)or分享 (0)