
我已將以下內容放入我的 http.conf 檔案中:
# mod_deflate configuration
<IfModule mod_deflate.c>
# Restrict compression to these MIME types
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/css
# Level of compression (Highest 9 - Lowest 1)
DeflateCompressionLevel 9
# Netscape 4.x has some problems.
BrowserMatch ^Mozilla/4 gzip-only-text/html
# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4\.0[678] no-gzip
# MSIE masquerades as Netscape, but it is fine
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
<IfModule mod_headers.c>
# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary
</IfModule>
</IfModule>
我的內容不會以 gzip 類型的內容編碼返回,但我發現自己收到了更多的 304,並且 Etag 附加了 +gzip 後綴。 mod_deflate 真的在做它的工作嗎? (抱歉,n00b-ishness)
答案1
標頭告訴您什麼,如果它沒有返回“content-encoding:gzip”,它可能不起作用..您可以按如下方式測試:
curl -I -H 'Accept-Encoding: gzip,deflate' http://yoursite.com/somefile
答案2
阿帕奇文檔依類型新增輸出過濾器表示該指令在 Apache httpd 2.1 及更高版本中已棄用,並且如果 Apache 無法確定 mime 類型,則該指令並不總是能正常運作。
我建議透過使用類似以下內容作為起點來啟用壓縮,然後添加所有瀏覽器調整和壓縮等級。
<Location />
SetOutputFilter DEFLATE
# Don't compress images
SetEnvIfNoCase Request_URI \.(?:gif|png|jpg|jpeg)$ no-gzip dont-vary
Header append Vary User-Agent env=!dont-vary
</Location>
使用 Michael Steinfeld 提到的 cURL 進行驗證。
答案3
無法添加評論,因為我沒有足夠的聲譽,因此我寫此作為答案。
Michael Steinfeld 的原始答案輸出以下錯誤(在 Windows 命令提示字元中)
curl: (6) Could not resolve host: gzip,deflate'
我透過刪除冒號後面的空格解決了這個問題:
curl -I -H 'Accept-Encoding:gzip,deflate' http://yoursite.com/somefile
答案4
和LogLevel
設定為debug
,mod_deflate 會將其偵錯訊息記錄到ErrorLog
。正在壓縮的每個資源都會在那裡註銷。您可以查看 mod_deflate 是否即時運作
tail -f /path/to/error-log | grep deflate
例如
[2015 年4 月22 日星期三05:37:49.742615] [deflate:debug] [pid 22627] mod_deflate.c(849): [client 127.0.0.1:55667] AH01384: Zlib: 壓縮至 1067 壓縮到 URL 1384: Zlib /js/ common.js,引用:http://localhost/index.php