Tips / Nginx

How to configure Nginx Gzip compression

Enabling Gzip compression in Nginx is very easy, and allow us to save bandwidth transfer and also almost duplicate/triplicate our page speed. First step is edit nginx.conf file, that could be located somewhere /etc/nginx/nginx.conf or /usr/local/nginx/conf/nginx.conf, in most distributions.

Once there, you just need to add this code to the http section, like this:

# enable gzip compression
gzip on;
gzip_min_length  1100;
gzip_buffers  4 32k;
gzip_types    text/plain application/x-javascript text/xml text/css;
gzip_vary on;
# end gzip configuration

Once you finish editing the file, just reload nginx:

/etc/init.d/nginx reload

Nginx GZip Compression Directives Explained

gzip on;  # Turns on/off the gzip compression.
gzip_min_length  1100; # The minimum size file to compress the files.

gzip_buffers  4 32k; # Set the buffer size of gzip, 4 32k is good enough for almost everybody.

gzip_types    text/plain application/x-javascript text/xml text/css; # This directive let you specify which file types should be compressed, in this case plain text, js files, xml and css.

gzip_vary on; # Enables response header of "Vary: Accept-Encoding
Note: you can also set the compression level using gzip_comp_level.

Examples shared by Adam Benayoun:

## Set gzip compression level at 1 – lowest and also the default.
gzip_comp_level 1;

## Set gzip compression level at 9 – highest parameter.
gzip_comp_level 9;

How to test Gzip compression is working in Nginx?

[my@lan ~]$ curl --header "Accept-Encoding: gzip,deflate,sdch" -I
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 26 Feb 2013 15:41:38 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Vary: Accept-Encoding
Content-Encoding: gzip

If you see Content-Encoding: gzip, that means your page was served using Nginx Gzip Module.

Another way to test this is using Gzip Test, an online tool that will check if you have Gzip compression enabled.

Additional Resources

Popular search terms:

  • nginx gzip
  • https://www scalescale com/tips/nginx/how-to-configure-nginx-gzip-compression/
  • how to use nginx gzip_types everything
  • gzip_buffers

Esteban Borges

Linux Geek, Webperf Addict, Nginx Fan. CTO @Infranetworking

  • A stupid question, is it possible to gzip things like images too, using these directives? Apparently there is even a directive “gzip_types *” which will gzip everything. Does that actually improve things?

    In my case, I use CloudFlare in front of everything, so I guess it makes little (or no) difference anyway, since CloudFlare will handle the compression by itself.

    • admin


      I guess it is possible, but not recommended, even if you use apache or any web server.
      As you said, if you are using Cloudflare, their CDN is already serving your images, not your nginx server.

  • /etc/init.d/nginx restart

    This is a very sad quote for a website named “nginxtips”.

    Almost never you need to restart nginx. Please, use reload instead. It’s more safe, fast and every request is served. With restart, there is a small period of time when users get “can not connect to server” error.

    • admin

      You are right on that Sergey, we started using reload instead of restart in all our tutorials.


  • I wish you would write in depth about the various directives and why they are good or not.

    For example – you haven’t touched the fact that one could specify the level of compression by adding this directive

    ## Set gzip compression level at 1 – lowest and also the default.
    gzip_comp_level 1;

    ## Set gzip compression level at 9 – highest parameter.
    gzip_comp_level 9;

    Here’s there is a tradeoff between the amount of compression you use (since compression utilize CPU – it will load your machine the more you’re aggressive) and the weight of the gzip’ed file. I would advise doing some tests but I usually find that for most files anything beyond 5 is just a waste of CPU cycle and does not affect compression anymore. 1-4 would probably be the safest bet.

  • admin


    You are right on that. I’ve added some explanation to each directive, and also shared your comment on the post.

    Thanks for your comment!

  • Adrian

    I am using ServerPilot in combo with digitalocean – and haven’t the slightest clue of how to do this. How do you actually edit the file? What are the steps? How do you get to the location etc..?

    Totally new to server setup and am lost.

  • admin

    Never worked with ServerPilot before, just use the ssh terminal and type:
    nano -w /etc/nginx/nginx.conf or nano -w /usr/local/nginx/conf/nginx.conf

    Write the changes and then hit CTRL + X and then press Y to save the file

    Reload nginx to apply changes:
    service nginx reload