【发布时间】:2016-03-11 19:54:38
【问题描述】:
我最近开始使用 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。我发现所有正确的 MIME 类型(大于 2048)都被压缩了——即使我没有发送 Accept-Encoding:gzip 标头。我测试了几种方法,包括切换压缩几次,调整 compressableMimeType 值,以及摆弄compressionMinSize,但据我所知,这个实现没有检查是否包含 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 "%r" %s %b" />
</Host>
</Engine>
</Service>
</Server>
【问题讨论】:
-
你是怎么测试的?
-
我使用 Google 应用 Postman 对其进行了测试。我向托管在我的 Tomcat 实例上的端点发送了多个 GET 请求,没有 Accept-Encoding 标头。每个响应都标有 Content-Encoding → gzip 标头。
标签: tomcat compression gzip