Enable Server-side Compression in Elastic Beanstalk

Updated Jan 25, 2018

This post is part of the series WordPress and Elastic Beanstalk.

Enabling gzip compression on your server results in smaller file sizes that need to be retrieved by the browser. To achieve this in Elastic Beanstalk running Apache we can add a configuration file to the .ebextensions directory in our source bundle.

Elastic Beanstalk will process any file in the .ebextensions directory ending in “.config”. Files are processed in alphabetical order. In this case we’ll name the file deflate.config:

deflate.config Download Copy
mode: "000644"
owner: root
group: root
content: |
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xml+rss
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/css
DeflateCompressionLevel 9
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
<IfModule mod_headers.c>
Header append Vary User-Agent env=!dont-vary

Put the above file in your .ebextensions directory in your source bundle and you should be all set. You might also be interested in the post Set Cache Controls in Elastic Beanstalk. Feel free to ask questions or suggest improvements in the comments section below.

Comments Leave a comment

  • John
    Does not work in 2023. Why is AWS not doing Optimizations by default (just like security)
  • Brad

    Awesome, work for me with a Flask app as well.

    P.s. take note of the spacing syntax, it must be precisely as you see!

  • Pablo

    Thanks! It worked perfectly with for flask application.


Leave a comment