Enable GZip compression of text-based responses in order to reduce file size and bandwidth. Can have significant impact on the perceived speed of website page loads.

To enable compression use the following code to your configuration file:

gzip on;
gzip_comp_level 2;
gzip_http_version 1.0;
gzip_proxied any;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;

In More Detail

Web servers use gzip to compress data returned to clients. When a gzip-enabled browser sends a request, it adds ‘gzip’ to its Accept-Encoding request header.

When a web server receives the request it processes the response as normal and then checks the Accept-Encoding request header to determine how to encode the response. If the requesting browser supports gzip and the server also supports gzip, it compresses each compressible resource using the gzip algorithm and returns those assets with an added Content-Encoding header, indicating that the resource has been encoded using gzip. The browser then decompresses the compressed content into its original uncompressed format and renders it in the browser.

Note that compression has a cost associated with it. Compression is a CPU-intensive process, and the more you compress a response, the longer it will take to do so. Given this size vs speed trade-off, gzip offers compression levels from 1 to 9, where 1 provides the fastest speed but with a lower compression ratio, and 9 offers the highest compression ratio but at a slower speed. In the rule above, a compression level of 2 is specified, as NGINX has traditionally preferred speed of response over compression ratio.