Apache-2.2
Apache gzip 壓縮
我有一個在 Ubuntu 上執行的繼承的 Apache 2.2.17 伺服器,我需要啟用壓縮。我已確保載入了 mod:
apachectl -t -D DUMP_MODULES Loaded Modules: ... deflate_module (shared) headers_module (shared) setenvif_module (shared) ...
然後我設置
http.conf
了以下規則:<IfModule deflate_module> SetOutputFilter DEFLATE <IfModule setenvif_module> # 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 \bMSIE !no-gzip !gzip-only-text/html # NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48, the above regex won’t work. You can use the following # workaround (comment the above line and uncomment the below line) to get the desired effect: # BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html # Don’t compress already-compressed files SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary SetEnvIfNoCase Request_URI .(?:avi|mov|mp3|mp4|rm|flv|swf|mp?g)$ no-gzip dont-vary SetEnvIfNoCase Request_URI .pdf$ no-gzip dont-vary </IfModule> <IfModule headers_module> # Make sure proxies don’t deliver the wrong content Header append Vary User-Agent env=!dont-vary </IfModule> </IfModule>
現在,如果我檢索
HEAD
using python 或 curl 我得到一個 gzip 響應(這只是一個.txt
文件,但在整個站點上是一致的):Date: Mon, 06 May 2013 11:53:31 GMT Server: Apache/2.2.17 (Ubuntu) Last-Modified: Sun, 05 May 2013 12:37:23 GMT ETag: "2412002-129c6-4dbf7d88766c0" Accept-Ranges: bytes Vary: Accept-Encoding,User-Agent Content-Encoding: gzip Content-Length: 32607 Content-Type: text/plain
但是,如果我執行
POST
orGET
,我不會得到 gzip :Date: Mon, 06 May 2013 11:53:25 GMT Server: Apache/2.2.17 (Ubuntu) Last-Modified: Sun, 05 May 2013 12:37:23 GMT ETag: "2412002-129c6-4dbf7d88766c0" Accept-Ranges: bytes Vary: Accept-Encoding,User-Agent Content-Type: text/plain Content-Length: 76230
更新: 這是來自
Firefox 20
. Firebug 和 Google 確認正在請求 gzip:Date: Tue, 07 May 2013 16:57:01 GMT Server: Apache/2.2.17 (Ubuntu) X-Powered-By: PHP/5.3.5-1ubuntu7.10 Expires: Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache Vary: Accept-Encoding,User-Agent Keep-Alive: timeout=15, max=98 Connection: Keep-Alive Content-Type: text/html Content-Length: 19922
這幾天讓我感到沮喪,任何幫助將不勝感激。
謝謝
事實證明,配置並沒有真正執行得很好,並且該站點總是選擇 deflate 而不是 gzip。因此,我沒有看到它工作。我簡化了我的
http.conf
,並啟用了 linux 上的日誌,這表明它實際上正在工作:LoadModule deflate_module /usr/lib/apache2/modules/mod_deflate.so SetOutputFilter DEFLATE SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ \ no-gzip dont-vary SetEnvIfNoCase Request_URI \ \.(?:exe|t?gz|zip|bz2|sit|rar)$ \ no-gzip dont-vary SetEnvIfNoCase Request_URI \.pdf$ no-gzip dont-vary BrowserMatch ^Mozilla/4 gzip-only-text/html BrowserMatch ^Mozilla/4\.0[678] no-gzip BrowserMatch \bMSIE !no-gzip !gzip-only-text/html DeflateFilterNote Input input_info DeflateFilterNote Output output_info DeflateFilterNote Ratio ratio_info LogFormat '"%r" %{output_info}n/%{input_info}n (%{ratio_info}n%%)' deflate CustomLog /var/log/apache2/deflate_log deflate
最後一節寫了一個日誌,顯示某些內容實際上正在壓縮。其中一些仍然沒有,但我相信現在這取決於 php 的執行方式。我將考慮使用 php 壓縮來更好地處理它。
感謝 Marcel 和 Chris S 幫助我解決這個問題。