您的位置:首页 > 运维架构 > Apache

Apache: 使用mod_deflate模块启用gzip功能

2014-04-02 01:41 701 查看
检查你的网站是否启用了gzip,用chrome打开网站比如http://www.techbrood.com
选中一个请求,查看Response header部分,如果没有包含
Content-Encoding:gzip
那么你还没有开启gzip网页压缩功能。
1.首先检查是否已安装mod_deflate:# /usr/local/apache2/bin/apachectl -t -D DUMP_MODULES
Loaded Modules:
...
deflate_module (static)
...
Syntax OK

如果没有deflate模块,则需要重新编译apache2.首先查看你apache2安装路径build目录下的config.nice文件,其中有原来的configure(编译选项)然后添加 ./configure ...... --enable-deflate,重新make && make install。重启apache。
2.在配置中开启gzip在httpd配置文件中添加如下语句(注意,这里使用了apache2里面的mod_deflate而不是1.3里面的mod_gzip):
<IfModule mod_mime.c>
 AddType application/x-javascript .js
 AddType text/css .css
</IfModule>
<IfModule mod_deflate.c>
 AddOutputFilterByType DEFLATE text/css application/x-javascript text/x-component text/html text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon application/javascript
 <IfModule mod_setenvif.c>
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4\.0[678] no-gzip
  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
 </IfModule>
 <IfModule mod_headers.c>
  Header append Vary User-Agent env=!dont-vary
 </IfModule>
</IfModule>

然后重启apache:service restart httpd
再用浏览器发送一个请求,查看Response header部分,如配置正确,应该如下所示:



通过gzip,通常可以把文本文件(如html/js/css)大小压缩到1/3到1/5左右,无疑会大大提高网站性能。
另外一个直观的方法是看请求返回文件大小和内容,如下图肯定是被压缩过了:


by iefreer
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: