Tomcat 服务器总是压缩为 gzip(即使 Accept-Encoding 字段不存在)
Tomcat server is always compressing as gzip (even when Accept-Encoding field is absent)
我最近开始使用 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 header。我测试了几种方法,包括多次切换压缩,调整 compressableMimeType 值,以及摆弄 compressionMinSize,但据我所知,这个实现没有检查 Accept-Encoding header已包含在内。
虽然我相信这可能是一个不必要的功能,因为现代浏览器早在 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 "%r" %s %b" />
</Host>
</Engine>
</Service>
</Server>
所以我终于弄明白 gzip 压缩是怎么回事了。由于 Postman 建立在 Chrome 之上(作为 Chrome 应用程序),所有 Postman 通信都通过 Chrome 发送。这很重要,因为 Chrome 有许多默认添加的 Header 值。事实上,这些“Restricted Http Headers” 被 Chrome 覆盖了。 Accept-Encoding 就是这样一个 header,所以我将其设置为什么并不重要——它被完全忽略了。
幸运的是 Chrome 还为 Postman 发布了一个插件来处理受限的 Http Header。一旦我下载了 Interceptor 插件并打开它,我就能看到我的 Tomcat 服务器实际上一直在正常工作。经验教训:如果您使用未正确校准的工具测量结果,那么您的答案是否正确并不重要。
我最近开始使用 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 header。我测试了几种方法,包括多次切换压缩,调整 compressableMimeType 值,以及摆弄 compressionMinSize,但据我所知,这个实现没有检查 Accept-Encoding header已包含在内。
虽然我相信这可能是一个不必要的功能,因为现代浏览器早在 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 "%r" %s %b" />
</Host>
</Engine>
</Service>
</Server>
所以我终于弄明白 gzip 压缩是怎么回事了。由于 Postman 建立在 Chrome 之上(作为 Chrome 应用程序),所有 Postman 通信都通过 Chrome 发送。这很重要,因为 Chrome 有许多默认添加的 Header 值。事实上,这些“Restricted Http Headers” 被 Chrome 覆盖了。 Accept-Encoding 就是这样一个 header,所以我将其设置为什么并不重要——它被完全忽略了。
幸运的是 Chrome 还为 Postman 发布了一个插件来处理受限的 Http Header。一旦我下载了 Interceptor 插件并打开它,我就能看到我的 Tomcat 服务器实际上一直在正常工作。经验教训:如果您使用未正确校准的工具测量结果,那么您的答案是否正确并不重要。