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

Apache 啟用 Gzip 與 Expires 快取優化網頁載入速度

編輯:關於伺服器

啟用模組

編輯 Apache 設定檔 (httpd.conf),移除下列兩行前的註解 #,重啟 Apache:

LoadModule deflate_module modules/mod_deflate.so
LoadModule expires_module modules/mod_expires.so

建立 .htaccess 設定壓縮與快取

在網站根目錄建立 .htaccess,加入以下內容:

1. 快取設定 (mod_expires)

<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/gif A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType image/png A2592000
ExpiresByType image/x-icon A2592000
ExpiresByType application/x-javascript A604800
ExpiresByType text/css A604800
</IfModule>

ExpiresByType 依 MIME 類型設定快取時間,A 代表存取後秒數 (如 A2592000 = 30 天)。

2. Gzip 壓縮設定 (mod_deflate)

<IfModule mod_deflate.c>
SetOutputFilter DEFLATE
AddOutputFilterByType DEFLATE text/html text/css image/gif image/jpeg image/png application/x-javascript
</IfModule>

對 HTML、CSS、圖片、JavaScript 等 MIME 類型啟用 DEFLATE (gzip) 壓縮。

關閉 ETag (多伺服器環境建議)

ETag 依伺服器唯一屬性產生,叢集環境下會導致驗證失效。在設定檔加入:

FileETag none

若為負載平衡多伺服器,可設為 FileETag MTime Size (預設含 INode),移除 INode 避免跨伺服器不一致。

總結

完成上述設定並重啟 Apache,即可同時擁有 Gzip 壓縮傳輸與客戶端快取機制,大幅降低頻寬消耗並加速頁面載入。

Copyright © Windows教程網 All Rights Reserved