首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

Squid refresh_pattern 參數深度解析與最佳配置範例

     編輯:PHP 綜合知識

Squid 代理服務器的 refresh_pattern 指令控制無明確過期頭(Expires/Cache-Control)的對象在緩存中的存活策略。本文系統拆解參數含義、LM-factor 算法、常用選項,並給出生產環境常見靜態資源配置模板。

指令語法

refresh_pattern [-i] regex min percent max [options]
參數含義單位/範圍
-i正則不區分大小寫—
regex匹配 URL 的正則表達式POSIX Extended Regex
min最小存活時間(強制新鮮)分鐘,0=無最小限制
percentLM-factor 百分比閾值0~100,見下文算法
max最大存活時間(強制過期)分鐘
options修飾符(override-expire 等)見下表

LM-factor 算法詳解

對象進入緩存後,Squid 追蹤兩個年齡:

  • Resource Age (對象年齡) = Date 頭時間 - Last-Modified 時間。代表源站對象「已存在多久」。
  • Response Age (響應年齡) = 當前時間 - 對象進入緩存時間。代表對象「在緩存待了多久」。

LM-factor = Response Age / Resource Age(無 Last-Modified 則視為 0)。

判斷邏輯:

  1. Response Age < min → 新鮮(直接命中)。
  2. Response Age > max → 過期(必須驗證/重取)。
  3. min ≤ Response Age ≤ max → 計算 LM-factor:
      LM-factor < percent/100 → 新鮮;
      LM-factor ≥ percent/100 → 過期。

常用選項

選項作用適用場景
override-expire忽略源站 Expires,優先檢查 min源站 Expires 過短/不可信,強制最小緩存
override-lastmod忽略 Last-Modified,優先檢查 min源站 Last-Modified 不準,強制最小緩存
reload-into-ims客戶端 Cache-Control: no-cache → 轉發為 If-Modified-Since帶寬受限環境,避免強制刷新穿透源站
ignore-reload忽略請求中的 no-cache/Pragma: no-cache極少變動資源(圖片、視頻、字體),永久緩存直到 purge

生產配置模板(Squid 4/5/6)

# === 核心原則:靜態資源長緩存,動態內容短緩存/不緩存 ===

# 1. 文本類靜態資源:HTML/XML/CSS/JS —— 1 天 min,90% LM,90 天 max,允許 reload-into-ims
refresh_pattern -i \.html?$     1440 90% 129600 reload-into-ims
refresh_pattern -i \.xml$       1440 90% 129600 reload-into-ims
refresh_pattern -i \.css$       1440 90% 129600 reload-into-ims
refresh_pattern -i \.js$        1440 90% 129600 reload-into-ims
refresh_pattern -i \.json$      1440 50% 129600 reload-into-ims  # API 響應較動態,percent 降低

# 2. 圖片/媒體:極少變動,ignore-reload 防止強制刷新浪費帶寬
refresh_pattern -i \.(jpe?g|png|gif|webp|avif|ico|svg)$ 1440 90% 129600 ignore-reload
refresh_pattern -i \.(mp4|webm|ogg|mp3|wav|flac)$       1440 90% 129600 ignore-reload
refresh_pattern -i \.(woff2?|ttf|eot|otf)$              1440 90% 129600 ignore-reload

# 3. 字體/文檔/壓縮包
refresh_pattern -i \.(pdf|docx?|xlsx?|pptx?|zip|tar|gz|rar|7z)$ 1440 50% 129600 reload-into-ims

# 4. 動態腳本/API:不緩存或極短緩存
refresh_pattern -i \.(php|jsp|asp|aspx|do|action)$ 0 0% 0
refresh_pattern -i /api/                          0 0% 0

# 5. 預設兜底:10 分鐘 min,20% LM,1 天 max
refresh_pattern . 10 20% 1440

調試與驗證

# 1. 查看對象緩存狀態
squidclient -h proxy -p 3128 mgr:info | grep -A5 'HTTP/1.1 200'
# 2. 模擬請求觀察 X-Cache
curl -I -x proxy:3128 https://example.com/style.css
# 觀察:X-Cache: HIT/MISS, Age, Cache-Control
# 3. 強制刷新測試 reload-into-ims
curl -I -H 'Cache-Control: no-cache' -x proxy:3128 https://example.com/style.css
# 應見 If-Modified-Since 轉發至源站
# 4. 查看具體對象元數據
squidclient -h proxy -p 3128 mgr:store | grep style.css

避坑指南

  • percent 設 100% 等同於「永不過期(直到 max)」,設 0% 等同於「min 後立即過期」。
  • max 不宜過大(> 1 年),導致磁盤佔用失控、清理困難。
  • 動態頁面(PHP/JSP)必須由應用層輸出 Cache-Control/ETag,Squid 會優先遵守顯式頭,refresh_pattern 僅作兜底。
  • HTTPS 對象默認不緩存(除非配置 ssl_bump 解密),上述規則僅對 HTTP 或已解密流量生效。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved