首先,说说Varnish的配置方法。
Varnish的启动需要配置文件(*.vcl),以及其他一些启动参数配合(具体参数在此略去不谈,man一下会看到一切)。我安装的是Varnish-2.0.4
整个安装过程如下:
#./configure --prefix=/usr/local/varnish --enable-developer-waring --enable-debugging-sybmbles --enable-werror
#make
#make install
编译安装完成,然后就需要启动它,我写的启动文件如下:
###Start.sh######
#!/bin/sh
#file:start.sh
date -u
/usr/local/varnish/sbin/varnishd -a 173.26.100.206:80 -s file,/cache/varnish/V,1024m -f /usr/local/varnish/etc/varnish/default.vcl -p thread_pool_max=1500 -p thread_pools=5 -p listen_depth=512 -p client_http11=on -p connect_timeout = 1s -p send_timeout = 20s
以下是deault.vcl配置
######defualt.vcl########
backend default {
.host = "173.26.100.206";
.port = "81";
}
#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.request != "GET" &&
req.request != "HEAD" &&
req.request != "PUT" &&
req.request != "POST" &&
req.request != "TRACE" &&
req.request != "OPTIONS" &&
req.request != "DELETE") {
/* Non-RFC2616 or CONNECT which is weird. */
return (pipe);
}
if (req.request != "GET" && req.request != "HEAD") {
/* We only deal with GET and HEAD by default */
return (pass);
}
if (req.http.Authorization || req.http.Cookie) {
/* Not cacheable by default */
return (pass);
}
return (lookup);
}
sub vcl_pipe {
# Note that only the first request to the backend will have
# X-Forwarded-For set. If you use X-Forwarded-For and want to
# have it set for all requests, make sure to have:
# set req.http.connection = "close";
# here. It is not set by default as it might break some broken web
# applications, like IIS with NTLM authentication.
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 (!obj.cacheable) {
return (pass);
}
if (obj.http.Set-Cookie) {
return (pass);
}
set obj.prefetch = -30s;
return (deliver);
}
sub vcl_deliver {
return (deliver);
}
sub vcl_discard {
/* XXX: Do not redefine vcl_discard{}, it is not yet supported */
return (discard);
}
sub vcl_prefetch {
/* XXX: Do not redefine vcl_prefetch{}, it is not yet supported */
return (fetch);
}
sub vcl_timeout {
/* XXX: Do not redefine vcl_timeout{}, it is not yet supported */
return (discard);
}
sub vcl_error {
set obj.http.Content-Type = "text/html; charset=utf-8";
synthetic {"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
Error "} obj.status " " obj.response {"
"} obj.response {"
Guru Meditation:
XID: "} req.xid {"
Varnish
"};
return (deliver);
}
如 此配置,运行./Start.sh启动Varnish监听本机80端口,backend为Apache监听的81端口。问题出现了,通过81端口访问,正 常响应,浏览器获得正确页面;访问81端口,出现503 error。对于这个问题在网上搜了很多方法试图解决,比较多的办法是在配置文件里修改添加:
backend default {
.host = "173.26.100.206";
.port = "81";
###下面三行为新加配置
.connect_timeout = 1s;
.first_byte_timeout = 5s;
.between_bytes_timeout = 2s;
}
事实上并不奏效,自己摸索良久,偶然将start.sh中一些启动选项去掉,结果发现可以正常响应了。
新的启动文件如下:
####Start.sh#####
#!/bin/sh
#file:start.sh
date -u
/usr/local/varnish/sbin/varnishd -a 173.26.100.206:80 -s file,/cache/varnish/V,1024m -f /usr/local/varnish/etc/varnish/default.vcl -p thread_pool_max=1500 -p thread_pools=5 -p listen_depth=512 -p client_http11=on
去掉了connect_timeout和send_timeout,相当于使用系统默认配置。
结果一切归于正常,Varnish又一次表现出高速的http代理能力。