环境:Ubuntu 9.10; squid监听173.26.100.214:80 ; varnish监听173.26.100.214:81
1.安装
*在http://sourceforge.net/projects/varnish/files/varnish/2.0.4/varnish-2.0.4.tar.gz下载varnish2.0.4.
*tar zxvf varnish-2.0.4.tar.gz
*在解压后的varnish-2.0.4目录下,运行./autogen.sh
*./configure –prefix [指定的目录以下记为dir] –enable-developer-warnings –enable-debugging-symbols
*make
*make install
2.配置
创建cache目录:
mkdir -p /cache/varnish/V && chown -R 777 /cache
编写启动脚本,下面是我的启动脚本:
#!/bin/sh
#file:start.sh
date -u
/usr/local/varnish/sbin/varnishd -a 173.26.100.214:81 -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
在dir/etc/varnish/default.vcl下修改配置,下面是我配置的一部分:
backend default {
.host = “173.26.100.214”;
.port = “80”;
} /*squid做后端*/
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 {
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 {“
<?xml version=”1.0″ encoding=”utf-8″?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html>
<head>
<title>”} obj.status ” ” obj.response {“</title>
</head>
<body>
<h1>Error “} obj.status ” ” obj.response {“</h1>
<p>”} obj.response {“</p>
<h3>Guru Meditation:</h3>
<p>XID: “} req.xid {“</p>
<address>
<a href=”http://www.varnish-cache.org/”>Varnish</a>
</address>
</body>
</html>
“};
return (deliver);
}
经测试,varnish做代理的访问速度比squid最快大10倍多