萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 網路安全 >  >> 網路應用技術 >> 應用程式教學

Varnish 3.0.3 從源碼編譯安裝與配置筆記(含多站點、緩存策略、啟動管理)

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

四、內核參數優化(/etc/sysctl.conf)

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 防洪水。

五、啟動 Varnishd 核心參數詳解

/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運行用戶/組(降權安全)
-fVCL 配置文件路徑
-a監聽地址:端口(此處 80)
-s存儲後端:文件路徑+大小(100M)
-w線程池:最小、最大、閒置超時(秒)
-t會話超時(秒)
-T管理介面 IP:端口(供 varnishadm 連接)
-p client_http11=on啟用 HTTP/1.1 支持
-PPID 文件路徑

六、啟動 varnishncsa 記錄訪問日誌

/usr/local/varnish/bin/varnishncsa -n /home/vcache -w /var/log/varnish.log &

-n 指定實例名稱目錄,-w 指定日誌文件,& 後台運行。

七、緩存清除操作(varnishadm)

  • 精確清除單一 URL:
    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,支持正則匹配更靈活。

八、加入開機自啟(/etc/rc.local)

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/varnishstat
  • 時間同步:安裝 ntp,配置 crontab 定時校準

十、附件:多主機多域名 VCL 配置範例 (varnish.conf)

backend 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); }

十一、避坑提示

  • Varnish 3.x 官方已停止維護,生產環境建議升級至 6.0 LTS 或 7.x 版本,VCL 語法有變化(如 vcl_recv 返回 return (hash) 等)。
  • 文件存儲 (-s file) 適合中小流量,大流量建議使用 malloc 內存存儲或持久化存儲 (MSE)。
  • 管理端口 (-T) 僅綁定 127.0.0.1,防止未授權訪問;如需遠程管理請配合 SSH 隧道或防火牆限制。
  • 定期檢查 varnishstat 中 cache_hit / cache_miss 比例,調整 TTL 與緩存大小。
copyright © 萬盛學電腦網 all rights reserved