Tomcat开启压缩传输可将静态资源压缩传输,节省带宽显著提高访问性能。开启方法很简单,打开%Tomcat%\conf\server.xml配置文件,找到访问端口对应的Connector段,如下配置即可:

<Connector port="80" protocol="HTTP/1.1"

connectionTimeout="20000"

relaxedPathChars="|{}[]^"

relaxedQueryChars="[]|{}^&#x5c;&#x60;&quot;&lt;&gt;"

redirectPort="443"

URIEncoding="UTF-8"

compression="on"

compressionMinSize="2048"

noCompressionUserAgents="gozilla, traviata"  

compressableMimeType="text/html,text/xml,text/plain,text/css,application/javascript"

useSendfile="false"

maxHttpHeaderSize="8192" />

注意:compressableMimeType项适用于Tomcat8及以下,Tomcat9中会报错:

org.apache.tomcat.util.digester.SetPropertiesRule.begin Match[Server/Service/Connector]无法将属性[compressableMimeType]设置为[text/html,text/xml,text/plain,text/css,application/javascript]

Tomcat9中用compressibleMimeType,即:

compressibleMimeType="text/html,text/xml,text/plain,text/css,application/javascript"

以上是针对http的配置,如果是配置了https,则在端口为443的Connector段中加,形如:

<Connector port="443" protocol="org.apache.coyote.http11.Http11NioProtocol"

maxThreads="1500" SSLEnabled="true" URIEncoding="UTF-8"

relaxedPathChars="|{}[]^"

relaxedQueryChars="[]|{}^&#x5c;&#x60;&quot;&lt;&gt;"

defaultSSLHostConfigName="www.huadaninfo.com"

compression="on"

compressionMinSize="2048"

noCompressionUserAgents="gozilla, traviata"  

compressibleMimeType="text/html,text/xml,text/plain,text/css,application/javascript"

useSendfile="false"

maxHttpHeaderSize="8192">

注意:布署华丹开发平台时,我们建议用nginx+tomcat集群的方式,因静态文件均由nginx处理,此时只需要在nginx中配置压缩传输即可,不需要在Tomcat中重复配置。