How to enable GZIP compression of server's http responses in JBoss EAP 7
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,deflateheader requesting the response to be compressed - The request must reach EAP via HTTP protocol, AJP protocol does not support gzip encoding
- The client must send an
-
In order to create the gzip filter:
-
Connect to JBoss cli :
./jboss-cli.sh -cand then create a gzip filter using the below command :/subsystem=undertow/configuration=filter/gzip=gzipfilter:add()This will add the below
gzipfilterunder 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> -
Then add this filter to the
default-hostusing 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 "%r" %s %b "%{i,Referer}" "%{i,User-Agent}" "%{i,COOKIE}" "%{o,SET-COOKIE}" %S "%I" %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
- Predicates: A predicate is a function that takes a value and returns a true or false value. This allows actions to be taken based on the return value of the predicate. See Predicates in EAP 7 Undertow Configuration
- Refer to Migration guide from EAP6 to EAP7 : Web Subsystem Migration Section for more details.
- Refer to Content from undertow.io is not included.Undertow Predicates-Attributes-Handlers for more details on How to Use Predicates.
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.