apache和tomcat下开启和检验gzip输出

1、tomcat下开启gzip压缩

     修改server.xml,在Connector节点增加属性

     compression="on" compressionMinSize="2048"
     compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain"

 <Connector port="8084" protocol="HTTP/1.1"  executor="tomcatThreadPool"
               connectionTimeout="20000"  redirectPort="8443" useBodyEncodingForURI="true"
               minProcessors="1" maxProcessors="5" enableLookups="false" acceptCount="50"
               compression="on" compressionMinSize="2048"
               compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain"
               debug="0" URIEncoding="UTF-8"/>

compression:on启用压缩, off关闭压缩,forces强制使用压缩,默认值是off。

compressableMimeType:使用HTTP压缩的MIME类型,使用逗号分割,默认值是text/html,text/xml,text/plain

compressionMinSize:启用压缩的输出内容大小,这里面默认为 2KB

noCompressionUserAgents: 不启用压缩的浏览器,默认为空

 

图片的mime类型为:image/gif,image/jpg

tomcat有没有开启gzip输出,压缩率如何可通过访问下面站点检验

http://www.gidnetwork.com/tools/gzip-test.php


2、apache下开启gzip压缩

     apache内置有mod_deflate模块来启用gzip功能,初次安装加上以下参数:

 ./configure --enable-deflate --enable-headers

    但假如安装apache的时候没有编译相关模块,就需要你手动安装一次,以启用它:

 

  1. 首先到你的apache源码目录,查找到mod_deflate.c文件
    Mac及Linux下都可用locate mod_deflate.c
    通常位置:apachehttpd源码目录/modules/filters/mod_deflate.c
     
  2. 进入上面找到的目录运行下面的命令:
    /usr/local/apache/bin/apxs -i -c -a mod_deflate.c
    注:apxs目录请参照您自己的机器,通常在apache安装目录的bin目录下
     
  3. 安装完成,到apache的modules目录看看是不是有了mod_deflates.so,如果有了这个文件,请编辑apache安装目录的conf/httpd.conf配置文件:
    LoadModule deflate_module modules/mod_deflate.so
    加载mod_deflate.so模块

    安装以后启用组件:

LoadModule deflate_module modules/mod_deflate.so

LoadModule headers_module modules/mod_headers.so

在http.conf末尾加上

 

#gzip压缩输出
<Location />
#启用压缩
SetOutputFilter DEFLATE
#指令设置压缩程度,越高的压缩程度就会有越好的压缩效果,同时也意味着占用越多的CPU资源。仅在 Apache 2.0.45 及以后的版本中可用
#DeflateCompressionLevel 9
#

#指定压缩的MIME类型
AddOutputFilterByType DEFLATE text/html text/xml text/javascript text/css text/plain
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</Location>

 

详解见官方文档:

http://man.lupaworld.com/content/manage/Apache2.2_chinese_manual/mod/mod_deflate.html

压缩效果检验:

http://www.phpchina.com/manual/apache/mod/core.html#location

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值