Enable GZIP Compression on nginx Servers

By  on  

Speed kills, and there's nothing like a speedy website.  When you come to this blog, I want you to have a great experience, which is why I've worked tirelessly to compress every asset and avoid unnecessary synchronous interactions.  In reviewing my site with Google Pagespeed Insights, I noticed that my virtual private server from Media Temple wasn't configured to serve assets gzip compressed.  Oh no!  Here's how I enabled gzip compression and made my site miles faster!

Create a file at /etc/nginx/conf.d/gzip.conf with the following content:

gzip on;
gzip_proxied any;
gzip_types text/plain text/xml text/css application/x-javascript;
gzip_vary on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";

With that file now in place, restart your server and you will now be serving site assets with gzip compression.  Google takes site speed into account when ranking and placing your sites in their search engine so do your users a favor and strive for the fastest site possible -- especially for mobile users!

Recent Features

  • By
    Create Namespaced Classes with MooTools

    MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes like the Dojo Toolkit does.  Many developers create their classes as globals which is generally frowned up.  I mostly disagree with that stance, but each to their own.  In any event...

  • By
    Create a CSS Cube

    CSS cubes really showcase what CSS has become over the years, evolving from simple color and dimension directives to a language capable of creating deep, creative visuals.  Add animation and you've got something really neat.  Unfortunately each CSS cube tutorial I've read is a bit...

Incredible Demos

  • By
    Introducing MooTools ScrollSpy

    I've been excited to release this plugin for a long time. MooTools ScrollSpy is a unique but simple MooTools plugin that listens to page scrolling and fires events based on where the user has scrolled to in the page. Now you can fire specific...

  • By
    Sexy Opacity Animation with MooTools or jQuery

    A big part of the sexiness that is Apple software is Apple's use of opacity. Like seemingly every other Apple user interface technique, it needs to be ported to the web (</fanboy>). I've put together an example of a sexy opacity animation technique...

Discussion

  1. You can use

    gzip_comp_level 1

    Sets a gzip compression level of a response. Acceptable values are in the range from 1 to 9.

    • Nice article, I also like to use gzip_min_length parameter which reduces some overhead to gzip small responses.

  2. To test if gzip is enabled, run:

    curl -H "Accept-Encoding: gzip" -I http://davidwalsh.name/
    

    You should see content-encoding: gzip

  3. Sean Hsien

    For production systems, I would suggest the use of gzip_static.

  4. Marlin Gezker

    If you want to quickly test whether GZIP is enabled on your website or not use this tool: http://www.giftofspeed.com/gzip-test/
    Saved me a lot of time. It also shows you the compression percentages and stuff (for some of my pages it was 70% or more! :-))

  5. so gzip is only work on text type content?

  6. gzip_types text/plain text/xml text/css application/x-javascript text/javascript application/xml+rss;

    I prefer this line as this enables for javascript too

  7. Brent

    This snippet made it so when I restarted nginx it failed. Not sure what the deal is. Just an FYI for anyone planning to follow those directions exactly.

    • Flawid

      nginx fails to restart because sometimes you’ll have gzip already enabled.
      So removing the line “gzip on;” or commenting it with a # should do the job!

    • If you are using Ubuntu 10.04 just execute this command “sudo nginx”, and it shows you where is the problem if exist, my Nginx fails because this line “gzip on;”.
      It seems Gzip already ON but no configuration was set.

  8. Hey Brent,
    That depends on where you put the code and how you did it.
    Can you share your error So I can help.

  9. I put the code into my website config file at /etc/nginx/sites-enabled/foobar.com/ and it works 100%

    • Thanks.. I thought it was because of my outdated nginx version. I updated it to latest version but still getting the same that all .js files were not gzip-compressed. Hence I added "application/x-javascript application/javascript text/javascript" all together. Thanks

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!