2013-09-03 10:48:11
来 源
itjs.cn
Varnish
本文介绍Varnish3.0.3详细安装配置过程,希望对于Varnish入门者给与帮助。更多Varnish资源请本站内搜索。

1.安装pcre库

cd pcre

./configure --prefix=/usr/local/pcre

make && make install

2.安装varnish

tar -zxvf varnish-3.0.3.tar.gz

cd varnish-3.0.3

export PKG_CONFIG_PATH=/usr/local/pcre/lib/pkgconfig/

./configure --prefix=/usr/local/webserver/varnish

make && make install

3.配置

backend webserver1 {

     .host = "127.0.0.1";

     .port = "8197";

}

backend webserver2 {

     .host = "127.0.0.1";

     .port = "5566";

}

director videowebserver random{

     {.backend = webserver1;.weight = 5;}

     {.backend = webserver2;.weight = 5;}

}

acl purge{

    "localhost";

    "127.0.0.1";

“192.168.0.1”/26;

}

# Below is a commented-out copy of the default VCL logic.  If you

# redefine any of these subroutines, the built-in logic will be

# appended to your code.

sub vcl_recv {

#开启压缩模式,图片格式取消压缩

        if (req.http.Accept-Encoding) {

                if (req.url ~ ".(jpg|png|gif|jpeg|flv|swf|ico|bmp|tif|tiff|img)" ) {

                        remove req.http.Accept-Encoding;

                        remove req.http.Cookie;

                } else if (req.http.Accept-Encoding ~ "gzip") {

                        set req.http.Accept-Encoding = "gzip";

                } else if (req.http.Accept-Encoding ~ "deflate") {

                        set req.http.Accept-Encoding = "deflate";

                } else {

                        remove req.http.Accept-Encoding;

                }

        }

if (req.request == "PURGE") {

               if (!client.ip ~ purge) {

                       error 405 "Not allowed.";

}elseif (req.url ~ ".(php|php5|cgi) ($|?)"){

                        return (pass);

                }

               return (lookup);

       }

if (req.http.host ~ "^192.168.0.1:5577") {

               set req.backend = videowebserver;

               if (req.request != "GET" && req.request != "HEAD") {

                       return (pipe);

               }

                elseif (req.url ~ ".(cgi|php|php5) ($|?)")

                {

                        return (pass);

                }

                elseif (req.http.Authenticate || req.http.Authorizationi || req.http.Cookie){

                        return (pass);

                }

               return (lookup);

       }else {

                if (req.http.host ~ "^112.124.0.233:5679") {

                        set req.backend = webserver1;

                }else{

                        error 404 "zhukai Cache Server";

                }

               return (lookup);

       }

if (req.request != "GET" &&

       req.request != "HEAD" &&

       req.request != "PUT" &&

       req.request != "POST" &&

       req.request != "TRACE" &&

       req.request != "OPTIONS" &&

       req.request != "DELETE") {

         return (pipe);

     }

     return (lookup);

}

sub vcl_pipe {

     return (pipe);

}

sub vcl_pass {

     return (pass);

 }

sub vcl_hash {

     hash_data(req.url);

     if (req.http.host) {

         hash_data(req.http.host);

     } else {

         hash_data(server.ip);

     }

     return (hash);

 }

sub vcl_hit {

        if (req.request == "PURGE") {

                set obj.ttl = 0s;

                error 200 "purge";

        }

        #if (obj.http.Vary) {

        #       unset obj.http.Vary;

        #}

     return (deliver);

}

sub vcl_miss {

        if(req.request == "PURGE") {

                error 404 "not in cache";

        }

     return (fetch);

}

}

sub vcl_fetch {

        #if (beresp.http.Set-Cookie) {

        #       return (pass);

        #}

if (req.request == "GET" && req.url ~ ".(css|js|html|htm)$") {

                set beresp.ttl = 1d;

        }

if( req.request == "GET" && req.url ~ ".(gif|jpg|jpeg|png|bmp|ico|img|bmp|xmf|tif|tiff)$") {

                set beresp.ttl = 7d;

        }

if( req.request == "GET" && req.url ~ ".(swf|mp3|mp4|wav|wmv|avi|rmvb)$") {

                set beresp.ttl = 10d;

        }

return (deliver);

}

sub vcl_deliver {

        if (obj.hits > 0) {

                set resp.http.X-Cache = "hit from v.q.com";

        }else{

                set resp.http.X-Cache = "miss from v.l.com";

        }

    return (deliver);

}

4.启动varnish

mkdir -p /data_disk/varnish/cache/ 

touch /data_disk/varnish/log

chmod -R 777 /data_disk/varnish

/data_disk/webserver/sbin/varnishd -f /data_disk/webserver/etc/varnish/movie.vcl -s malloc,512M -a0.0.0.0:5577 -w 512,51200,10 -T 127.0.0.1:3500 -n /data_disk/varnish/cache/ -p thread_pools=4 ,lru_interval=43200,listen_depth=5000

thread_pools:用来设置线程池的数量。一般和cpu数目相同就好,设置多一些pool,varnish并发处理能力更强,但是会消耗更多的cpu和内存。

lru_inerval:内存中一个对象超过了这个时间还没有被重用时,就从lru队列中移除

listen_depth:设置tcp连接的队列长度,设置大一些提高并发能力

5.启动varnishncsa用来将Varnish访问日志写入日志文件:

/data_disk/webserver/bin/varnishncsa -n /data_disk/varnish/cache/ -w /data_disk/varnish/log &

6.查看varnish进程

ps -ef | grep varnish

察看端口80和3500处于监听状态

netstan -antl | grep 3500

7.管理varnish运行日志

#!/bin/sh

date=$(date -d "yesterday" +"%Y-%m-%d")

pkill -9 varnishncsa

mv /data_disk/varnish/log /data_disk/varnish/logs/${date}.log

/usr/local/webserver/varnish/bin/varnishncsa -n /data_disk/varnish/cache/ -w /data_disk/varnish/log &

8.配置开机启动varnish

vi /etc/rc.local

/usr/local/webserver/varnish/sbin/varnishd -f /usr/local/webserver/varnish/etc/varnish/movie.vcl -s malloc,512M -a0.0.0.0:5577 -w 512,51200,10 -T 127.0.0.1:3500 -n /data_disk/varnish/cache/ -p thread_pools=4,lru_interval=43200,listen_depth=5000

/usr/local/webserver/varnish/bin/varnishncsa -n /data_disk/varnish/cache/ -w /data_disk/varnish/log &

9.优化linux内核参数

vi /etc/sysctl.conf

在末尾增加以下内容:

net.ipv4.tcp_fin_timeout = 30

net.ipv4.tcp_keepalive_time = 300

net.ipv4.tcp_syncookies = 1

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_tw_recycle = 1

net.ipv4.ip_local_port_range = 5000    65000

10.管理varnish

10.1察看varnish缓存效果

[email protected]:/home/zhukai# curl -I http://111.111.111.111/movie-1.html

HTTP/1.1 200 OK

Server: nginx/1.2.4

Content-Type: text/html; charset=utf-8

Vary: Accept-Encoding

X-Powered-By: PHP/5.2.14

Pragma: no-cache

Date: Fri, 29 Nov 2013 07:30:38 GMT

X-Varnish: 580099933

Age: 0

Via: 1.1 varnish

Connection: keep-alive

X-Cache: miss from v.languang.com

[email protected]:/home/zhukai# curl -I http://111.111.111.111/movie-1.html

HTTP/1.1 200 OK

Server: nginx/1.2.4

Content-Type: text/html; charset=utf-8

Vary: Accept-Encoding

X-Powered-By: PHP/5.2.14

Pragma: no-cache

Date: Fri, 29 Nov 2013 07:30:44 GMT

X-Varnish: 580099934 580099933

Age: 5

Via: 1.1 varnish

Connection: keep-alive

X-Cache: hit from v.languang.com

前后对比缓存命中

察看varnish缓存状态

/data_disk/webserver/bin/varnishstat -n /data_disk/varnish/cache/

10.2通过Varnish管理端口进行管理:

/data_disk/webserver/bin/varnishadm -T 127.0.0.1:3500 help

help [command]

ping [timestamp]

auth response

quit

banner

status

start

stop

vcl.load <configname> <filename>

vcl.inline <configname> <quoted_VCLstring>

vcl.use <configname>

vcl.discard <configname>

vcl.list

vcl.show <configname>

param.show [-l] [<param>]

param.set <param> <value>

panic.show

panic.clear

storage.list

backend.list

backend.set_health matcher state

ban.url <regexp>

ban <field> <operator> <arg> [&& <field> <oper> <arg>]...

ban.list

10.3 通过varnish管理端口清除缓存

清除缓存内容命令格式如下:

/data_disk/webserver/bin/varnishadm -T 127.0.0.1:3500 ban.url <regexp>

列出最近清除的详细url列表

/data_disk/webserver/bin/varnishadm -T 127.0.0.1:3500 ban.list

(可能各个版本不同)

1.清除/a/b/s*.html

/data_disk/webserver/bin/varnishadm -T 127.0.0.1:3500 ban.url ^/a/b/s.*$

2.一个清除Squid缓存的PHP函数(清除Varnish缓存同样可以使用该函数,无需作任何修改,十分方便):

<?php

function purge($ip, $url)

{

    $errstr = '';

    $errno = '';

    $fp = fsockopen ($ip, 80, $errno, $errstr, 2);

    if (!$fp)

    {

         return false;

    }

    else

    {

        $out = "ban.url $url HTTP/1.1rn";

        $out .= "Host:example.comrn";

        $out .= "Connection: closernrn";

        fputs ($fp, $out);

        $out = fgets($fp , 4096);

        fclose ($fp);

        return true;

    }

}

purge("192.168.0.4", "/index.php");

?>

grace mode如果后端需要很长时间来生成一个对象,这里有一个线程堆积的风险。为了避免这 种情况,你可以使用 Grace。他可以让varnish 提供一个存在的版本,然后从后端生成新 的目标版本。

当同时有多个请求过来的时候,varnish只发送一个请求到后端服务器,在“set beresp.grace = 30m; ”时间内复制旧的请求结果给客户端。

vcl_fetch

如果您的服务每秒有数千万的点击率,那么这个队列是庞大的,没有用户喜欢等待服务器响应。为了使用过期的 cache 给用户提供服务,我们需要增加他们的 TTL,保存所有cache 中的内容在 TTL过期以后30 分钟内不删除,使用以下VCL:

sub vcl_fetch {

set beresp.grace = 30m;

}

vcl_recv

Varnish 还不会使用过期的目标给用户提供服务,所以我们需要配置以下代码,在cache过期后的15 秒内,使用旧的内容提供服务:

sub vcl_recv {

set req.grace = 15s;

}

为什么要多保存过去的内容 30 分钟?当然,如果你使用了健康检查,你可以通过健康状态设置保存的时间:

if (! req.backend.healthy) {

set req.grace = 5m;

} else {

set req.grace = 15s;

}

声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。