0755mx 发表于 2017-1-9 09:04:54

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的时候没有编译相关模块,就需要你手动安装一次,以启用它:


[*]
首先到你的apache源码目录,查找到mod_deflate.c文件

Mac及Linux下都可用locate mod_deflate.c

通常位置:apachehttpd源码目录/modules/filters/mod_deflate.c

 
[*]
进入上面找到的目录运行下面的命令:

/usr/local/apache/bin/apxs -i -c -a mod_deflate.c

注:apxs目录请参照您自己的机器,通常在apache安装目录的bin目录下

 
[*]
安装完成,到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 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
页: [1]
查看完整版本: apache和tomcat下开启和检验gzip输出