Apache 开启 GZip 压缩

我这里用的是 xampp 1.7.0

打开 xampp\apache\conf\httpd.conf 文件

搜索 deflate_module modules/mod_deflate.so

把 LoadModule deflate_module modules/mod_deflate.so 前面的 # 号给去掉 , 注意 不要在 deflate.so 后面加 #remark info , 会不能启动 apache

在 httpd.conf 文件的最后 , 加入如下代码 :

<IfModule mod_deflate.c>

<Location />

# Insert filter

SetOutputFilter DEFLATE

# 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 to get the desired effect:

BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

# Don't compress images

SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary

# Make sure proxies don't deliver the wrong content

Header append Vary User-Agent env=!dont-vary

</Location>

</IfModule>

重启 apache 即可启动 gzip 压缩 。

测试 : 访问本机的 localhost/test/gzip.php 页面 , 用火狐浏览器的 freebug 查看请求 url 的返回信息 , 在 "响应头信息" 里有 Content-Encoding : gzip deflate 表明已经成功启用 GZIP 压缩 , 我的这个测试页面 600b 左右 , 压缩后是 470b 左右

关于 apache gzip 的其他信息 : http://httpd.apache.org/docs/2.0/mod/mod_deflate.html