一、安装varnish
1、下载varnish-2.0.4.tar.gz
2、tar -zxvf varnish-2.0.4.tar.gz
3、cd varnish-2.0.4
4、./configure --prefix=/usr/local/varnish --enable-debugging-symbols --enable-developer-warnings --enable-dependency-tracking
若你的gcc版本是4.2.0以上的加上:--enable-extra-warnings
查看gcc版本的方法是: rpm -qa | grep gcc
5、make
6、make install
二、配置varnish:
首先建立日志文件目录,mkdir /usr/local/varnish/logs
主要是记录访问日志的文件目录
其次找到_.vsl,用的命令是:find / -name _.vsl
找到后调整start.sh脚本里面最后一句 -n /usr/local/var/varnish/nagios-server/ 的内容为_.vsl的目录
1、vi start.sh
以下内容;
#!/bin/sh
# file: start.sh
date -u
/usr/local/varnish/sbin/varnishd /
-a :80 /
-s file,/usr/local/varnish/var/varnish,1024m /
-f /usr/local/varnish/vcl.conf /
-p thread_pool_max=1500 /
-p thread_pools=5 /
-p listen_depth=512 /
-p client_http11=on /
-T 127.0.0.1:5000
/usr/local/varnish/bin/varnishncsa -n /usr/local/var/varnish/nagios-server/ -w /usr/local/varnish/logs/varnish.log &
2、chmod u+x start.sh
3、vi vcl.conf
以下内容:
backend blog
{
.host = "192.168.1.221";
.port = "http";
}
#acl
acl purge
{
"localhost";
"127.0.0.1";
"192.168.0.0"/16;
}
sub vcl_recv
{
if (req.request == "PURGE")
{
if (!client.ip ~ purge)
{
error 405 "Not allowed.";
}
lookup;
}
if (req.http.Cache-Control ~ "no-cache")
{
purge_url(req.url);
}
# if ( req.url ~"^/user_self_photo" )
# {
# pipe;
# }
if (req.request == "GET" && req.url ~ "/.(gif|jpg|swf|css|js)$")
{
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_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_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|bmp|css|)$")
{
set obj.ttl = 3600s;
}
else
{
set obj.ttl = 60s;
}
}
4、启动varnish: ./start.sh
5、添加日志滚动脚本:
vi roatate.sh
内容如下:
#!/bin/sh
date=` date +"%Y-%m-%d" `
cp /usr/local/varnish/logs/varnish.log /usr/local/varnish/logs/access$date.log
>/usr/local/varnish/logs/varnish.log
6、chmod u+x roatate.sh