我把以下内容放在我的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>
我的内容不会返回一个types为gzip的Content-Encoding,但是我发现自己获得了更多的304s,而Etag附加了+ gzip后缀。 mod_deflate是否真的在做它的工作? (关于n00b-ishness的抱歉)
什么标题告诉你,如果它不是返回“content-encoding:gzip”它可能不工作..你可以testing如下:
curl -I -H 'Accept-Encoding: gzip,deflate' http://yoursite.com/somefile
AddOutputFilterByType的apache文档表明这个指令在Apache httpd 2.1及更高版本中被弃用,如果Apache无法确定MIMEtypes,它并不总是奏效。
我build议使用类似于以下内容的方式来启用压缩,然后将所有浏览器调整和压缩级别添加回来。显然,您可能要仔细检查httpd.conf以确保它实际上也加载了mod_deflate.so :
<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来validation。
LogLevel设置为debug ,mod_deflate会将其debugging消息logging到ErrorLog 。 每个被压缩的资源都会被注销。 你可以看到,如果mod_deflate实时工作
tail -f /path/to/error-log | grep deflate
例如
[Wed Apr 22 05:37:49.742615 2015] [deflate:debug] [pid 22627] mod_deflate.c(849):[client 127.0.0.1:55667] AH01384:Zlib:Compressed 10700 to 2637:URL / web / js / common.js,referer: http://localhost/index.php