Apache 的 mod_expires 模組能有效減少約 10% 的重複請求,讓回訪用戶直接從本地快取讀取指定頁面資源,無需再向伺服器發送請求,大幅降低伺服器負載與頻寬消耗。本文將詳細介紹如何啟用該模組、配置快取策略,以及結合 mod_headers 實現更精細的快取控制。
若您自行安裝 Apache 架設網站主機,需編輯 httpd.conf 設定檔。搜尋以下行並移除行首的 # 註解符號:
LoadModule expires_module modules/mod_expires.so
儲存後重新啟動 Apache 使變更生效。
租用虛擬主機時通常無法存取 httpd.conf,此時可在網站根目錄建立 .htaccess 設定檔,配置相對靈活。mod_expires 的設定同樣支援寫在 .htaccess 中。
瀏覽器瀏覽網頁時會將資源快取於本地,下次訪問同一頁面可直接讀取快取,達到加速效果。mod_expires 透過設定網頁文件的過期時間(Expires Header),延長資源在瀏覽器快取中的保存期限。只要未過期,瀏覽器便直接使用快取資料,無需重新下載,同時也能減少流量消耗——對有流量限制的虛擬主機尤為重要。
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 10 days"
ExpiresByType text/css "access plus 1 second"
</IfModule>
ExpiresActive On:啟用模組功能。ExpiresDefault:設定預設過期時間,"access plus 10 days" 表示從訪問時間起算 10 天。ExpiresByType:針對特定 MIME 類型設定過期時間,此例中 CSS 僅快取 1 秒(適合開發調試)。文字敘述型時間格式:"access plus 1 month 15 days 2 hours",支援 years、months、weeks、days、hours、minutes、seconds。起算基準有三種:access(訪問時間,同 now)、modification(檔案最後修改時間)。
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault A86400
ExpiresByType image/x-icon A2592000
ExpiresByType application/x-javascript A2592000
ExpiresByType text/css A2592000
ExpiresByType image/gif A604800
ExpiresByType image/png A604800
ExpiresByType image/jpeg A604800
ExpiresByType text/plain A604800
ExpiresByType application/x-shockwave-flash A604800
ExpiresByType video/x-flv A604800
ExpiresByType application/pdf A604800
ExpiresByType text/html A900
</IfModule>
A86400:A 代表 access(訪問時間起算),後接秒數。86400 秒 = 1 天。M 代碼代表 modification(檔案修改時間起算),適合頻繁更新的 HTML 頁面。<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault A0
# 1 年
<FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$">
ExpiresDefault A9030400
</FilesMatch>
# 1 週
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
ExpiresDefault A604800
</FilesMatch>
# 3 小時
<FilesMatch "\.(txt|xml|js|css)$">
ExpiresDefault A10800
</FilesMatch>
</IfModule>
使用 <FilesMatch> 依副檔名正則分組,比 ExpiresByType 更直觀彈性,且不依賴 MIME 類型映射準確性。
YSlow、PageSpeed 等工具常提示「Add an Expires header」得分低。瀏覽器快取機制取決於四個 Header:Cache-Control、Expires、Last-Modified、ETag。若伺服器不支援 mod_expires,可改用 mod_headers 直接設定 Cache-Control: max-age。
<IfModule mod_headers.c>
# HTML、文字類:1 小時
<FilesMatch "\.(html|htm|txt)$">
Header set Cache-Control "max-age=3600"
</FilesMatch>
# CSS、JS、SWF:1 週
<FilesMatch "\.(css|js|swf)$">
Header set Cache-Control "max-age=604800"
</FilesMatch>
# 圖片、PDF、影片等:1 年
<FilesMatch "\.(ico|gif|jpg|jpeg|png|flv|pdf)$">
Header set Cache-Control "max-age=29030400"
</FilesMatch>
</IfModule>
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
<FilesMatch "\.(xml|txt)$">
Header set Cache-Control "max-age=18000, public, must-revalidate"
</FilesMatch>
<FilesMatch "\.(html|htm|php)$">
Header set Cache-Control "max-age=3600, must-revalidate"
</FilesMatch>
<IfModule mod_expires.c>...</IfModule>:防止模組未啟用時導致配置錯誤;若確定已啟用可省略。ExpiresActive On|Off:啟用/關閉功能。ExpiresDefault:全域預設過期時間。ExpiresByType MIME-type "time":按 MIME 類型設定。<FilesMatch regex>...</FilesMatch>:按檔案名稱正則設定。| 時間長度 | 秒數 | 代碼範例 |
|---|---|---|
| 1 分鐘 | 60 | A60 / M60 |
| 1 小時 | 3,600 | A3600 |
| 1 天 | 86,400 | A86400 |
| 1 週 | 604,800 | A604800 |
| 30 天 | 2,592,000 | A2592000 |
| 1 年 | 31,536,000 | A31536000 |
善用 mod_expires 與 mod_headers,能大幅提升網站載入速度、降低伺服器負載與流量成本,是 Apache 效能優化的基石配置之一。