啟用 Gzip 可減少文字型資源的傳輸量,例如 HTML、CSS、JavaScript 與 XML,進而提升頁面載入速度。
在 Linux 下,可編輯 /etc/httpd/conf/httpd.conf,找到 deflate_module 與 headers_module,並移除前方註解符號。
在設定檔底部加入 IfModule 區段,指定要壓縮的內容類型,並排除圖片、壓縮檔與部分文件格式。
SetOutputFilter DEFLATE SetEnvIfNoCase Request_URI gif no-gzip dont-vary SetEnvIfNoCase Request_URI jpg no-gzip dont-vary SetEnvIfNoCase Request_URI png no-gzip dont-vary AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css AddOutputFilterByType DEFLATE application/x-javascript
完成設定後,重新啟動 Apache:
service httpd restart
之後可用瀏覽器開發者工具或 curl 檢查回應標頭,確認 Content-Encoding 是否出現 gzip。