Tomcat 压缩不会在标头中添加 Content-Encoding: gzip

发布于 2024-07-13 11:58:20 字数 902 浏览 10 评论 0原文

我正在使用 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" />

但是,在 HTTP 标头中(通过 YSlow 观察到),我没有看到

Content-Encoding: gzip

YSlow 分数不佳的结果。

我所看到的只是

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

我正在运行 apache mod_jk Tomcat 配置。

如何使用 Tomcat 压缩 HTML 内容,并在标头中添加“Content-Encoding: gzip”?

I am using Tomcat to compress my HTML content like this:

<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

Content-Encoding: gzip

resulting in a poor YSlow score.

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.

How do I compress HTML content with Tomcat, and also have it add "Content-Encoding: gzip" in the header?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

风蛊 2024-07-20 11:58:20

看看 http://sourceforge.net/projects/pjl-comp-filter/

其他自定义解决方案可能存在内存泄漏。

另外,如果您使用 mod_jk,那么您肯定不会使用 8080 连接器(支持压缩)来处理这些请求。

Have a look at 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.

小嗷兮 2024-07-20 11:58:20

Tomcat 将进行压缩。 但是,因为您使用的是 mod_jk,我猜您是通过端口 80 上的 Apache 而不是端口 8080 上的 tomcat 获取请求。 作为实验,尝试通过端口 8080 获取页面,然后检查 yslow,您应该会看到正确的标头。

我认为正在发生的事情是 apache 正在解压缩通过 mod_jk 从 tomcat 获取的内容,然后将压缩的内容传递到浏览器。

如果您希望使用 mod_jk,那么您需要在 Apache 而不是 Tomcat 上设置压缩。

Tomcat will be doing the compression. However because you are using mod_jk I 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 yslow you should see the correct headers.

I think what is happening is that apache is unzipping the content that it is getting from tomcat via mod_jk and then passing the deflated content on to the browser.

If you wish to use mod_jk then you will need to set up your compression on Apache rather than Tomcat.

毁梦 2024-07-20 11:58:20

也许Tomcat所指的压缩不是gzip? 这是一次黑暗中的尝试,但它可能与空白压缩或线条修剪有关。

我想 Tomcat 在这方面会更加明确(希望如此)。

我们的应用程序中运行着 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>

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.

I would imagine Tomcat would be a bit more explicit in this regard (here's hoping).

We have the gzip filter mentioned by duffmo running in our application, the web.xml looks something like this:

<?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>
若有似无的小暗淡 2024-07-20 11:58:20

要提高 J2EE Web 应用程序的整体客户端性能,您可以尝试 WebUtilities java 库。

这是链接::http://code.google.com/p/webutilities/< /a>.


它提供过滤器、标签、servlet 组件来应用各种客户端性能实践,从而获得比 PageSpeed/YSlow 更高的性能评级。

从 0.0.4 版本开始,它有助于以下性能实践。

  1. 最小化 HTTP 请求 - 可以在一个请求中提供多个 JS/CSS 文件
  2. 客户端缓存 - 添加适当的缓存控制、过期标头
  3. 动态 JS/CSS 缩小 - 使用 YUICompressor
  4. 压缩 - 支持 gzip/deflate/compress 编码的 2way 压缩
  5. 响应缓存在服务器上 - 避免重新处理未更改的资源
  6. 添加字符编码 - 让浏览器提前知道

它也是针对 MIME、URL 或用户代理的高度可配置/定制。

To improve overall client side performance of J2EE web application, you can try WebUtilities java library.

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.

Since version 0.0.4 it helps with following performance practices.

  1. Minimize HTTP requests - can serve multiple JS/CSS files in one request
  2. Client Side Caching - adds proper Cache-Control, Expires header
  3. On the fly JS/CSS minification - using YUICompressor
  4. Compression - supports 2way compression for gzip/deflate/compress encodings
  5. Response Caching at Server - to avoid reprocessing of unchanged resources
  6. Add Character Encoding - to let browser know in advance

It is also highly configurable/customization against MIME, URL or User-Agents.

梦在深巷 2024-07-20 11:58:20

我在这里查看了 Tomcat 文档:
http://tomcat.apache.org/tomcat-5.5-doc/ config/http.html

它提到使用对我有用的 compression="force" 。 它还表示您可以设置最小数量。 这对我来说效果很好

<Connector port="8080" compression="256000" />

(压缩任何超过 256Kb 的内容)

compressableMimeType 的默认值意味着我不需要该属性。
另请注意,它没有列出 CompressionMinSize 属性。

I had a look at the Tomcat documentation here:
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

<Connector port="8080" compression="256000" />

(compress anything over 256Kb)

The default value for compressableMimeType meant that I didn't need that attribute.
Also note that it doesn't list CompressionMinSize attribute.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文