October 12th, 2011

Effectively enabling gzip in nginx

Google released their Page Speed Online tool today. Originally available as a Chrome extension, Pagespeed lets you analyze the performance of your page and how it adheres to web best practices. It is a pretty neat tool which developers should be running on their sites before they go live to make sure everything is in order.

One of the thing I was missing was having gzip compression enabled on all my resources. By default, nginx has gzip compression enabled, but only for plain text MIME types. With all the HTML5 sites sporting fancy web fonts these days, compression becomes especially important.

The gzip_types directive inside the html {} configutation block lets you specify what MIME types you want to gzip. Check out the mime.types file in your nginx configuration directory to see the mappings between MIME types and file extensions.

Here is how you get gzip compression going for more of the commonly used MIME types for a typical HTML5 site:

gzip on;
gzip_types text/plain text/css
           application/x-javascript application/xml
           application/x-font-woff image/svg+xml;
gzip_vary on;

Javascript, CSS, and web fonts should all be compressed with gzip. Woohoo!