Apache使用mod_deflate模块启用gzip功能

时间:2022-07-13 22:26:47

Apache使用mod_deflate模块启用gzip功能

检查你的网站是否启用了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):[plain] view plain copy Apache使用mod_deflate模块启用gzip功能Apache使用mod_deflate模块启用gzip功能
  1. <IfModule mod_mime.c>  
  2.  AddType application/x-javascript .js  
  3.  AddType text/css .css  
  4. </IfModule>  
  5. <IfModule mod_deflate.c>  
  6.  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  
  7.  <IfModule mod_setenvif.c>  
  8.   BrowserMatch ^Mozilla/4 gzip-only-text/html  
  9.   BrowserMatch ^Mozilla/4\.0[678] no-gzip  
  10.   BrowserMatch \bMSIE !no-gzip !gzip-only-text/html  
  11.  </IfModule>  
  12.  <IfModule mod_headers.c>  
  13.   Header append Vary User-Agent env=!dont-vary  
  14.  </IfModule>  
  15. </IfModule>  

然后重启apache:
service restart httpd
再用浏览器发送一个请求,查看Response header部分,如配置正确,应该如下所示:
Apache使用mod_deflate模块启用gzip功能
通过gzip,通常可以把文本文件(如html/js/css)大小压缩到1/3到1/5左右,无疑会大大提高网站性能。
另外一个直观的方法是看请求返回文件大小和内容,如下图肯定是被压缩过了:Apache使用mod_deflate模块启用gzip功能
转自:http://blog.csdn.net/iefreer/article/details/22766025