本文目標是在同一台機器上讓 Varnish 與系統自帶的 Apache 並存(連接埠不同),並驗證 Varnish 確實快取了後端 Apache 的內容。
環境:RedHat 4.8 64 位,IP 192.168.5.55,Varnish 2.1.2。
強烈建議版本與本文一致,因為 1.x 與 2.x 的 VCL 語法差異不小,混用會很痛苦。
tar -zxvf varnish-2.1.2.tar.gz cd varnish-2.1.2 ./configure --prefix=/usr/local/varnish --enable-dependency-tracking \ --enable-debugging-symbols --enable-developer-warnings
若提示 no package 'libpcre' found,指定 pcre 的 pkgconfig 路徑即可(源碼安裝的通常在安裝目錄下的 lib/pkgconfig):
export PKG_CONFIG_PATH=你的pcre庫路徑 make && make install
預設配置全是註解,位於 /usr/local/varnish/etc/varnish/。先備份再新建:
backend webserver {
.host = "192.168.5.55";
.port = "9999";
}
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;
}
# 非標準方法直接 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 不快取
if (req.request != "GET" && req.request != "HEAD") {
return (pass);
}
# 泛域名匹配,交給後端
if (req.http.host ~ "^(.*).zzz.com") {
set req.backend = webserver;
}
}
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); }
}
# 加上自訂標頭,方便判斷是否命中
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT from www.zzz.com";
} else {
set resp.http.X-Cache = "MISS from www.zzz.com";
}
return (deliver);
}
/usr/local/varnish/sbin/varnishd \ -f /usr/local/varnish/etc/varnish/default.vcl \ -s malloc,128m -T 127.0.0.1:200 -a 0.0.0.0:9000
參數說明:-f 指定配置檔;-s 指定存儲類型與容量(malloc 是 C 的記憶體配置函數,128m 即配置 128MB);-T 是文字管理介面;-a 指定監聽位址與埠。
ps -ef | grep varnishd netstat -an | grep 9000 # 代理埠 netstat -an | grep 200 # 管理埠
Apache 配置基本不動,只把埠改為 9999,並在 /var/www/html 建一個 index.html:
/etc/init.d/httpd start # 本機 hosts 加上映射 192.168.5.55 www.zzz.com
兩邊內容相同,光看頁面無法判斷:
curl https://www.zzz.com:9000 # hello!!! curl https://www.zzz.com:9999 # hello!!!
看回應標頭裡自訂的 X-Cache 就清楚了。首次是 MISS:
curl -I https://www.zzz.com:9000 # X-Varnish: 337763601 # Age: 0 # X-Cache: MISS from www.zzz.com
再請求一次變成 HIT,且 X-Varnish 出現兩個 ID、Age 遞增:
curl -I https://www.zzz.com:9000 # X-Varnish: 337763602 337763601 # Age: 1 # X-Cache: HIT from www.zzz.com
在 VCL 裡設定沒生效的話,實務上通常在後端伺服器設定。另外要知道:Varnish 若不指定快取時間,預設只有 120 秒。編輯 httpd.conf:
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/gif A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType image/png A2592000
ExpiresByType image/x-icon A2592000
ExpiresByType application/x-javascript A604800
ExpiresByType text/css A604800
ExpiresByType text/html A125
</IfModule>
/etc/init.d/httpd restart
再抓一次標頭,Cache-Control: max-age=125 與 Expires 都出現了,代表生效(Expires 顯示的是 GMT,換算時差要加 8 小時):
curl -I https://www.zzz.com:9000 # Cache-Control: max-age=125 # Expires: Tue, 13 Nov 2012 09:14:28 GMT # X-Cache: MISS from www.zzz.com
/usr/local/varnish/bin/varnishstat
輸出中值得留意的欄位:Hitrate ratio/Hitrate avg(命中率)、Client connections accepted、Client requests received、Cache hits、Cache misses、Backend conn. success、N struct sess_mem、N worker threads、SMA bytes allocated/free、Total Sessions、Total fetch、Total header bytes、Total body bytes、N expired objects、N LRU moved objects 等。
# 依正則清除 /usr/local/varnish/bin/varnishadm -T 127.0.0.1:200 purge.url <regexp> # 列出最近清除的 URL /usr/local/varnish/bin/varnishadm -T 127.0.0.1:200 purge.list # 清空全部快取 /usr/local/varnish/bin/varnishadm -T 127.0.0.1:200 purge.url ^.*$