我有一个继承的 Apache 2.2.17 服务器在 Ubuntu 上运行,我需要启用压缩。我已确保模块已加载:
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>
现在,如果我使用 python 或 curl 检索,HEAD
我会得到一个 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
或GET
,则不会得到 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
这件事已经困扰我好几天了,如果能得到任何帮助我都会非常感激。
谢谢
答案1
事实证明,配置运行得并不好,网站总是选择 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 帮助我解决这个问题。