Tomcat服务器始终压缩为gzip(即使没有Accept-Encoding字段)

时间:2016-03-11 19:54:38

标签: tomcat compression gzip

我最近开始使用Apache Tomcat,我的一项任务是在动态Web内容上启用gzip压缩。虽然我在线发现了许多此功能的实现,但最后我选择按照此处的说明(http://viralpatel.net/blogs/enable-gzip-compression-in-tomcat/)并将以下行添加到server.xml文件的连接器部分。

compression="on" 
noCompressionUserAgents="gozilla, traviata" 
compressionMinSize="2048" 
compressableMimeType="text/html,text/xml,application/json"

编辑完文件并重新启动服务器后,我开始使用Postman测试我的一个servlet。我发现大于2048的正确MIME类型的所有内容都被压缩 - 即使我没有发送Accept-Encoding:gzip标头。我测试了几种方法,包括切换压缩几次,调整compressableMimeType值,摆弄compressMinSize,但尽管我可以告诉这个实现不检查是否包含了Accept-Encoding标头。

虽然我可以相信这可能是一个不必要的功能,因为现代浏览器在2000年开始支持压缩,我想确保我覆盖了我的所有基础。我假设我在这里遗漏了一些小事,因为我无法在网上找到这个问题的答案。我已经包含了我的server.xml的副本,我正在运行Apache Tomcat 7.0.65,我会感谢任何和所有帮助,以确定为什么我似乎无法选择不接收来自我新配置的服务器的压缩响应

<?xml version='1.0' encoding='utf-8'?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.  See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License.  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<Listener className="org.apache.catalina.core.JasperListener" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container"
          type="org.apache.catalina.UserDatabase"
          description="User database that can be updated and saved"
          factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
          pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>

<Service name="Catalina">

<Connector port="8080" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443"
    compression="on" 
    noCompressionUserAgents="gozilla, traviata" 
    compressionMinSize="2048" 
    compressableMimeType="text/html,text/xml" />

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

<Engine name="Catalina" defaultHost="localhost">

  <Realm className="org.apache.catalina.realm.LockOutRealm">
    <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
           resourceName="UserDatabase"/>
  </Realm>

  <Host name="localhost"  appBase="webapps"
        unpackWARs="true" autoDeploy="true">

    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
           prefix="localhost_access_log." suffix=".txt"
           pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>
</Server>

1 个答案:

答案 0 :(得分:3)

所以我终于弄清楚了gzip压缩的情况。由于Postman建立在Chrome(作为Chrome应用程序)之上,因此所有Postman通信都通过Chrome发送。这很重要,因为Chrome默认添加了许多标头值。事实上,这些“Restricted Http Headers”被Chrome覆盖。 Accept-Encoding是一个这样的标题,所以我设置它并不重要 - 它被彻底忽略了。

幸运的是,Chrome还发布了一个Postman插件来处理Restricted Http Headers。一旦我下载了Interceptor插件并打开它,我就能看到我的Tomcat服务器一直在正常工作。获得的经验教训:如果您使用未正确校准的工具测量结果,那么您的答案是否正确无关紧要。