varnish 配置

[root@zong ~]# tar xvf pcre-8.20.tar.gz

[root@zong pcre-8.20]# ./configure --prefix=/usr/local/pcre

[root@zong pcre-8.20]# make && make install

[root@zong ~]# tar xvf varnish-3.0.5.tar.gz

[root@zong ~]# cd varnish-3.0.5

[root@zong varnish-3.0.5]# useradd -s /sbin/nologin varnish

[root@zong varnish-3.0.5]# mkdir -p /data/varnish/cache

[root@zong varnish-3.0.5]# mkdir /data/varnish/log

[root@zong varnish-3.0.5]# chown -R varnish:varnish /data/varnish/cache

[root@zong varnish-3.0.5]# chown -R varnish:varnish /data/varnish/log

[root@zong varnish-3.0.5]# export PKG_CONFIG_PATH=/usr/local/pcre/lib/pkgconfig

[root@zong varnish-3.0.5]# ./configure --prefix=/usr/local/varnish --enable-dependency-tracking --enable-debugging-symbols --enable-developer-warnings

[root@zong varnish-3.0.5]# make && make install

[root@zong varnish-3.0.5]# cp redhat/varnish.initrc /etc/init.d/varnish

[root@zong varnish-3.0.5]# cp redhat/varnish.sysconfig /etc/sysconfig/varnish

[root@localhost varnish]# more /usr/local/varnish/etc/varnish/1.conf

backend webserver {
.host = "192.168.1.30";
.port = "80";
}
sub vcl_recv {
if (req.http.x-forwarded-for) {
# set req.http.X-Forwarded-For = req.http.X-Forwarded-For ", " client.ip;
} else {
set req.http.X-Forwarded-For = client.ip;
}
#如果请求的类型不是GET、HEAD、PUT、POST、TRACE、OPTIONS、DELETE时,进入pipe模式。注意这里是“&&”的关系。
if (req.request != "GET" &&
req.request != "HEAD" &&
req.request != "PUT" &&
req.request != "POST" &&
req.request != "TRACE" &&
req.request != "OPTIONS" &&
req.request != "DELETE") {
return (pipe);
}
#如果请求的类型不是GET与HEAD,则进入pass模式。
if (req.request != "GET" && req.request != "HEAD") {
return (pass);
}
#对123tiger.net或者123tiger.cn两个域名进行缓存加速,这是个泛域名的概念,也就是所有以123tiger.net或者123tiger.cn结尾的域名都进行缓存

if (req.http.host ~ "^(.*).123tiger.net" || req.http.host ~ "^(.*).123tiger.cn") {
set req.backend = webserver;
}
#对以.jsp和.do结尾以及带有?的URL时,直接从后端服务器读取内容。
if (req.url ~ "\.(jsp|do|php)($|\?)") {
return (pass);
} else {
return (lookup);
}
}

sub vcl_pipe {
return (pipe);
}

sub vcl_pass {
return (pass);
}

sub vcl_miss {
return (fetch);
}

sub vcl_fetch {
#对于请求类型是GET,并且请求的URL中包含upload,那么就进行缓存,缓存的时间是300秒,即5分钟。
if (req.request == "GET" && req.url ~ "^/upload(.*)$") {
set beresp.ttl = 300s;
}
#对于请求类型是GET,并且请求的URL以png、xsl、xml、gif、css、js等结尾时,则进行缓存,缓存时间为600秒。
if (req.request == "GET" && req.url ~ "\.(png|xsl|xml|pdf|ppt|doc|docx|chm|rar|zip|bmp|jpeg|swf|ico|mp3|mp4|rmvb|ogg|mov|avi|wmv
|swf|txt|png|gif|jpg|css|js|html|htm)$") {
set beresp.ttl = 600s;
}
return (deliver);
}
#下面是添加一个Header标识,以判断缓存是否命中。
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT from www.123tiger.net";
} else {
set resp.http.X-Cache = "MISS from www.123tiger.net";
}
return (deliver);
}

多台web

[root@zong varnish]# more 1.conf
backend webserver1 {
.host = "192.168.1.30";
.port = "80";
.probe = {
.interval = 10s;
.timeout = 2s;
.window = 3;
.threshold = 3;
}
}
backend webserver2 {
.host = "192.168.0.152";
.port = "80";
.probe = {
.interval = 10s;
.timeout = 2s;
.window = 3;
.threshold = 3;
}
}
backend webserver3 {
.host = "192.168.0.15";
.port = "8082";
.probe = {
.interval = 10s;
.timeout = 2s;
.window = 3;
.threshold = 3;
}
}
acl purge{
"localhost";
"127.0.0.1";
"192.168.1.192"/24;
}

director webserver random{
{.backend = webserver1; .weight = 1; }
{.backend = webserver2; .weight = 1; }
{.backend = webserver3; .weight = 1; }
}
sub vcl_recv {
if (req.http.x-forwarded-for) {
# set req.http.X-Forwarded-For = req.http.X-Forwarded-For ", " client.ip;
} else {
set req.http.X-Forwarded-For = client.ip;
}
#如果请求的类型不是GET、HEAD、PUT、POST、TRACE、OPTIONS、DELETE时,进入pipe模式。注意这里是“&&”的关系。
if (req.request != "GET" &&
req.request != "HEAD" &&
req.request != "PUT" &&
req.request != "POST" &&
req.request != "TRACE" &&
req.request != "OPTIONS" &&
req.request != "DELETE") {
return (pipe);
}
if (req.request == "PURGE") {
if (!client.ip ~ purge) {
error 405 "NOT allowed.";
}
else {
return (lookup);
}
}
#如果请求的类型不是GET与HEAD,则进入pass模式。
if (req.request != "GET" && req.request != "HEAD") {
return (pass);
}
#对123tiger.net或者123tiger.cn两个域名进行缓存加速,这是个泛域名的概念,也就是所有以123tiger.net或者123tiger.cn结尾的域名都进行缓存

if ((req.http.host ~ "^(.*).123tiger.net" || req.http.host ~ "^(.*).123tiger.cn") && (req.restarts == 0)) {
set req.backend = webserver;
}
#对以.jsp和.do结尾以及带有?的URL时,直接从后端服务器读取内容。
if (req.url ~ "\.(jsp|do|php|html|htm|js)($|\?)") {
return (pass);
} else {
return (lookup);
}
}

sub vcl_pipe {
return (pipe);
}

sub vcl_pass {
return (pass);
}

#sub vcl_hash {
# set req.hash += req.url;
# if (req.http.host) {
# set req.hash += req.http.host;
# } else {
# set req.hash += server.ip;
# }
# return (hash);
#}

sub vcl_hit {
# if (!obj.cacheable) {
# return (pass);
# }
return (deliver);
}

sub vcl_miss {
return (fetch);
}

sub vcl_fetch {
# if (!beresp.cacheable) {
# return (pass);
# }
# if (beresp.http.Set-Cookie) {
# return (pass);
# }
#当url中包含servlet时,不进行缓存。
# if (req.url ~ "^/servlet/") {
# return (pass);
# }
#当url中包含services时,不进行缓存。
# if (req.url ~ "^/services/") {
# return (pass);
# }
#对于请求类型是GET,并且请求的URL中包含upload,那么就进行缓存,缓存的时间是300秒,即5分钟。
if (req.request == "GET" && req.url ~ "^/upload(.*)$") {
set beresp.ttl = 300s;
}
#对于请求类型是GET,并且请求的URL以png、xsl、xml、gif、css、js等结尾时,则进行缓存,缓存时间为600秒。
if (req.request == "GET" && req.url ~ "\.(png|xsl|xml|pdf|ppt|doc|docx|chm|rar|zip|bmp|jpeg|swf|ico|mp3|mp4|rmvb|ogg|mov|avi|wmv
|swf|txt|png|gif|jpg|css)$") {
set beresp.ttl = 600s;
}
return (deliver);
}
#下面是添加一个Header标识,以判断缓存是否命中。
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT from www.123tiger.net";
} else {
set resp.http.X-Cache = "MISS from www.123tiger.net";
}
return (deliver);
}


/usr/local/varnish/sbin/varnishd -f /usr/local/varnish/etc/varnish/1.conf -s file,/data/varnish/cache/varnish_cache,1G -T 192.168.0.8:2000 -a 0.0.0.0:80

/usr/local/varnish/bin/varnishncsa -w /data/varnish/log/varnish.log &

posted @ 2014-03-17 16:41  kingtigerhu  阅读(239)  评论(0)    收藏  举报