简述:Varnish是一款高性能的开源HTTP加速器,挪威最大的在线报纸 Verdens Gang 使用3台Varnish代替了原来的12台Squid,性能比以前更好.
系统环境:
CentOS release 5.5 (Final) 64-bit
所需软件:
varnish -2.1.4.tar.gz
Varnish官方网站:
http://www.varnish -cache.org/
安装前准备:
创建www用户和组,以及Varnish缓存文件存放目录(/elain/data/vcache):
/usr/sbin/groupadd www -g 600
/usr/sbin/useradd -u 600 -g www www
mkdir -p /elain/data/vcache
chmod w /elain/data/vcache
chown -R www:www /elain/data/vcache
创建Varnish日志目录(/elain/logs/varnish):
mkdir -p /elain/logs/varnish
chmod w /elain/logs/varnish
chown -R www:www /elain/logs/varnish
安装:
wget http://cdnetworks-kr-1.dl.sourceforge.net/project/pcre/pcre/8.12/pcre-8.12.tar.gz
tar zxvf pcre-8.12.tar.gz
cd pcre-8.12/
./configure --prefix=/elain/apps/pcre
make && make install
cd ../
wget http://repo.varnish-cache.org/source/varnish-2.1.5.tar.gz
tar zxvf varnish-2.1.5.tar.gz
cd varnish-2.1.5
export PKG_CONFIG_PATH=/elain/apps/pcre/lib/pkgconfig
./configure -prefix=/elain/apps/varnish
make
make install
cd ..
配置:
默认配置文件样板:
/elain/apps/varnish/etc/varnish/default.vcl
cd /elain/apps/varnish/etc/varnish/
cp default.vcl elain_vcl.conf
vi elain_vcl.conf
#############################
backend www {
.host = "www.elain.org";
.port = "80";
}
acl purge {
"localhost";
"127.0.0.1";
"172.16.2.0"/24;
}
sub vcl_recv {
if (req.restarts == 0) {
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;
}
}
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);
}
else {
lookup;
}
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);
}
return (deliver);
}
sub vcl_deliver {
return (deliver);
}
sub vcl_error {
set obj.http.Content-Type = "text/html; charset=utf-8";
synthetic {"
<?xml version="1.0" e