Skip to content

gZip setup

Overview

If you’re serving your AIV web application through Apache Tomcat and have pre-compressed your JavaScript files as .js.gz, you’ll need to ensure that Tomcat serves these gzipped files to clients that support gzip compression.

Setup Steps

  1. Ensure the .js.gz files exist:
    Before making any configuration changes, ensure that you’ve pre-compressed your .js files to .js.gz in the appropriate directories.

  2. Enable the gzip filter in Tomcat:

  • Open the web.xml file located in TOMCAT_HOME/conf/.
  • Find the <mime-mapping> section and add the following mapping for .gz files:
     <mime-mapping>
         <extension>gz</extension>
         <mime-type>application/gzip</mime-type>
     </mime-mapping>
  • Save the changes.
  1. Configure Your Application to Serve Gzipped Content (This step is not compulsory contact with the AIV team so that they can make suggestions based on your environment):
  • In your web application’s web.xml file (usually located in WEB-INF/web.xml), add the following filter and filter-mapping:
     <filter>
         <filter-name>GzipFilter</filter-name>
         <filter-class>org.apache.catalina.filters.ExpiresFilter</filter-class>
         <init-param>
             <param-name>ExpiresByType application/javascript</param-name>
             <param-value>access plus 1 month</param-value>
         </init-param>
     </filter>
     
     <filter-mapping>
         <filter-name>GzipFilter</filter-name>
         <url-pattern>*.js</url-pattern>
     </filter-mapping>
  • Save the changes.
  1. Locate the server.xml File
  • The server.xml file can be found in the conf directory of your Tomcat installation. The typical path is:
TOMCAT_HOME/conf/server.xml
  • Where TOMCAT_HOME is the root directory of your Tomcat installation.
  1. Find the <Connector> Element
  • Within the server.xml file, locate the <Connector> element that’s configured for the HTTP/1.1 protocol. This is typically the connector used for serving HTTP requests.

  • It will look something like this:

<Connector port="8080" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443" />
  1. Update the <Connector> Element with Compression Settings

Add the compression settings to the <Connector> element as follows:

<Connector port="8080" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443"
           compression="on"
           compressionMinSize="1024"
           noCompressionUserAgents="gozilla, traviata" 
           compressableMimeType="text/html,text/xml,text/plain,
           text/css,text/javascript,application/javascript" />

Here’s a breakdown of the added attributes:

  • compression="on": Enables GZIP compression.
  • compressionMinSize="1024": Sets the minimum size (in bytes) for which Tomcat will compress the output. In this case, output will be compressed if it’s larger than 1024 bytes.
  • noCompressionUserAgents="gozilla, traviata": Specifies user agents for which compression should not be used.
  • compressableMimeType="...": Specifies a comma-separated list of MIME types for which compression should be applied.
  1. Test Your Configuration:
  • Restart Tomcat.
  • Access your web application in a browser.
  • Use browser developer tools to inspect the response headers of your JavaScript files. You should see the Content-Encoding: gzip header for .js files if the client supports gzip and the server is correctly serving the gzipped content.

Conclusion

  • By following the above steps, you can set up Tomcat to serve pre-compressed .js.gz files for your web application.