Posted on 09.05.2013 by Rajib

One of the most popular form of compression for file transfer Gzip compression have been used for decades now. Modern browsers are smart enough to recognize if the webpage has Gzip compression header. But what does Gzip actually do ? In short it compresses the texts, scripts, in the page. It can also compress Images but usually web servers are designed to compress the images by default, so, compressing the image again using Gzip would be waste of resources. So, its better to use Gzip to compress the texts, CSS, JavaScripts, etc. Nginx offers Gzip compression via Nginz Gzip Module. Lets look at the Nginx config for compressing page with Gzip and it'd be easier to explain.

gzip on;
gzip_buffers 16 8k;
gzip_comp_level 5;
gzip_http_version 1.1;
gzip_min_length 10;
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_vary on;
gzip_static on;
gzip_proxied any;
gzip_disable "MSIE [1-6]\."

I guess the configuration is crystal clear and it explains everything in pure english, the notable part might be gzip_comp_level which describes the compression level of gzip, it can be between 1 to 9 with 9 being best for only the file compression as of the time of writing, but we have to consider the fact that Gzip takes more CPU resources while deploying the compression level 9 algorithm and the benefit compared to compression level 5 is too less. So, for overall performance compression level 4 or 5 is the best. This picture may give you some idea about it

Effects of Gzip compression with Nginx

Gzip compression table with Nginx

If you want to see the result of different level of compression then has some valuable benchmark result.

Other thing to consider is gzip_types where you can define the mime types that you want gzip to compress.

Gzip static is the most lovely thing here, usually when you are using gzip it only compresses the file in the client site, but gzip static is different beast. Normally with Gzip the server request works like this:

Browser Makes the Request -> Server Receives the Request

Server compresses the file to serve -> Browser receives compressed file

With each request server has to compress the file, which takes some CPU resources and also increases the latency. But with Gzip static, server stores the compressed file in it and when:

Browser Makes the Request - > Server Receives the Requests

Server Sends the Compressed file -> Browser receives the compresses file

This how more latency/bandwidth will be saved with gzip_static on.

To use gzip_static on, you have to compile Nginx --with-gzip-static