启用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" />
发表评论(对文章涉及的知识点还有疑问,可以在这里留言,老高看到后会及时回复的。)