系統(tǒng)環(huán)境:
CentOS release 5.5 (Final) 64-bit
所需軟件:
varnish-2.1.4.tar.gz
Varnish官方網(wǎng)站:
http://www.varnish-cache.org/
安裝前準(zhǔn)備:
創(chuàng)建apache用戶和組,以及Varnish緩存文件存放目錄(/var/vcache):
/usr/sbin/groupadd apache -g 48
/usr/sbin/useradd -u 48 -g apache apache
mkdir -p /var/vcache
chmod +w /var/vcache
chown -R apache:apache /var/vcache
創(chuàng)建Varnish日志目錄(/var/logs/):
mkdir -p /var/logs
chmod +w /var/logs
chown -R apache:apache /var/logs
安裝:
wget http://repo.varnish-cache.org/source/varnish-2.1.4.tar.gz
tar zxvf varnish-2.1.4.tar.gz
cd varnish-2.1.4
./configure -prefix=/usr/local/varnish
make
make install
cd ..
配置:
默認(rèn)配置文件樣板:
/usr/local/varnish/etc/varnish/default.vcl
cd /usr/local/varnish/etc/varnish/
cp default.vcl elain_vcl.conf
vi elain_vcl.conf
#############################
backend www {
.host = “www.elain.org”;
.port = “80″;
}
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);
}
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″ 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>
<hr>
<p>Varnish cache server</p>
</body>
</html>
“};
return (deliver);
}
###################################
配置文件解釋:
(1)、Varnish通過反向代理請(qǐng)求后端IP為1.0.0.121,端口為80的web服務(wù)器;
(2)、Varnish允許localhost、127.0.0.1、10.0.0.***三個(gè)來源IP通過PURGE方法清除緩存;
(3)、Varnish對(duì)域名為www.elain.org的請(qǐng)求進(jìn)行處理,非www.elain.org域名的請(qǐng)求則返回“elain Cache Server”;
(4)、Varnish對(duì)HTTP協(xié)議中的GET、HEAD請(qǐng)求進(jìn)行緩存,對(duì)POST請(qǐng)求透過,讓其直接訪問后端Web服務(wù)器。之所以這樣配置,是因?yàn)镻OST請(qǐng)求一般是發(fā)送數(shù)據(jù)給服務(wù)器的,需要服務(wù)器接收、處理,所以不緩存;
(5)、Varnish對(duì)以.txt和.js結(jié)尾的URL緩存時(shí)間設(shè)置1小時(shí),對(duì)其他的URL緩存時(shí)間設(shè)置為30天。
啟動(dòng)Varnish
ulimit -SHn 65535
/usr/local/varnish/sbin/varnishd -f /usr/local/varnish/etc/varnish/elain_vcl.conf -a /var/vcache -s malloc,1G -u apache -g apache -T 127.0.0.1:2000 -a 0.0.0.0:8080
注:通常我們apache或nginx等 WEB 會(huì)使用8080端口,這樣 我們就把上面的啟動(dòng)參數(shù)選項(xiàng)-a去掉,讓其默認(rèn)使用80端口(也就是在配置文件里的端口)
啟動(dòng)varnishncsa用來將Varnish訪問日志寫入日志文件:
/usr/local/varnish/bin/varnishncsa -n /var/vcache -w /var/logs/varnish.log &
停止Varnish
pkill varnish
配置開機(jī)自動(dòng)啟動(dòng)Varnish
vi /etc/rc.local
在末尾增加以下內(nèi)容:
ulimit -SHn 65535
/usr/local/varnish/sbin/varnishd -f /usr/local/varnish/etc/varnish/elain_vcl.conf -n /var/vcache -s malloc,1G -u apache -g apache -T 127.0.0.1:2000
/usr/local/varnish/bin/varnishncsa -n /var/vcache -w /var/logs/youvideo.log &
優(yōu)化Linux內(nèi)核參數(shù)
vi /etc/sysctl.conf
在末尾增加以下內(nèi)容:
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
sysctl -p
管理Varnish:
1、查看Varnish服務(wù)器連接數(shù)與命中率:
/usr/local/varnish/bin/varnishstat
2、通過Varnish管理端口進(jìn)行管理:
用help看看可以使用哪些Varnish命令:
/usr/local/varnish/bin/varnishadm -T 127.0.0.1:2000 help
[root@postfix varnish]# /usr/local/varnish/bin/varnishadm -T 127.0.0.1:2000 help
help [command]
ping [timestamp]
auth response
quit
banner
status
start
stop
stats
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>
purge.url <regexp>
purge <field> <operator> <arg> [&& <field> <oper> <arg>]…
purge.list
3、通過Varnish管理端口,使用正則表達(dá)式批量清除緩存:
(1)、例:清除類似http://www.elain.org/tmp/aa.html的URL地址):
/usr/local/varnish/bin/varnishadm -T 127.0.0.1:2000 url.purge /tmp/
(2)、例:清除類似http://www.elain.org/dl 的URL地址:
/usr/local/varnish/bin/varnishadm -T 127.0.0.1:2000 url.purge w*$
(3)、例:清除所有緩存:
/usr/local/varnish/bin/varnishadm -T 127.0.0.1:2000 url.purge *$
每天0點(diǎn)運(yùn)行,按天切割Varnish日志,生成一個(gè)壓縮文件,同時(shí)刪除上個(gè)月舊日志的腳本(/var/logs/cutlog.sh):
/var/logs/cutlog.sh文件內(nèi)容如下:
#!/bin/sh
# This script run at 00:00
date=$(date -d “yesterday” +”%Y-%m-%d”)
pkill -9 varnishncsa
mv /var/logs/youvideo.log /var/logs/${date}.log
/usr/local/varnish/bin/varnishncsa -n /var/vcache -w /var/logs/youvideo.log &
mkdir -p /var/logs/youvideo/
gzip -c /var/logs/${date}.log > /var/logs/youvideo/${date}.log.gz
rm -f /var/logs/${date}.log
rm -f /var/logs/youvideo/$(date -d “-1 month” +”%Y-%m*”).log.gz
設(shè)置在每天00:00定時(shí)執(zhí)行:
crontab -e
0 0 * * * /bin/sh /var/logs/cutlog.sh
配置實(shí)例:
#Example 1 – manipulating headers
#Lets say we want to remove the cookie for all objects in the /static directory of our web server::
sub vcl_recv {
if (req.url ~ “^/images”) {
unset req.http.cookie;
}
}
Now, when the request is handled to the backend server there will be no cookie header. The interesting line is the one with the if-statement. It matches the URL, taken from the request object, and matches it against the regular . Note the match operator. If it matches the Cookie: header of the request is unset (deleted).
#Example 2 – manipulating beresp
#Here we override the TTL of a object comming from the backend if it matches certain criteria::
sub vcl_fetch {
if (beresp.url ~ “\.(png|gif|jpg)$”) {
unset beresp.http.set-cookie;
set beresp.ttl = 3600;
}
}
#Example 3 – ACLs?
#You create a named access control list with the acl keyword. You can match the IP address of the client against an ACL with the match operator.:
# Who is allowed to purge….
acl local {
“l(fā)ocalhost”;
“10.0.0.0.0″/24; /* and everyone on the local network */
! “10.0.0.0.23″; /* except for the dialin router */
}sub vcl_recv {
if (req.request == “PURGE”) {
if (client.ip ~ local) {
return(lookup);
}
}
}sub vcl_hit {
if (req.request == “PURGE”) {
set obj.ttl = 0s;
error 200 “Purged.”;
}
}sub vcl_miss {
if (req.request == “PURGE”) {
error 404 “Not in cache.”;
}
}
補(bǔ)充幾條相關(guān)命令
查看Varnish狀態(tài)
/usr/local/varnish/bin/varnishstat -n /var/vcache/
查看訪問最多的Referer
/usr/local/varnish/bin/varnishtop -n /var/vcache/ -i rxheader -I Referer
查看訪問最多的URL
/usr/local/varnish/bin/varnishtop -n /var/vcache/ -i rxurl
官方文檔:
http://www.varnish-cache.org/docs/2.1/
FAQ:
1、安裝出錯(cuò)
configure: error: Package requirements (libpcre) were not met:
No package ‘libpcre’ found
答:yum install pcre-devel -y