How to Enable Gzip Compression in Apache Tomcat

Standard

How to Enable Gzip Compression in Apache Tomcat

1. The tools

  • Java JDK
  • Apache Tomcat

2. Introduction

HTTP compression is a capability that can be built into web servers and web clients to improve transfer speed and bandwidth utilization. HTTP data is compressed before it is sent from the server: compliant browsers will announce what methods are supported to the server before downloading the correct format; browsers that do not support compliant compression method will download uncompressed data.

The most common compression schemes include gzip and Deflate. This means that when it is in use, your bandwidth costs for serving the site, will be lower because people visiting the site will be downloading smaller files. Using GZip, takes time and processor power to zip and unzip the files, but typically this is not a problem because the time it takes to do that, is often less than the time that is saved by downloading a smaller file. Therefore the overall effect is a time saving, despite the browser having to unzip the file.

In this example we are going to show how to enable Gzip compression on Tomcat Server.

3. Prerequisites

  • JDK installed

4. Download Tomcat

Go to the page https://tomcat.apache.org/download-80.cgi

Download the tomcat server as a zip compressed file for windows.

1 Download Tomcat for Windows

1 Download Tomcat for Windows

5. Tomcat Installation

5.1 Uncompress Apache Tomcat

Choose an installation directory and uncompress the Tomcat server in its own directory.

2 Uncompress Tomcat

2 Uncompress Tomcat

5.2 Install the Tomcat service

Open the Windows terminal and go to the Tomcat Installation bin directory.

Tomcat installation directory

1
C:\Java\Apache Tomcat 8.0.15\bin>

Install the service with the following command:

Install Tomcat service

1
C:\Java\Apache Tomcat 8.0.15\bin>service install

You should get an output similar to this:

install Tomcat output

1
2
3
4
5
6
7
Installing the service 'Tomcat8' ...
Using CATALINA_HOME:    "C:\Java\Apache Tomcat 8.0.15"
Using CATALINA_BASE:    "C:\Java\Apache Tomcat 8.0.15"
Using JAVA_HOME:        "C:\Java\jdk1.8.0_40"
Using JRE_HOME:         "C:\Java\jre1.8.0_40"
Using JVM:              "C:\Java\jre1.8.0_40\bin\client\jvm.dll"
The service 'Tomcat8' has been installed.

5.3 Start the Tomcat service

Start the service with the following command:

Start tomcat output

1
C:\Java\Apache Tomcat 8.0.15\bin>sc start Tomcat8

You should get an output similar to the following:

console

01
02
03
04
05
06
07
08
09
10
SERVICE_NAME: Tomcat8
        TYPE               : 10  WIN32_OWN_PROCESS
        STATUS             : 2  START_PENDING
                                (NOT_STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
        WIN32_OUTPUT_CODE  : 0  (0x0)
        SERVICE_OUTPUT_CODE: 0  (0x0)
        CHECK-POINT        : 0x0
        START-INDICATOR    : 0x7d0
        PID                : 5552
        MARKS              :

5.4 Check that tomcat is running

Open the browser in the URL: http://localhost:8080

And you should see the Tomcat Welcome screen.

3 Tomcat Welcome

3 Tomcat Welcome

6. Test your Tomcat before enable compression

6.1 Check with CURL

Curl is a command line tool for getting or sending files using URL syntax. With Tomcat running, open your terminal and type:

curl

1
curl  -H "Accept-Encoding: gzip" -I http://localhost:8080

You should get the following response:

Curl response not gzip

1
2
3
4
5
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=UTF-8
Transfer-Encoding: chunked
Date: Thu, 27 Oct 2016 16:37:55 GMT

6.2 Check with your Browser

Open your browser and open the developers tools [CTRL]+[SHIFT]+i in most browsers. Then go to the Tomcat URL http://localhost:8080

4 Before enable Gzip

4 Before enable Gzip

As you can see there is no Content-Encoding response and the size of the response is 32.8 KB

7. Enable Gzip on Tomcat

Go to TOMCAT_HOME/conf and edit server.xml

Under <Service name="Catalina"> tag
edit the connector tag so it looks like the following

connector

1
2
3
4
5
6
7
8
<Connector port="8080" maxHttpHeaderSize="8192"
           maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
           enableLookups="false" redirectPort="8443" acceptCount="100"
           connectionTimeout="20000" disableUploadTimeout="true"
           compression="on"
           compressionMinSize="1024"
           noCompressionUserAgents="gozilla, traviata"
           compressableMimeType="text/html,text/xml"/>

compression:

The Connector may use HTTP/1.1 GZIP compression in an attempt to save server bandwidth. The acceptable values for the parameter is “off” (disable compression), “on” (allow compression, which causes text data to be compressed), “force” (forces compression in all cases), or a numerical integer value (which is equivalent to “on”, but specifies the minimum amount of data before the output is compressed). If the content-length is not known and compression is set to “on” or more aggressive, the output will also be compressed. If not specified, this attribute is set to “off”.

compressionMinSize:

If compression is set to “on” then this attribute may be used to specify the minimum amount of data before the output is compressed. If not specified, this attribute is defaults to “2048”.

noCompressionUserAgents:

The value is a regular expression (using java.util.regex) matching the user-agent header of HTTP clients for which compression should not be used, because these clients, although they do advertise support for the feature, have a broken implementation. The default value is an empty String.

compressableMimeType:

The value is a comma separated list of MIME types for which HTTP compression may be used. The default value is text/html,text/xml,text/plain,text/css,text/javascript,application/javascript.

Restart Tomcat.

8. Test your Tomcat after enable compression

8.1 Check with CURL

Curl is a command line tool for getting or sending files using URL syntax.

With Tomcat running, open your terminal and type:

curl

1
curl  -H "Accept-Encoding: gzip" -I http://localhost:8080

You should get the following response:

Curl response not gzip

1
2
3
4
5
6
7
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=UTF-8
Transfer-Encoding: chunked
Content-Encoding: gzip
Vary: Accept-Encoding
Date: Thu, 27 Oct 2016 17:43:16 GMT

8.2 Check with your Browser

Open your browser and open the developers tools [CTRL]+[SHIFT]+i in most browsers. Then go to the Tomcat URL http://localhost:8080

5 After enable Gzip

5 After enable Gzip

As you can see the Content-Encoding response header is present and the size of the response is 23.8 KB

9. Conclusion

Using compression in Tomcat can save a lot of bandwidth. In this example we have seen that the initial size of the page was 32.8 KB and the size of the page after enable the compression was 23.8. This saves us 9 KB in this small page, multiply this for the number of daily request of your application and you will save a lot of bandwidth at the end of the day.

Leave a comment