配置Apache2启用deflate压缩加快传输

今天下载了Yahoo的给Firefox开发的YSlow插件,这个插件确实不错,能够给出不少优化建议,发现所在主机没有开启deflate压缩。

首先加载mod_deflate。

在httpd.conf中加入

<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|exe|bmp|mp3|rar|zip|swf|cab|t?gz|bz2|sit)$ no-gzip dont-vary

SetEnvIfNoCase Request_URI .pdf$ no-gzip dont-vary

Make sure proxies don’t deliver the wrong content

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

</Location>
再重启apache即可,/etc/init.d/apache2 restart

经过port80software的在线检测,首页大小从91977 bytes压缩到13376 bytes,传输速度提高6.8X,传输速度从原来12.832 s缩减到1.866 s。

如果修改配置之后提示“Invalid command ‘Header’, perhaps mis-spelled or defined by a module not included in the server configuration”,则加载Header模块即可,不过最好还是在编译apache模块时直接加上–enable-deflate –enable-headers就省事多了。