Varnish 是高性能開源 HTTP 反向代理/加速器,憑藉「零拷貝」架構與靈活的 VCL 配置語言,在高併發場景下表現優於傳統 Squid,已成為大型站點前端加速首選。本文記錄在 CentOS/RHEL 環境下,從源碼編譯安裝 Varnish 3.0.3 的完整流程,涵蓋內核參數優化、運行用戶/權限、緩存存儲、管理端口、日誌、緩存清除、開機自啟、監控統計及多站點 VCL 範例,供運維參考。
# 安裝編譯依賴
yum install -y automake autoconf libtool ncurses-devel libxslt groff pcre-devel pkgconfig
# 下載源碼包(官方倉庫)
wget https://repo.varnish-cache.org/source/varnish-3.0.3.tar.gz
tar zxf varnish-3.0.3.tar.gz
cd varnish-3.0.3
./autogen.sh
./configure --prefix=/usr/local/varnish
make && make install
安裝後二進制文件位於 /usr/local/varnish/sbin/varnishd,配置目錄 /usr/local/varnish/etc/varnish/。
groupadd www
useradd -g www www
mkdir -p /home/vcache
chown -R www:www /home/vcache
chmod -R 750 /home/vcache
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 生效。上述參數針對高併發短連接場景,縮短 TIME_WAIT、擴大端口範圍、啟用 SYN Cookie 防洪水。
/usr/local/varnish/sbin/varnishd \
-u www -g www \
-f /usr/local/varnish/etc/varnish/varnish.conf \
-a 0.0.0.0:80 \
-s file,/home/vcache/varnish_cache.data,100M \
-w 1024,8192,10 \
-t 3600 \
-T 127.0.0.1:3500 \
-p client_http11=on \
-P /usr/local/varnish/var/varnish.pid
| 參數 | 含義 |
|---|---|
| -u/-g | 運行用戶/組(降權安全) |
| -f | VCL 配置文件路徑 |
| -a | 監聽地址:端口(此處 80) |
| -s | 存儲後端:文件路徑+大小(100M) |
| -w | 線程池:最小、最大、閒置超時(秒) |
| -t | 會話超時(秒) |
| -T | 管理介面 IP:端口(供 varnishadm 連接) |
| -p client_http11=on | 啟用 HTTP/1.1 支持 |
| -P | PID 文件路徑 |
/usr/local/varnish/bin/varnishncsa -n /home/vcache -w /var/log/varnish.log &
-n 指定實例名稱目錄,-w 指定日誌文件,& 後台運行。
varnishadm -T 192.168.1.180:3500 purge "req.http.host ~ www.5013.org$ && req.url ~ /static/image/tp.php"
varnishadm -T 192.168.1.180:3500 "ban.url .*"
varnishadm -T 192.168.1.180:3500 "ban.url /image/"
註:Varnish 3.x 使用 ban 替代舊版 purge,支持正則匹配更靈活。
ulimit -SHn 51200
/usr/local/varnish/sbin/varnishd -u www -g www -f /usr/local/varnish/etc/varnish/varnish.conf -a 0.0.0.0:80 -s file,/home/vcache/varnish_cache.data,100M -w 1024,8192,10 -t 3600 -T 127.0.0.1:3500
/usr/local/varnish/bin/varnishncsa -n /home/vcache -w /var/log/varnish.log &
pkill varnishd/usr/local/varnish/bin/varnishstatbackend blog {
.host = "198.56.193.190";
.port = "80";
}
backend www {
.host = "192.168.1.170";
.port = "80";
}
sub vcl_recv {
# 根據 Host 分發後端
if (req.http.host ~ "^(www\.)?5013\.org$") {
set req.backend = blog;
} elsif (req.http.host ~ "^(www\.)?(test1\.com|test2\.com)$") {
set req.backend = www;
} else {
error 404 "Unknown virtual host";
}
# X-Forwarded-For 鏈
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;
}
}
# 非標準方法直接 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);
}
# 動態頁面(php/cgi)不緩存
if (req.url ~ "\.(php|cgi)(\?|$)") {
return (pass);
}
# 可選:帶 Cookie/Authorization 的請求不緩存
# if (req.http.Authorization || req.http.Cookie) { return (pass); }
return (lookup);
}
sub vcl_pipe { return (pipe); }
sub vcl_pass { return (pass); }
sub vcl_hash {
hash_data(req.url);
if (req.http.host) { hash_data(req.http.host); }
else { hash_data(server.ip); }
return (hash);
}
sub vcl_hit { return (deliver); }
sub vcl_miss { return (fetch); }
sub vcl_fetch {
# Hit-For-Pass:不可緩存對象標記 2 分鐘
if (beresp.ttl <= 0s || beresp.http.Set-Cookie || beresp.http.Vary == "*") {
set beresp.ttl = 120s;
return (hit_for_pass);
}
# 靜態圖片緩存 1 小時並移除 Set-Cookie
if (req.url ~ "\.(png|gif|jpg)$") {
unset beresp.http.set-cookie;
set beresp.ttl = 1h;
}
return (deliver);
}
sub vcl_deliver { return (deliver); }
sub vcl_error {
set obj.http.Content-Type = "text/html; charset=utf-8";
set obj.http.Retry-After = "5";
synthetic {"
Error
Error
Server encountered an error.
"};
return (deliver);
}
vcl_recv 返回 return (hash) 等)。malloc 內存存儲或持久化存儲 (MSE)。varnishstat 中 cache_hit / cache_miss 比例,調整 TTL 與緩存大小。