views:

3660

answers:

5

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?

+2  A: 

Try using a gzip compression filter:

http://www.onjava.com/pub/a/onjava/2003/11/19/filters.html

Configure it in your web.xml and you should be all set.

duffymo
A variation on this filter is what I ended up doing. I often find Tomcat disappointing.
Julien Chastang
This is old and only for educational purposes. After using it in production for 2 years I found out that it was leaking memory.
cherouvim
Interesting. What would an industrial strength one look like? Did you ever profile it to pinpoint where the leak was coming from?
duffymo
@duffymo: I know that my comment was without references and I'm afraid that I do not have any further input right now on where the problem was. The truth though is that PJL saved me. Too bad it seems to be a dead project as well.
cherouvim
What's PJL? Never heard of it.
duffymo
http://sourceforge.net/projects/pjl-comp-filter/
cherouvim
Thank you, cherouvim, much appreciated. I'll have to look at the source code and see how they're different.
duffymo
Not sure that I agree with "dead project" being a bad thing. What else can be done with a compression filter? It should be possible to bring it to a final state and leave it. I'd consider it "mature" - so much nicer than the "dead" label.
duffymo
@duffymo: I said it is "dead" because it was last updated on Sep 20 2007 and there are many unanswered questions at the forums: http://sourceforge.net/forum/forum.php?forum_id=378427
cherouvim
Good point. 13 bugs, 12 feature requests. The first bug was submitted in Mar 2008, so it looks a bit more active than you're saying. I'd have to spend some time to see if any of the bugs were serious. Are you still using it? No memory leaks? Working well?
duffymo
@duffymo: I've been using it for 1.5 year on ~20 production sites. The busiest does 300.000 tomcat pageviews per day. Largest page generated is around 160kb. It runs on TC6 with -Xms256m -Xmx512m and highest uptime has been 2 months (until I restart for upgrade reasons).
cherouvim
update: busiest now does 1 mil pages per day
cherouvim
+1  A: 

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>
Neil McKeown
No, enabling compression in tomcat means gzip - it works fine for us under tomcat 6.0.20, though we do not have apache in front of our tomcat
nos
+2  A: 

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.

cherouvim
cherouvim: Thanks. I will take a look.
Julien Chastang
+1  A: 

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.

Good luck

AJP is a binary protocol anyway, so compression won't work on the AJP Connector. http://en.wikipedia.org/wiki/Apache_JServ_ProtocolI wouldn't recommend using the HTTP connector on a high-load production server anyway, regardless of compression being on/off.
opyate
+1  A: 

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.

AndCo