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

網站前端效能優化實戰:利用「內容過期」與快取機制大幅降低請求數

編輯:關於伺服器

背景與目標

依據 Yahoo! 34 條黃金法則與 YSlow 13 項評分規則,第 3 條「Add an Expires header(新增 Expires 標頭)」 是最容易實施且效果立竿見影的優化手段。透過設定靜態資源的快取過期時間,讓瀏覽器二次訪問時直接從本地快取讀取,減少 HTTP 請求與頻寬消耗。

快取控制標頭對比

標頭HTTP 版本用途優先級
Expires1.0指定絕對過期時間(GMT 格式)較低
Cache-Control: max-age=秒數1.1指定相對過期秒數,支援 public/private/no-cache 等指令較高(覆蓋 Expires)
Last-Modified1.0/1.1資源最後修改時間,配合 If-Modified-Since 驗證驗證用
ETag1.1實體標籤(檔案指紋),強驗證機制,解決 1 秒內多次修改問題驗證用

資源分類與建議過期策略

資源類型建議過期理由
圖片、Favicon、字體、影音1 年或更久(max-age=31536000)極少變動,檔名可帶雜湊值做版本控制
CSS、JavaScript1 個月~1 年(配合檔名版本號)部署時更新檔名(如 app.v2.css)即可強制刷新
HTML 動態頁面不快取或極短(max-age=0, must-revalidate)內容頻繁變動,避免顯示舊資料
API 回應視業務而定(通常 no-store)資料即時性要求高

Web 伺服器配置範例

Apache (mod_expires)

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

# 啟用並設定預設規則
ExpiresActive On
ExpiresDefault "access plus 1 month"

# 依類型細分
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/webp "access plus 1 year"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType application/json "access plus 1 month"
ExpiresByType font/woff "access plus 1 year"
ExpiresByType font/woff2 "access plus 1 year"

# 動態內容不快取
<FilesMatch "\.(php|jsp|asp|aspx)$">
    ExpiresActive Off
    Header set Cache-Control "no-store, no-cache, must-revalidate, max-age=0"
    Header set Pragma "no-cache"
</FilesMatch>

Nginx

location ~* \.(jpg|jpeg|png|gif|ico|webp|svg|woff|woff2)$ {
    expires 1y;
    add_header Cache-Control "public, immutable";
}

location ~* \.(css|js)$ {
    expires 30d;
    add_header Cache-Control "public";
}

# 動態頁面
location ~ \.php$ {
    expires -1;
    add_header Cache-Control "no-store, no-cache, must-revalidate";
}

IIS (網站 → HTTP 回應標頭 → 設定常用標頭)

IIS 設定內容過期介面

  • 勾選「啟用內容過期」
  • 選擇「在此時間段後過期」,輸入天數/小時
  • 或針對特定資料夾/檔案類型單獨設定

實測效果對比(HttpWatch / YSlow)

首次訪問 YSlow 首次

二次訪問 YSlow 二次

指標首次訪問二次訪問(啟用過期)二次訪問(未啟用)
HTTP 請求數499 (Result=Cache)49 (含 304 Not Modified)
傳輸位元組完整下載近 0 (從快取讀取)僅標頭交換
頁面載入時間基準大幅降低(秒開體驗)略快於首次

關鍵觀察

  • 啟用 Expires/Cache-Control 後,二次訪問零請求、零流量,瀏覽器直接讀取快取(HttpWatch 顯示 Result: Cache)。
  • 未啟用時,雖瀏覽器發送 If-Modified-Since/If-None-Match 獲取 304 Not Modified,但請求數未減少,仍有 RTT 延遲。
  • 使用者按 F5 強制重新整理 時,瀏覽器會忽略快取重新請求(發送 Cache-Control: max-age=0)。

程式碼層面補充(ASP.NET / PHP 等)

// ASP.NET
Response.Cache.SetExpires(DateTime.Now.AddDays(30));
Response.Cache.SetCacheability(HttpCacheability.Public);

// PHP
header("Cache-Control: public, max-age=2592000"); // 30 天
header("Expires: " . gmdate("D, d M Y H:i:s", time() + 2592000) . " GMT");

總結

正確設定「內容過期」是成本最低、效果最即時的前端優化。搭配檔名版本控制(指紋),即可同時兼顧「長效快取」與「精準更新」。建議所有靜態資源皆納入策略,動態頁面視業務謹慎設定。

Copyright © Windows教程網 All Rights Reserved