How to enable gzip on Amazon EC2 Instance

I am still tweaking small things on the Amazon EC2 server that is hosting my site. One of the things that I did not do immediately is enable gzip compression of all the site data when it is served to a browser. What this does is compress all the files down before they are pushed across all those tubes that make up the internet, and the browser then decompresses the files on the other side.

Depending on the type of file that is being compressed, this can really reduce the bandwidth needed to display your site. This is a benefit to both the site owner and and the site user.

In the case of the EC2 AMI1 installation that I am using, the apache server loads the mod_deflate module by default, so there is nothing to configure in /etc/http/conf/httpd.conf.

All that is needed is to paste the following recommendations from HTML5 Boilerplate to your .htaccess file that is located at the public root of your site:

#|Compression |
#Force compression for mangled headers.

SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s,?\s)+|[X~-]{4,13}$ HAVE_Accept-Encoding
RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding

#Compress all output labeled with one of the following MIME-types
#(for Apache versions below 2.3.7, you don't need to enable mod_filter
#and can remove the and lines
#as AddOutputFilterByType is still in the core directives).

AddOutputFilterByType DEFLATE application/atom+xml \
application/javascript \
application/json \
application/ld+json \
application/rss+xml \
application/ \
application/x-font-ttf \
application/x-web-app-manifest+json \
application/xhtml+xml \
application/xml \
font/opentype \
image/svg+xml \
image/x-icon \
text/css \
text/html \
text/plain \
text/x-component \

This will allow your site to consume significantly less bandwidth in many situations. A painless win!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s