我尝试为 Apache 2.2.15 和 5.3.3 启用输出压缩,但 php 生成的网页并未压缩 :(
我的deflate.conf:
[user@host conf.d]# cat deflate.conf
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE application/x-httpd-php
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
DeflateCompressionLevel 9
# Browser specific settings
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
BrowserMatch \bOpera !no-gzip
# Setup custom deflate log
DeflateFilterNote Input instream
DeflateFilterNote Output outstream
DeflateFilterNote Ratio ratio
LogFormat '"%r" %{outstream}n/%{instream}n (%{ratio}n%%)' deflate
# Example of log file
CustomLog logs/deflate_log DEFLATE
vhost
以及用于日志记录的配置文件中的字符串:
CustomLog logs/vhost/deflate_log deflate
我在日志中看到的内容:
"GET /tag/%D0%BF%D0%BB%D1%83%D1%82%D0%BE%D0%BD/ HTTP/1.1" -/- (-%)
"GET /2011/02/28/%D1%88%D0%BA%D1%83%D1%80%D0%BA%D0%B8-%D1%81-%D0%B7%D0%BE%D0%BD%D0%B0%D0%BC%D0%B8-%D0%BF%D1%80%D0%BE%D0%B1%D0%B8%D1%82%D0%B8%D1%8F/ HTTP/1.1" -/- (-%)
"POST /wp-login.php HTTP/1.1" -/- (-%)
"GET /2011/04/08/%D0%BA%D0%B0%D0%BA-%D0%B7%D0%B0%D1%80%D0%B0%D0%B1%D0%B0%D1%82%D1%8B%D0%B2%D0%B0%D1%82%D1%8C-%D0%B1%D0%BE%D0%BB%D1%8C%D1%88%D0%B5-%D0%BA%D1%80%D0%B5%D0%B4%D0%B8%D1%82%D0%BE%D0%B2-%D0%B2-world-of-tanks/ HTTP/1.1" -/- (-%)
"GET /wp-content/themes/the-erudite/css/erudite.css HTTP/1.1" 4368/15392 (28%)
"GET /wp-content/plugins/wp-table-reloaded/css/plugin.css?ver=1.9.4 HTTP/1.1" 396/980 (40%)
"GET /wp-content/plugins/wp-downloadmanager/download-css.css?ver=1.50 HTTP/1.1" 457/1980 (23%)
"GET /wp-content/uploads/2012/03/first-120x240-3.gif HTTP/1.1" -/- (-%)
"GET /wp-content/plugins/loginza/img/vkontakte.png HTTP/1.1" -/- (-%)
"GET /wp-includes/js/comment-reply.min.js?ver=3.7.1 HTTP/1.1" 387/753 (51%)
"GET /wp-includes/js/jquery/jquery-migrate.min.js?ver=1.2.1 HTTP/1.1" 3050/7200 (42%)
"GET /wp-content/themes/the-erudite/js/common.js HTTP/1.1" 2570/6379 (40%)
"GET /wp-content/themes/the-erudite/js/jquery.scrollTo-min.js?ver=1.4.2 HTTP/1.1" 1181/2252 (52%)
"GET /wp-content/plugins/wp-recaptcha/recaptcha.css HTTP/1.1" 584/1739 (33%)
"GET /wp-includes/js/jquery/jquery.js?ver=1.10.2 HTTP/1.1" 32725/93085 (35%)
怎么了?
答案1
这个答案给出了 Apache 2.2 中可能出现的问题的最佳摘要mod_deflate
。解决方案是使用filter_module
&deflate_module
代替:
# Declare a "gzip" filter, it should run after all internal filters like PHP or SSI
FilterDeclare gzip CONTENT_SET
# "gzip" filter can change "Content-Length", can not be used with range requests
FilterProtocol gzip change=yes;byteranges=no
# Enable "gzip" filter if "Content-Type" contains "text/html", "text/css" etc.
FilterProvider gzip DEFLATE resp=Content-Type $text/html
FilterProvider gzip DEFLATE resp=Content-Type $text/css
FilterProvider gzip DEFLATE resp=Content-Type $text/javascript
FilterProvider gzip DEFLATE resp=Content-Type $application/javascript
FilterProvider gzip DEFLATE resp=Content-Type $application/x-javascript
# Add "gzip" filter to the chain of filters
FilterChain gzip