萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> PHP 程式設計

Apache 靜態資源緩存配置指南:mod_expires 模塊詳解與最佳實踐

啟用 Apache mod_expires 實現瀏覽器端靜態資源緩存

通過配置 mod_expires 模塊,可為靜態資源(CSS、JS、圖片、字體等)設置 Expires 與 Cache-Control 響應頭,減少重複請求,顯著提升頁面加載速度並降低服務器帶寬壓力。

步驟一:啟用模塊

編輯 httpd.conf(或 apache2.conf),取消註釋以下行:

LoadModule expires_module modules/mod_expires.so

重啟 Apache 生效:systemctl restart httpd 或 apachectl restart。

步驟二:配置緩存策略(推薦放在 VirtualHost 或 .htaccess)

<IfModule mod_expires.c>
    ExpiresActive On

    # 默認緩存 10 天(適用於未匹配類型的資源)
    ExpiresDefault "access plus 10 days"

    # 文本類資源:14 天
    ExpiresByType text/css          "access plus 14 days"
    ExpiresByType text/javascript   "access plus 14 days"
    ExpiresByType application/javascript "access plus 14 days"
    ExpiresByType application/json  "access plus 14 days"
    ExpiresByType application/xml   "access plus 14 days"
    ExpiresByType text/xml          "access plus 14 days"
    ExpiresByType text/html         "access plus 14 days"  # HTML 謹慎設置,建議 0 或極短

    # 圖片類:30 天(極少變動)
    ExpiresByType image/jpeg        "access plus 30 days"
    ExpiresByType image/png         "access plus 30 days"
    ExpiresByType image/gif         "access plus 30 days"
    ExpiresByType image/webp        "access plus 30 days"
    ExpiresByType image/svg+xml     "access plus 30 days"
    ExpiresByType image/x-icon      "access plus 30 days"  # favicon.ico

    # 字體文件:1 年(幾乎不變)
    ExpiresByType font/woff         "access plus 1 year"
    ExpiresByType font/woff2        "access plus 1 year"
    ExpiresByType application/font-woff  "access plus 1 year"
    ExpiresByType application/x-font-ttf "access plus 1 year"

    # 視頻/音頻/文檔:30 天
    ExpiresByType video/mp4         "access plus 30 days"
    ExpiresByType audio/mpeg        "access plus 30 days"
    ExpiresByType application/pdf   "access plus 30 days"

    # 針對特定擴展名強制設置(防 MIME 類型識別錯誤)
    <FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|jpg|jpeg|png|gif|webp|woff|woff2|ttf|svg)$">
        ExpiresDefault "access plus 30 days"
        Header append Cache-Control "public"
    </FilesMatch>

    # 動態腳本禁用緩存(關鍵!)
    <FilesMatch "\.(php|jsp|asp|aspx|py|pl)$">
        ExpiresActive Off
        Header set Cache-Control "private, no-cache, no-store, proxy-revalidate, no-transform"
        Header set Pragma "no-cache"
    </FilesMatch>
</IfModule>

關鍵指令解析

指令說明示例
ExpiresActive On|Off啟用/禁用模塊ExpiresActive On
ExpiresDefault默認過期時間(相對訪問時間/修改時間)ExpiresDefault "access plus 10 days"
ExpiresByType MIME-type針對特定 MIME 類型設置過期時間ExpiresByType text/css "access plus 14 days"
時間語法access(訪問時間)或 modification(文件修改時間)+ plus + 數值 + years|months|weeks|days|hours|minutes|seconds"access plus 1 year"

配合 mod_headers 強化緩存控制

<IfModule mod_headers.c>
    # 靜態資源:公共緩存,允許 CDN/代理緩存
    <FilesMatch "\.(css|js|jpg|jpeg|png|gif|webp|woff|woff2|ttf|svg|ico)$">
        Header set Cache-Control "max-age=2592000, public, immutable"
    </FilesMatch>

    # HTML:私有緩存,必須重新驗證
    <FilesMatch "\.html$">
        Header set Cache-Control "max-age=600, private, must-revalidate"
    </FilesMatch>

    # 移除 ETag(分佈式部署時避免 Inode 差異導致緩存失效)
    Header unset ETag
    FileETag None
</IfModule>

驗證配置是否生效

  1. 瀏覽器開發者工具 → Network → 選中靜態資源 → Headers,檢查 Expires、Cache-Control。
  2. 命令行:curl -I https://example.com/style.css 查看響應頭。
  3. 在線工具:RED Bot、KeyCDN Cache Checker。

常見問題與避坑

  • HTML 緩存過久:導致部署更新用戶看不到新版本。建議 HTML 設置短緩存(max-age=600)或不緩存,靜態資源文件名帶 Hash(如 app.a1b2c3.js)實現長緩存+精準失效。
  • 動態接口被緩存:確保 FilesMatch 正確匹配動態腳本後綴並關閉緩存。
  • HTTPS 下緩存失效:確保 Cache-Control: public 設置,否則某些代理/瀏覽器不緩存 HTTPS 內容。
  • CDN 回源緩存:源站設置長緩存,CDN 邊緣節點再按業務設置二級緩存策略。

Nginx 等效配置對照

# Nginx location 塊中
location ~* \.(css|js|jpg|jpeg|png|gif|webp|woff|woff2|ttf|svg|ico)$ {
    expires 30d;
    add_header Cache-Control "public, immutable";
    access_log off;
}
location ~* \.html$ {
    expires 10m;
    add_header Cache-Control "private, must-revalidate";
}

合理配置 Apache 靜態資源緩存,配合文件名哈希版本控制,是前端性能優化投入產出比最高的手段之一。

copyright © 萬盛學電腦網 all rights reserved