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:
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.