2014-12-28

How to enable GZIP compression in Wildfly 8.2

Front-End Application performance is the key to improve user experience. Users expect pages to be loaded in two seconds. With large JavaScript libraries used for dynamic sites this is even more challenging. To speed up page loading and data transmission it is highly recommended (Google PageSpeed Rules, YSlow) to enable transparent gzip compression on the web-server. All modern browser support it.

Activate gzip compression in wildfly configuration file (e.g. standalone.xml) using the gzip filter as follows:

<subsystem xmlns="urn:jboss:domain:undertow:1.2">
  <server name="default-server">
    <host name="default-host" alias="localhost">
      <filter-ref name="gzipFilter" predicate="exists['%{o,Content-Type}'] and regex[pattern='(?:application/javascript|text/css|text/html|text/xml|application/json)(;.*)?', value=%{o,Content-Type}, full-match=true]"/>
      <filter-ref name="Vary-header"/>
    </host>
  </server>
  <filters>
    <gzip name="gzipFilter"/>
    <response-header name="Vary-header" header-name="Vary" header-value="Accept-Encoding"/>
  </filters>
</subsystem>

This enables compression based on resource content type for javascript, html and css.
Important:
To make proxy servers happy, you have to add the Vary: Accept-Encoding header as shown above if you use gzip compression. For details see here.

Check the site using Google PageSpeed Insights, Firebug plugin for Firefox or Chrome developer tools. Or use online tools like Pingdom Website Speed Test.