Enabling compression on Apache Tomcat

If you use Apache as a front-end for Tomcat with mod_jk or mod_proxy, it's advisable to use Apache for compression instead of Tomcat. Apache's compression is much more flexible and faster.

It is not possible to disable compression for incompatible browsers using Tomcat itself. You may wish to do this using a front-end web server that does support it.

To enable compression, first find your Tomcat directory. On Windows it will probably be C:\Program Files\Apache\Apache Tomcat, on Unix there is no standard location but it contains your webapps directory so you should know where it is.

Open the conf directory inside it, and edit the server.xml file. Find the CoyoteConnector connector line, which should look like this:

<Connector className="org.apache.coyote.tomcat4.CoyoteConnector"
    acceptCount="100" bufferSize="2048" compression="off"
    connectionLinger="-1" connectionTimeout="20000"
    connectionUploadTimeout="300000" debug="0"
    disableUploadTimeout="true" enableLookups="true"
    maxKeepAliveRequests="100" maxProcessors="75"
    minProcessors="5" port="8130" 
    protocolHandlerClassName="org.apache.coyote.http11.Http11Protocol" 
    proxyPort="0" redirectPort="8132" scheme="http" 
    secure="false" serverSocketTimeout="0" 
    tcpNoDelay="true" tomcatAuthentication="true" 
    useBodyEncodingForURI="true" useURIValidationHack="false">

Change compression to 5000, which sets the minimum size of response which will be compressed, and compressableMimeType to text/html,text/xml,text/plain,application/x-javascript,text/css