美文网首页
tomcat8 启动gzip压缩

tomcat8 启动gzip压缩

作者: webmrxu | 来源:发表于2018-01-27 15:47 被阅读0次

参考goole提供的优化步骤进行优化。网址:https://developers.google.com/speed/pagespeed/insights/

  1. 启用gzip压缩,使用的tomcat服务器,需要对服务器进行配置。修改%TOMCAT_HOME%/conf/server.xml文件
   <Connector port="80" protocol="HTTP/1.1"   
         connectionTimeout="20000"   
         redirectPort="8443" executor="tomcatThreadPool" URIEncoding="utf-8"  
 
         compression="on"   
         compressionMinSize="50"
         noCompressionUserAgents="gozilla, traviata"
     useSendfile="false" 
         compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain" />

各属性介绍:

  • compression="on" 打开压缩功能
  • compressionMinSize="50" 启用压缩的输出内容大小,默认为2KB
  • noCompressionUserAgents="gozilla, traviata" 对于以下的浏览器,不启用压缩
  • compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain" 哪些资源类型需要压缩
  • useSendfile="false" tomcat默认设置是当数据大小达到48kb时,将启用文件传输(sendfile),所以我们想要压缩超过48kb的数据时必须将useSendfile设置为false,具体看官方文档解释。

以上开启gzip压缩的配置在tomcat8中不生效。
原因:tomcat7以后,js文件的mimetype类型变为了application/javascript
解决:
将配置:compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain"
改为:compressableMimeType="text/html,text/xml,application/javascript,text/css,text/plain"

完整配置:tomcat8 启动gzip压缩

   <Connector port="80" protocol="HTTP/1.1"   
         connectionTimeout="20000"   
         redirectPort="8443" executor="tomcatThreadPool" URIEncoding="utf-8"  
         compression="on"   
         compressionMinSize="50"
         noCompressionUserAgents="gozilla, traviata"
        useSendfile="false" 
       compressableMimeType="text/html,text/xml,application/javascript,text/css,text/plain"  />

压缩比率:3.3M的js文件,经过压缩,大小为542KB

压缩前: 最大的文件为 2.2M

1516929638(1).jpg

压缩后:最大文件为 507kb


1516929704(1).jpg

相关文章

网友评论

      本文标题:tomcat8 启动gzip压缩

      本文链接:https://www.haomeiwen.com/subject/khriaxtx.html