tomcat开启gzip后,部分文件压缩,部分没压缩
随手记
tomcat中开启gzip压缩后,访问网页,发现部分css文件被压缩,但是部分css没有被压缩,tomcat配置如下:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" compressionMinSize="2048" noCompressionUserAgents="gozilla, traviata" compression="force"
compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain,application/javascript,image/jpeg,image/png"
relaxedQueryChars="[]|{}^\`"<>" relaxedPathChars="[]|{}^\`"<>" />
解决办法:
经查阅tomcat官网知:http://tomcat.apache.org/tomcat-7.0-doc/config/http.html

tomcat默认开启的useSendfile优先级比压缩的高,当文件大于48kb时,使用useSendfile传输模式。
所以,在配置中加上这句把useSendfile属性关掉就好:
useSendfile="false"
关于 useSendfile 和 compression的个人理解:
compression:服务器压缩资源,以节省带宽,是耗费cpu换带宽的做法
useSendfile:直接文件形式传输,把头尾工作去掉,节省cpu
一般启用gzip都是为了省带宽,所以还是compression香
浙公网安备 33010602011771号