如何验证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(); 
   
} 
} 


} 

控制台结果为乱码 如下:

-1

�u:E>_��*�%�+:���ꍈk������ñdo�~#�6�M���v��Xݩ;�+v�Vܼ�;$1���v@����$�v޶����4����n#��g0��|��� �g��F[6k�R+{^60) ��U���_jt��9
��,��!9�


说明配置压缩网站的信息成功,此法可能对服务器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://gzip.zzbaike.com/  输入你的网站域名 检测压缩情况。








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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

prefectjava

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值