java Tomcat 压缩不添加 Content-Encoding: gzip in the Header
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/550024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tomcat Compression Does Not Add a Content-Encoding: gzip in the Header
提问by Julien Chastang
I am using Tomcat to compress my HTML content like this:
我正在使用 Tomcat 来压缩我的 HTML 内容,如下所示:
<Connector port="8080" maxHttpHeaderSize="8192"
maxProcessors="150" maxThreads="150" minSpareThreads="25"
maxSpareThreads="75" enableLookups="false" redirectPort="8443"
acceptCount="150" connectionTimeout="20000" disableUploadTimeout="true"
compression="on" compressionMinSize="128" noCompressionUserAgents="gozilla, traviata"
compressableMimeType="text/html"
URIEncoding="UTF-8" />
In the HTTP header (as observed via YSlow), however, I am not seeing
但是,在 HTTP 标头中(如通过 YSlow 观察到的),我没有看到
Content-Encoding: gzip
resulting in a poor YSlow score.
导致 YSlow 得分不佳。
All I see is
我看到的只是
HeadersPost
Response Headers
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=ISO-8859-1
Content-Language: en-US
Content-Length: 5251
Date: Sat, 14 Feb 2009 23:33:51 GMT
I am running an apache mod_jk Tomcat configuration.
我正在运行 apache mod_jk Tomcat 配置。
How do I compress HTML content with Tomcat, and also have it add "Content-Encoding: gzip" in the header?
如何使用 Tomcat 压缩 HTML 内容,并让它在标题中添加“Content-Encoding: gzip”?
采纳答案by cherouvim
Have a look at http://sourceforge.net/projects/pjl-comp-filter/.
看一下 http://sourceforge.net/projects/pjl-comp-filter/。
Other custom solutions may have memory leaks.
其他自定义解决方案可能存在内存泄漏。
Also, if you are using mod_jk then you are certainly not using the 8080 connector (which supports compression) for those requests.
此外,如果您使用的是 mod_jk,那么您肯定不会为这些请求使用 8080 连接器(支持压缩)。
回答by cherouvim
Tomcat will be doing the compression. However because you are using mod_jkI guess you are getting you requests via Apache on port 80 rather than tomcat on port 8080. As an experiment try getting your page via port 8080 and then checking yslowyou should see the correct headers.
Tomcat 将进行压缩。但是,因为您正在使用,mod_jk我猜您是通过端口 80 上的 Apache 而不是端口上的 tomcat 获取请求8080。作为实验,尝试通过端口 8080 获取您的页面,然后检查yslow您是否应该看到正确的标头。
I think what is happening is that apache is unzipping the content that it is getting from tomcat via mod_jkand then passing the deflated content on to the browser.
我认为正在发生的事情是 apache 正在解压缩它通过 tomcat 获取的内容,mod_jk然后将压缩的内容传递给浏览器。
If you wish to use mod_jkthen you will need to set up your compression on Apache rather than Tomcat.
如果您想使用,mod_jk那么您需要在 Apache 而不是 Tomcat 上设置压缩。
回答by Neil McKeown
Perhaps the compression Tomcat is referring to isn't gzip? It's a stab in the dark, but it might relate to white-space compression, or line trimming.
也许Tomcat所指的压缩不是gzip?这是黑暗中的刺痛,但它可能与空白压缩或线条修剪有关。
I would imagine Tomcat would be a bit more explicit in this regard (here's hoping).
我想 Tomcat 在这方面会更明确一些(希望如此)。
We have the gzip filter mentioned by duffmo running in our application, the web.xml looks something like this:
我们在我们的应用程序中运行了 duffmo 提到的 gzip 过滤器,web.xml 看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-app_2_4.xsd">
<display-name>App-Web</display-name>
<!-- FILTERS -->
<!-- Gzip filter -->
<filter>
<filter-name>GZIPFilter</filter-name>
<filter-class>weblogicx.servlet.gzip.filter.GZIPFilter</filter-class>
</filter>
[snip]
</web-app>
回答by Neil McKeown
I had a look at the Tomcat documentation here: http://tomcat.apache.org/tomcat-5.5-doc/config/http.html
我在这里查看了 Tomcat 文档:http: //tomcat.apache.org/tomcat-5.5-doc/config/http.html
It mentions using compression="force"which worked for me. It also says you can set a minimum number. This worked fine for me
它提到使用compression="force"which 对我有用。它还说你可以设置一个minimum number. 这对我来说很好用
<Connector port="8080" compression="256000" />
(compress anything over 256Kb)
(压缩超过 256Kb 的任何内容)
The default value for compressableMimeTypemeant that I didn't need that attribute.
Also note that it doesn't list CompressionMinSizeattribute.
的默认值compressableMimeType意味着我不需要该属性。另请注意,它没有列出CompressionMinSize属性。
回答by Rajendra
To improve overall client side performance of J2EE web application, you can try WebUtilities java library.
为了提高 J2EE Web 应用程序的整体客户端性能,您可以尝试使用 WebUtilities Java 库。
Here is the link :: http://code.google.com/p/webutilities/.
It provides filter, tag, servlet components to apply various client side performance practices resulting in higher performance rating against PageSpeed/YSlow.
它提供过滤器、标记、servlet 组件来应用各种客户端性能实践,从而针对 PageSpeed/YSlow 获得更高的性能评级。
Since version 0.0.4 it helps with following performance practices.
从 0.0.4 版开始,它有助于遵循性能实践。
- Minimize HTTP requests - can serve multiple JS/CSS files in one request
- Client Side Caching - adds proper Cache-Control, Expires header
- On the fly JS/CSS minification - using YUICompressor
- Compression - supports 2way compression for gzip/deflate/compress encodings
- Response Caching at Server - to avoid reprocessing of unchanged resources
- Add Character Encoding - to let browser know in advance
- 最小化 HTTP 请求 - 可以在一个请求中提供多个 JS/CSS 文件
- 客户端缓存 - 添加适当的缓存控制、过期标头
- 动态 JS/CSS 缩小 - 使用 YUICompressor
- 压缩 - 支持 gzip/deflate/compress 编码的 2way 压缩
- 服务器上的响应缓存 - 避免重新处理未更改的资源
- 添加字符编码 - 让浏览器提前知道
It is also highly configurable/customization against MIME, URL or User-Agents.
它也是针对 MIME、URL 或用户代理的高度可配置/自定义。

