How to enable GZIP compression of server's http responses in JBoss EAP 7

Solution Verified - Updated

Environment

  • Red Hat Enterprise Application Platform (EAP)
    • 7.x

Issue

  • How to update our Jboss system to enable GZIP compression. Is this something new in EAP 7 and would it require changes to enable it?

Resolution

  • First of all consider that there are a number of conditions for response compression to work:

    • The client must send an Accept-Encoding=gzip,deflate header requesting the response to be compressed
    • The request must reach EAP via HTTP protocol, AJP protocol does not support gzip encoding
  • In order to create the gzip filter:

    1. Connect to JBoss cli : ./jboss-cli.sh -c and then create a gzip filter using the below command :

       /subsystem=undertow/configuration=filter/gzip=gzipfilter:add()
      

      This will add the below gzipfilter under the undertow subsystem :

      <subsystem xmlns="urn:jboss:domain:undertow:3.1">
      ........
      ........
       <filters>
                    <response-header name="server-header" header-name="Server" header-value="JBoss-EAP/7"/>
                    <response-header name="x-powered-by-header" header-name="X-Powered-By" header-value="Undertow/1"/>
                    <gzip name="gzipfilter"/>
                </filters>
            </subsystem>
      
    2. Then add this filter to the default-host using the below CLI command :

       /subsystem=undertow/server=default-server/host=default-host/filter-ref=gzipfilter:add()
      

      which creates the below configuration in standalone-*.xml :

        <subsystem xmlns="urn:jboss:domain:undertow:3.1">
                  ......
                  .......
      
                      <host name="default-host" default-web-module="ClusterWebApp.war" alias="localhost">
                          <access-log pattern="%h %l %u %t &quot;%r&quot; %s %b &quot;%{i,Referer}&quot; &quot;%{i,User-Agent}&quot; &quot;%{i,COOKIE}&quot; &quot;%{o,SET-COOKIE}&quot; %S &quot;%I&quot; %T"/>
                          ......
                          .....
                          <filter-ref name="gzipfilter"/>
      
                          .......
      
  • Predicates can be used for conditional execution of the gzip filter. Some examples:

    • For using gzip filter for COMPRESSION_MIME_TYPES like text/javascript,text/css,text/html & having COMPRESSION_MIN_SIZE as 1024.

      /subsystem=undertow/server=default-server/host=default-host/filter-ref=gzipfilter:add(predicate="regex[pattern='text/(css|html|javascript)',value=%{o,Content-Type}] and (max-content-size[value=1024] or not exists(%{o,Content-Length}))")
      

      which creates the below configuration in standalone-*.xml :

      <subsystem xmlns="urn:jboss:domain:undertow:3.1">
                    ... other configs....
                      <server name="default-server">
                          .... other configs....
                              <filter-ref name="gzipfilter" predicate="regex[pattern='text/(css|html|javascript)',value=%{o,Content-Type}] and (max-content-size[value=1024] or not exists(%{o,Content-Length}))"/>
                      .... other configs....
                      <filters>
                          <gzip name="gzipfilter"/>
                      </filters> 
       	</subsystem>
      

      This will cause the gzip filter to work for only text/javascript,text/css,text/html content types which are greater than 1024 bytes or in chunked encoded request (no content-length header).

    • Similarly, the filter can be configured per context:

      <subsystem xmlns="urn:jboss:domain:undertow:3.1">
                    ... other configs....
                      <server name="default-server">
                          .... other configs....
                             <filter-ref name="gzipfilter" predicate="path-template(value='/context1/*', match=%U) or path-template(value='/context2/*', match=%U) or path-template(value='/context3/*', match=%U)"/> 
                      .... other configs....
                      <filters>
                          <gzip name="gzipfilter"/>
                      </filters> 
       	</subsystem>
      
  • Additional reading

Components
Category

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.