脚本宝典收集整理的这篇文章主要介绍了如何验证Tomcat Gzip配置是否生效的方法,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我们在使用Tomcat优化配置时,都会开始Tomcat的Gzip压缩功能,配置如下:
<Connector port="9080" PRotocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" useBodyEncodingForURI="true" comPression="on" compressionMinSize="2048" noCompressionUserAgents="gozilla, traviata" compressableMimeTyPE="text/htML,text/XMl,text/javascript,text/css,text/plain,image/gif,image/jpg" />
即把以下内容加入到 apache-tomcat-6\conf\server.xml 中的connector标签中
<!-- Note : To use Gzip compression you could set the following properties : compression="on" compressionMinSize="2048" noCompressionUserAgents="gozilla, traviata" compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain,image/gif,image/jpg" -->
参数说明:
compression="on" 打开压缩功能
compressionMinSize="2048" 启用压缩的输出内容大小,当被压缩对象的大小>=该值时才会被压缩,这里面默认为2KB
noCompressionUserAgents="gozilla, traviata" 对于以下的浏览器,不启用压缩
compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain,image/gif,image/jpg" 压缩类型
注意:tomcat7以后,js文件的mimetype类型变为了application/JavaScript,具体的tomcat7定义的类型可以在:conf/web.xml文件中找到。
我的Tomcat6所以 还是 text/javascript
Tomcat7 js文件的mimetype类型变为了application/javascript
自己注意 配错了起不到压缩的作用哦
那么我们如何测试配置的Gzip压缩功能生效了呢?
答案就是:使用apache HttpClient访问该tomcat加载项目中的一个静态资源(比如一个js文件),然后打印请求的资源内容 或 资源ContentLength,如果打印的资源内容为乱码 或 ContentLength为 -1,则说明gzip生效了。
import org.apache.COMmons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.GetMethod; /** * @classname: Gziptest.java * @Description: TODO(用一句话描述该文件做什么) * * @author Administrator * @E-mail 809044093@QQ.com * @version V1.0 * @Date 2014-3-27 上午09:07:00 */ public class GzipTest { /** * @param args */ public static void main(String[] args) throws Exception{ HttpClient http = new HttpClient(); GetMethod get = new GetMethod("http://127.0.0.1:9080/memberCMS/style/shop/js/base.js"); try{ get.addRequestHeader("accept-encoding", "gzip,deflate"); get.addRequestHeader("user-agent", "Mozilla/5.0 (compatible; MSIE 6.0; Windows NT 5.0; Alexa Toolbar; Maxthon 2.0)"); int er = http.executeMethod(get); if(er==200){ System.out.println(get.getResponseContentLength()); String html = get.getResponseBodyAsString(); System.out.println(html); System.out.println(html.getBytes().length); } }finally{ get.releaseConnection(); } } }
控制台结果为乱码
说明配置压缩网站的信息成功,此法可能对服务器cpu有些损耗。
Apache 开启Gzip压缩配置:
去掉以下两个 注释#
LoadModule deflate_module modules/mod_deflate.so LoadModule headers_module modules/mod_headers.so
在 httpd.conf最后加入
#apache Gzip <Location /> # Insert filter SetOutputFilter DEFLATE # Netscape 4.x has some problems... browserMatch ^Mozilla/4 gzip-only-text/html # Netscape 4.06-4.08 have some more problems BrowserMatch ^Mozilla/4\.0[678] no-gzip # MSIE masquerades as Netscape, but IT is fine BrowserMatch \bMSIE !no-gzip !gzip-only-text/html # Don't compress images SetenvIfNoCase REQUEST_URI .(?:gif|jpe?g|png)$ no-gzip dont-VARy SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary SetEnvIfNoCase Request_URI .(?:pDF|doc|avi|mov|mP3|rm)$ no-gzip dont-vary AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/js # Make sure proxies don't deliver the wrong content #Header append Vary User-Agent env=!dont-vary </Location>
设置完成之后 访问http://tool.chinaz.com/Gzips/ 输入你的网站域名 检测压缩情况。
@H_126_55@ 您可能感兴趣的文章:以上是脚本宝典为你收集整理的如何验证Tomcat Gzip配置是否生效的方法全部内容,希望文章能够帮你解决如何验证Tomcat Gzip配置是否生效的方法所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。