通過配置 mod_expires 模塊,可為靜態資源(CSS、JS、圖片、字體等)設置 Expires 與 Cache-Control 響應頭,減少重複請求,顯著提升頁面加載速度並降低服務器帶寬壓力。
編輯 httpd.conf(或 apache2.conf),取消註釋以下行:
LoadModule expires_module modules/mod_expires.so
重啟 Apache 生效:systemctl restart httpd 或 apachectl restart。
<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" |
<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>
Expires、Cache-Control。curl -I https://example.com/style.css 查看響應頭。max-age=600)或不緩存,靜態資源文件名帶 Hash(如 app.a1b2c3.js)實現長緩存+精準失效。FilesMatch 正確匹配動態腳本後綴並關閉緩存。Cache-Control: public 設置,否則某些代理/瀏覽器不緩存 HTTPS 內容。# 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 靜態資源緩存,配合文件名哈希版本控制,是前端性能優化投入產出比最高的手段之一。