| 標頭 | 分類 | HTTP 版本 | 作用 | 優先級 |
|---|---|---|---|---|
Expires | 過期控制 | 1.0 | 絕對過期時間 (GMT) | 低 |
Cache-Control | 過期控制 | 1.1 | 相對秒數、指令集 | 高 (覆蓋 Expires) |
Last-Modified | 驗證標記 | 1.0/1.1 | 最後修改時間 (秒精度) | 弱驗證 |
ETag | 驗證標記 | 1.1 | 實體指紋 (強驗證) | 強驗證 |
Expires: Wed, 21 Oct 2025 07:28:00 GMTmod_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" # 動態頁不快取
Expires: Wed, 11 Jan 1984 05:00:00 GMT (永遠過期)。Expires,會根據 Last-Modified 推算啟發式過期時間 (通常為距今 10% 時間差)。| 指令 | 方向 | 說明 |
|---|---|---|
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=秒數 | 回應 | 源站錯誤時允許提供過期內容 |
200 OK + Last-Modified + ETag。If-Modified-Since + If-None-Match。304 Not Modified (無 Body,極快)。200 OK + 新內容 + 新標記。Apache 預設 FileETag INode MTime Size (Inode+修改時間+大小)。
FileETag MTime Size (移除 Inode) 或 FileETag None (停用 ETag,依賴 Last-Modified)。Cache-Control: no-store。Last-Modified/ETag → 發送條件請求驗證。Cache-Control: max-age=0 強制驗證。Cache-Control: no-cache (繞過快取直接向源站)。# 載入模組
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%+ 靜態請求,大幅降低延遲與頻寬成本。