我最近尝试使用 gzip 压缩来提高 Web UI 性能。我按如下方式配置了 Tomcat Connector。
compression="on"
compressionMinSize="2048"
noCompressionUserAgents="gozilla, traviata"
compressableMimeType="text/html,text/xml,text/css,text/javascript,text/json,application/x-javascript,application/javascript,application/json"
下面是RequestHeader - Accept-Encoding是gzip,deflate。
Key Value
Request GET /app/jquery-ui.min.js HTTP/1.1
Accept */*
Referer https://cdduat.app.com/Apptech/
Accept-Language en-US
User-Agent Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3)
Accept-Encoding gzip, deflate
Host cdduat.app.com
Connection Keep-Alive
Cache-Control no-cache
Cookie JSESSIONID=CB793FFEE9A34B5B8E7DE34A17C90B5D; mbox=session#1436174197635-942865#1436176058|PC#1436174197635-942865.28_07#1437383802; s_fid=498342221B10B4ED-297E46742B9393BE; s_vi=[CS]v1|2ACD23BB851D5DBF-40001903C00C9391[CE]; oo_event_entry=41eebf1007f6e19f5b0ee4b5841be2441e970f9c
对于响应标头 - 没有 Accept-enconding 键值。此外,我不确定它是否有效。下面是响应标头。Web 表单加载时间仍然相同。不确定我在这里是否做错了什么。
Server Apache-Coyote/1.1
Accept-Ranges bytes
ETag W/"238326-1435860126000"
Last-Modified Thu, 02 Jul 2015 18:02:06 GMT
Content-Type application/javascript
Content-Length 238326
Date Mon, 13 Jul 2015 22:22:23 GMT
我还可以看到 Cache-control 值为 no-cache。这是否意味着浏览器不会缓存 JS 文件并在后续请求中再次请求这些资源?
我注意到 Transfer-encoding 是分块的。不确定它是否会覆盖 gzip 编码。任何帮助或指导都将非常有用。
答案1
看Apache Tomcat HTTP 连接器属性。您必须为所有连接器及其协议添加这些属性:
compression="on" compressionMinSize="0" useSendFile="false"
1.http连接器:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
compression="on" compressionMinSize="0" useSendFile="false"
redirectPort="8443" />
2.https连接器:将证书更改为您的证书
<Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol"
compression="on" compressionMinSize="0" useSendFile="false"
maxThreads="150" SSLEnabled="true" >
<SSLHostConfig>
<Certificate certificateKeyFile="conf/localhost-rsa-key.pem"
certificateFile="conf/localhost-rsa-cert.pem"
certificateChainFile="conf/localhost-rsa-chain.pem"
type="RSA" />
</SSLHostConfig>
</Connector>
如果您使用http2
协议并且tomcat9
- 也添加此行:
3. http2 连接器
<UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol"
compression="on" compressionMinSize="0" useSendFile="false" />