How to activate GZIP in a Grails 3 app deployed with ElasticBeanstalk?

Background: I needed to activate GZIP in a Grails 3 app which I’ve deployed with AWS Elastic Beanstalk in a Tomcat 8. I landed in this Stackoverflow question which pointed me in the right direction.

If you want to have a folder in the root directory of your Grails 3 generated war then you will have to create the folder under src/main/webapp.

1. Create the .ebextensions folder


2. Create a myapp.config file under the previously created directory with the next content:

            command: "cp .ebextensions/enable_mod_deflate.conf /etc/httpd/conf.d/enable_mod_deflate.conf"

This will trigger the copy of the enable_mod_deflate.conf  file to the apache configuration directory.

3.  Create enable_mod_deflate.conf file with the next content. You can configure the mime types for which you desire to active deflate.

SetOutputFilter DEFLATE
# mod_deflate configuration
<IfModule mod_deflate.c>
    # Restrict compression to these MIME types
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE application/json
    AddOutputFilterByType DEFLATE application/xml+rss
    AddOutputFilterByType DEFLATE application/x-javascript
    AddOutputFilterByType DEFLATE text/javascript
    AddOutputFilterByType DEFLATE text/css
    <IfModule mod_headers.c>
        # Make sure proxies don't deliver the wrong content
        Header append Vary User-Agent env=!dont-vary

4. Generate a new war file (with this folder in the root directory) and deploy it to ElasticBeanstalk.

5. Ssh to your EC2 instance and restart apache manually. Note. Restarting the app though ElasticBeanstalk wizard will just restart Tomcat 8.

Do you like to read about groovy development? Yes, then Subscribe to Groovy Calamari a weekly curated email newsletter about the Groovy ecosystem which I happen to write 😉

Leave a Reply

Your email address will not be published. Required fields are marked *