和Squid相比Varnish更适合在大内存的server上使用。

软件:varnish-1.1.2.tar.gz

安装过程
#/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

 

Also see:

  • No Related Post
Subscribe in reader