Maciej Rumianowski's Weblog 

GZIP Content-Encoding in WildFly 8.0.0.Final with Undertow

by Maciej Rumianowski


Posted on Sunday Mar 23, 2014 at 05:37PM in General


WildFly 8.0.0.Final support gzip content-encoding which is recommended by Yahoo! YSlow. However, it is hard to find documentation how to enable this feature. After digging JBoss community forum and looking into some commits I have found some clues and here is the configuration!

<subsystem xmlns="urn:jboss:domain:undertow:1.0">
   <buffer-caches>
      <buffer-cache name="default" buffer-size="1024" buffers-per-region="1024" max-regions="10"/>
   </buffer-caches>
   <server name="default-server">
      <http-listener name="default" socket-binding="http"/>
      <host name="default-host" alias="localhost">
          <location name="/" handler="welcome-content" />
          <filter-ref name="gzipFilter" predicate="path-suffix['.css'] or path-suffix['.js']" />
          <filter-ref name="server-header"/>
          <filter-ref name="x-powered-by-header"/>
      </host>
   </server>
   <servlet-container name="default" default-buffer-cache="default" stack-trace-on-error="local-only">
      <jsp-config/>
   </servlet-container>
   <handlers>
      <file name="welcome-content" path="${jboss.home.dir}/welcome-content" directory-listing="true"/>
   </handlers>
   <filters>
      <response-header name="server-header" header-name="Server" header-value="Wildfly 8"/>
      <response-header name="x-powered-by-header" header-name="X-Powered-By" header-value="Undertow 1"/>
      <gzip name="gzipFilter"/>
   </filters>
</subsystem>

Two things have to be done:

  1. Enable gzip filter in filters tag
  2. Use filter-ref tag in host configuration. Above configuration uses predicate to compress only CSS and JavaScript files

UPDATE:

Jean Revertera has pasted in comments a better regular expression to match resources by Content-Type rather than a file extension. I have seen other patterns in undertow schema, but they were not working correctly for me. I don't know yet what is the syntax for value in regex but it looks like "o" is better than "i" in the "value=%{o,Content-Type}" part of the regex. Here is Jean's filter:

<filter-ref name="gzipFilter" predicate="regex[pattern='(?:application/javascript|text/css|text/html)(;.*)?', value=%{o,Content-Type}, full-match=true]"/>

http://pastebin.com/bJgCk56r



Comments:

Nice post. One note: the proposed predicate will not match requests having a query string appended (which is fairly common when using JSF resources). You may otherwise rely on the response content-type to apply the filter, something like that: http://pastebin.com/bJgCk56r

Posted by Jean Revertera on May 15, 2014 at 03:08 PM GMT #


Very useful post and comments :) . There is very little documentation about this matter so they've helped me a lot! It would be good that you added this information in the official documentation too, so that other users can access it easily: https://docs.jboss.org/author/display/WFLY8/Undertow+%28web%29+subsystem+configuration . Thank you a lot!

Posted by negora on June 05, 2014 at 06:44 PM GMT #


Playing with the predicate, I've came with one that covers HTML, CSS, JavaScript and typical XML responses too: [filter-ref name="gzipFilter" predicate="regex[pattern='^(?:text/(?:html|css)|application/(?:javascript|/xml))(?:;|$)', value=%{o,Content-Type }, full-match=false]"/] This predicate uses a partial match in order to avoid evaluating the rest of the Content-Type header.

Posted by negora on June 05, 2014 at 08:09 PM GMT #


To only gzip application/octet-stream with content larger than 999 bytes, use this filter (content-length needs to be set in your http response): <filter-ref name="gzipFilter" predicate="regex[pattern='(?:application/octet-stream)(;.*)?', value=%{o,Content-Type}, full-match=true] and regex[pattern='\d{4,}', value=%{o,Content-Length}, full-match=true]"/>

Posted by sepadev on October 01, 2014 at 11:59 AM GMT #


Follow up to last post. Regex predicate (io.undertow.predicate.RegularExpressionPredicate) crashes(NPE) if Content-Type or Content-Length are not defined. This is a fully functional predicate: <filter-ref name="gzipFilter" predicate="exists['%{o,Content-Type}'] and exists['%{o,Content-Length}'] and regex[pattern='(?:application/octet-stream)(;.*)?', value=%{o,Content-Type}, full-match=true] and regex[pattern='\d{4,}', value=%{o,Content-Length}, full-match=true]"/>

Posted by sepadev on November 21, 2014 at 11:32 AM GMT #


Thanks

Posted by Hamed on November 28, 2014 at 07:56 AM GMT #


Hello Thank you all for your shares, I was able to use your ideas + http://dimaki.blogspot.pt/2014/12/how-to-enable-gzip-compression-in.html and I came up with this that works for me: <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]"/>

Posted by Joni on January 10, 2015 at 06:16 AM GMT #


[Trackback] Environment WildFly 8.2.0.Final Requirement WildFly runs as a regular user owning process WildFly listens port 8080 and 8443 iptables forwards 80 and 443 to ports of WildFly is listening Record combin...

Posted by Kyle's Laboratory on February 08, 2015 at 07:23 AM GMT #


Hi, I'm using Wildfly 8.2 in production, in my scenario has multiple host example: app.mydomain / 1000 and app.mydomain / 1001 and app.mydomain / 2000. Where these numbers after my domain is .WAR versions of Java. 1000.war and 1001.war. I want to enable GZip for them all, but I noticed that the setting in Standalone-full.xml I do package by package or by DNS DNS. And this becomes laborious. I wonder if there is any way to apply the GZip for all the packages made on the application server. Any idea?

Posted by Adriano on February 10, 2015 at 10:35 PM GMT #


I used the previous feature to enable gzip in my ubuntu machine. But I could enable the gzip in my centos server. Is there any further changes I have to do before I can enable gzip in the Centos Server. I used most of the filter-refs in the above comments. But still I'm failed.

Posted by Arachchi on February 27, 2015 at 02:42 PM GMT #


I used the setting GZip started to work, however there are very small files that I do not want it done GZip them, there cofigurar as the minimum size of the files to be zipped or not? Use WildFly 8.2, thank you

Posted by Cristian on March 11, 2015 at 08:19 PM GMT #


Leave a Comment

HTML Syntax: NOT allowed