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

Apache 啟用 mod_expires 模組優化網站快取效能完整指南

編輯:關於伺服器

Apache 的 mod_expires 模組能有效減少約 10% 的重複請求,讓回訪用戶直接從本地快取讀取指定頁面資源,無需再向伺服器發送請求,大幅降低伺服器負載與頻寬消耗。本文將詳細介紹如何啟用該模組、配置快取策略,以及結合 mod_headers 實現更精細的快取控制。

一、確認並啟用 mod_expires 模組

1. 自建 Apache 伺服器(編輯 httpd.conf)

若您自行安裝 Apache 架設網站主機,需編輯 httpd.conf 設定檔。搜尋以下行並移除行首的 # 註解符號:

LoadModule expires_module modules/mod_expires.so

儲存後重新啟動 Apache 使變更生效。

2. 虛擬主機環境(使用 .htaccess)

租用虛擬主機時通常無法存取 httpd.conf,此時可在網站根目錄建立 .htaccess 設定檔,配置相對靈活。mod_expires 的設定同樣支援寫在 .htaccess 中。

二、瀏覽器快取機制與 mod_expires 原理

瀏覽器瀏覽網頁時會將資源快取於本地,下次訪問同一頁面可直接讀取快取,達到加速效果。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 頁面。
  • 圖片、CSS、JS 等靜態資源設定較長快取(7~30 天),HTML 設定較短(15 分鐘)。

範例三:搭配 FilesMatch 依副檔名分組設定

<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 類型映射準確性。

四、進階:結合 mod_headers 實現 Cache-Control

YSlow、PageSpeed 等工具常提示「Add an Expires header」得分低。瀏覽器快取機制取決於四個 Header:Cache-Control、Expires、Last-Modified、ETag。若伺服器不支援 mod_expires,可改用 mod_headers 直接設定 Cache-Control: max-age。

mod_headers 配置範例(.htaccess)

<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 分鐘60A60 / M60
1 小時3,600A3600
1 天86,400A86400
1 週604,800A604800
30 天2,592,000A2592000
1 年31,536,000A31536000

善用 mod_expires 與 mod_headers,能大幅提升網站載入速度、降低伺服器負載與流量成本,是 Apache 效能優化的基石配置之一。

Copyright © Windows教程網 All Rights Reserved