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

72 篇文章 0 订阅
40 篇文章 0 订阅

检查你的网站是否启用了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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值