varnish2.04 配置

varnish 2.04 配置


环境 :centos 5.4
     varnish 2.04
     pcre-8.01

安装
varnish安装,首先要安装pcre

wget http: //blog.s135.com/soft/linux/nginx_php/pcre/pcre-8.01.tar.gz
wget http:
//sourceforge.net/projects/varnish/files/varnish/2.0.4/varnish-2.0.4.tar.gz/download

pcre安装

tar zxvf pcre-8.01.tar.gz
cd pcre-8.01
./configure
make && make install
 
yum install pcre-devel

varnish 安装

tar zxvf varnish-2.0.4
./configure --prefix=/usr/local/varnish
make && make install


vhost.vcl配置如下


backend dvd1 {
                .host =
"174.137.22.10" ;
                .port =
"80" ;
}

backend dvd2 {
                .host =
"174.142.22.115" ;
                .port =
"80" ;
}


acl purge {
            
"localhost" ;
            
"127.0.0.1" ;
            
"174.36.133.0" /28;
}

sub vcl_recv {
            
if (req.request == "PURGE" ) {
                            
if (!client.ip ~ purge) {
                                             error 405
"Not allowed." ;
                             }
                             lookup;
             }




            
if (req.http.host ~ ".dvd11.com" ) {
                             set req.backend = dvd1;
                            
if (req.request != "GET" && req.request != "HEAD" ) {
                                                        pipe;
                                            }
                                            
else {
                                                        lookup;
                                            }
             }


             elseif (req.http.host ~
".dvd22.com" ) {
                             set req.backend = dvd2;
                            
if (req.request != "GET" && req.request != "HEAD" ) {
                                                     pipe;
                                         }
                                        
else {
                                                     lookup;
                                         }
            }


            

            
else {
                                            error 404;
                                            lookup;
             }
}

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

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

sub vcl_fetch {
                set obj.ttl = 30d;
                remove obj.http.Set-Cookie;
}



sub vcl_deliver {
            set resp.http.x-hits = obj.hits ;
            
if (obj.hits > 0) {
                        set resp.http.X-Cache =
"HIT" ;
            }
else {
                        set resp.http.X-Cache =
"MISS" ;
            }
}

启动参数

/usr/local/varnish/sbin/varnishd -n /data/vcache -f /usr/local/varnish/etc/vhost/vhost.vcl -a 0.0.0.0:80 -s file,/data/vcache/varnish_cache.data,1G    -T 127.0.0.1:3500 -p client_http11=on  -p thread_pools=8 -p thread_pool_min=16 -p thread_pool_max=512

varnish启动,停止,开启日志脚本

#!/bin/sh

if [ "$1" = "start" ];then

                /usr/local/varnish/sbin/varnishd -n /data/vcache \
                -f /usr/local/varnish/etc/vhost/vhost.vcl -a 0.0.0.0:80 \
                 -s file,/data/vcache/varnish_cache.data,1G        -T 127.0.0.1:3500 \
                 -p client_http11=on    -p thread_pools=8 -p thread_pool_min=16 -p thread_pool_max=512

elif [
"$1" = "stop" ];then

                killall varnishd

elif [
"$1" = "status" ];then

                /usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 status

elif [
"$1" = "start-log" ];then

                /usr/local/varnish/bin/varnishncsa -n /data/vcache -w /var/log/varnish.log &
else
                echo $0
"{start|stop|status|start-log}"
fi

varnish 日志轮询

参考张宴的脚本稍修改了下  http://blog.s135.com/post/313/

more cutvlog.sh 

#!/bin/sh
vlog=/var/log/varnish-log
date=$(date -d
"yesterday" + "%Y-%m-%d" )
pkill -9 varnishncsa
mv /var/log/varnish.log /var/log/varnish-${date}.log
/usr/local/varnish/bin/varnishncsa -n /data/vcache -w /var/log/varnish.log &
mkdir -p /var/log/varnish-log/
cd /var/log
tar -zcvf    $vlog/varnish-${date}.log.tar.gz varnish-${date}.log
rm -f /var/log/${date}.log
rm -f /var/log/varnish-log/$(date -d
"-1 month" + "%Y-%m*" ).log.tar.gz

可将cutvlog.sh添加到任务中,每天00:00运行

0 0 * * * /data/shell/cutvlogg.sh

批量删除varnish缓存脚本

请参考田哥文章吧。呵呵. http://sery.blog.51cto.com/10037/163993

你可能感兴趣的:(安装,职场,varnish,休闲)