Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> 電腦軟體教學 >> 伺服器技術 >> 關於伺服器

Web 快取機制深度解析:Expires、Cache-Control、Last-Modified、ETag 完整指南

編輯:關於伺服器

核心快取標頭對照

標頭分類HTTP 版本作用優先級
Expires過期控制1.0絕對過期時間 (GMT)低
Cache-Control過期控制1.1相對秒數、指令集高 (覆蓋 Expires)
Last-Modified驗證標記1.0/1.1最後修改時間 (秒精度)弱驗證
ETag驗證標記1.1實體指紋 (強驗證)強驗證

Expires 詳細機制

  • 格式:Expires: Wed, 21 Oct 2025 07:28:00 GMT
  • Apache mod_expires 可依 MIME 類型自動添加:
    ExpiresActive On
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType text/html "access plus 0 seconds"  # 動態頁不快取
  • 動態頁面 (PHP/ASP.NET) 若未主動設定,Apache 預設回傳 Expires: Wed, 11 Jan 1984 05:00:00 GMT (永遠過期)。
  • 瀏覽器若無 Expires,會根據 Last-Modified 推算啟發式過期時間 (通常為距今 10% 時間差)。

Cache-Control 完整指令集

指令方向說明
max-age=秒數請求/回應資源新鮮壽命 (秒)
s-maxage=秒數回應僅針對共用快取 (Proxy/CDN)
public回應可被任何快取儲存
private回應僅客戶端私有快取可存
no-cache請求/回應**必須向源站驗證** 後才能使用快取 (非不快取)
no-store請求/回應**嚴禁快取** 任何部分 (含磁碟/記憶體)
must-revalidate回應過期後**必須**驗證,不可使用過期內容
proxy-revalidate回應針對共用快取的 must-revalidate
immutable回應資源永不變更 (配合檔名雜湊),瀏覽器可跳過驗證
stale-while-revalidate=秒數回應允許在背景驗證時提供過期內容
stale-if-error=秒數回應源站錯誤時允許提供過期內容

Last-Modified 與 ETag 驗證流程

  1. 首次請求:伺服器回傳 200 OK + Last-Modified + ETag。
  2. 二次請求:瀏覽器送出 If-Modified-Since + If-None-Match。
  3. 伺服器比對:
    • 均未變更 → 304 Not Modified (無 Body,極快)。
    • 任一變更 → 200 OK + 新內容 + 新標記。

ETag 格式與配置

Apache 預設 FileETag INode MTime Size (Inode+修改時間+大小)。

  • 負載均衡多台伺服器: Inode 不同導致 ETag 不同 → 瀏覽器重複下載。
    解決:FileETag MTime Size (移除 Inode) 或 FileETag None (停用 ETag,依賴 Last-Modified)。
  • 動態頁面: 應用程式自行生成 ETag (如資料庫版本號/雜湊)。

瀏覽器快取行為觀察

  • Firefox/Chrome **永遠快取所有回應** (含過期、無標頭),除非 Cache-Control: no-store。
  • 過期但有 Last-Modified/ETag → 發送條件請求驗證。
  • 使用者按 F5/Ctrl+R → 發送 Cache-Control: max-age=0 強制驗證。
  • 使用者按 Ctrl+F5/Shift+F5 → 發送 Cache-Control: no-cache (繞過快取直接向源站)。

Apache 完整配置範例

# 載入模組
LoadModule expires_module modules/mod_expires.so
LoadModule headers_module modules/mod_headers.so

# 靜態資源長效快取 + immutable
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType image/gif "access plus 1 year"
    ExpiresByType image/webp "access plus 1 year"
    ExpiresByType font/woff "access plus 1 year"
    ExpiresByType font/woff2 "access plus 1 year"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
</IfModule>

# 針對長效快取資源添加 immutable
<IfModule mod_headers.c>
    <FilesMatch "\.(jpg|jpeg|png|gif|webp|woff|woff2|css|js)$">
        Header set Cache-Control "public, max-age=31536000, immutable"
    </FilesMatch>
    
    # 動態頁面不快取
    <FilesMatch "\.(php|jsp|asp|aspx)$">
        Header set Cache-Control "no-store, no-cache, must-revalidate, max-age=0"
        Header set Pragma "no-cache"
        Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
    </FilesMatch>
</IfModule>

# 多台負載均衡移除 Inode
FileETag MTime Size

總結

快取策略核心原則:靜態資源長效快取 + 檔名版本化 (指紋) + immutable;動態資源不快取或短效快取 + must-revalidate。正確配置可減少 80%+ 靜態請求,大幅降低延遲與頻寬成本。

Copyright © Windows教程網 All Rights Reserved