利用Varnish构建Cache服务器笔记

公司最近需要用varnish和memcache做构架调整,现就自己做了一些前提准备,希望对大家有帮助!

Varnish是一个非常不错的HTTP accelerator,具体我也不做多方面介绍了大家可以亲自尝试一下,just do it!"停止一切空谈!"。

以下是我配置服务的过程贴出来和朋友们一起分享。在调试过程中感谢手机之家张建(ajian)[手之家目前利用他来代替Squid作为图片缓存服务器性能表现非常不错!~]帮忙调试排除问题!同时欢迎朋友们与我分享你的心得!

一.Varnish安装

wget ?modtime=1226669272&big_mirror=0
mkdir /data/cache
chown /data/cache/ -R
chmod a+w /data/cache/ -R
./configure --prefix=/usr/local/varnish && make && make install

二.配置vcl.conf
#vi /usr/local/varnish/etc/varnish/vcl.conf
#贴出我的多域名虚拟主机配置,不做细节介绍.

#Cache for linuxtone sites
#backend vhost
backend  img {
.host = "219.235.244.11";
.port = "8080";
}
backend  www {
.host = "211.101.76.140";
.port = "80";
}
backend  netseek {
.host = "211.101.76.141";
.port = "80";
}
#acl
acl purge {
"localhost";
"127.0.0.1";
"192.168.169.0"/24;
}
sub vcl_recv {
if (req.request == "PURGE") {
if (!client.ip ~ purge) {
error 405 "Not allowed.";
}
lookup;
}
if (req.http.host ~ "^imgsns.linuxtone.org") {
set req.backend = img;}
elseif (req.http.host ~ "^(www)|(bbs)|(doc).linuxtone.org") {
set req.backend = www;}
elseif (req.http.host ~ ".netseek.com") {
set req.backend = netseek;}
else {
error 404 "the server is wrong!";
}
if (req.request != "GET" && req.request != "HEAD") {
pipe;
}
elseif (req.url ~ "\.(php|cgi)($|\?)")
{
pass;
}
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 (req.request == "GET" && req.url ~ "\.(txt|js|css)[        DISCUZ_CODE_1        ]quot;) {
set obj.ttl = 10s;
}
else {
set obj.ttl = 1d;
}
}

三.Varnish启动脚本制作
1.启动脚本:

ulimit -SHn 51200
/usr/local/varnish/sbin/varnishd -f /usr/local/varnish/etc/varnish/vcl.conf -a 0.0.0.0:80 \
-n /var/vcache \
-g www -u www \
-T 127.0.0.1:6082 \
-p thread_pool_max=2048 \
-p thread_pools=4 \
-p client_http11=on \
-s file,/data/cache/varnish_cache.data,1024M

2.制作成系统服务启动脚本

四.Varnish管理
1.通过端口进行管理

/usr/local/varnish/bin/varnishadm -T 127.0.0.1:6082 help

2.查看varnish缓存状态,

/usr/local/varnish/bin/varnishstat  

3.通过管理端口清除cache

/usr/local/varnish/bin/varnishadm -T 127.0.0.1:6082 purge.url <regexp>
/usr/local/varnish/bin/varnishadm -T 127.0.0.1:6082 purge.list #列出刚才清除的具体URL列表.

4.日志记录

/usr/local/varnish/bin/varnishncsa -a -w /usr/local/varnish/logs/varnish.log &


varnish日志的rotate
touch /etc/logrotate.d/varnish
/etc/logrotate.d/varnish内容

/usr/local/varnish/logs/varnish.log {
daily
rotate 60
copytruncate
notifempty
missingok
prerotate
killall varnishncsa
endscript
postrotate
/usr/local/varnish/bin/varnishncsa -a -w /usr/local/varnish/logs/varnish.log &
endscript
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/185dc0de18b49d10e285bdd5d844345b.html