Nginx Gzip Compression Configuration

          With the growth of Nginx, more and more websites are using it, so Nginx optimization has become increasingly important. Today let’s take a look at how Nginx gzip compression actually works.

gzip (GNU-ZIP) is a compression technology. After gzip compression, page size can be reduced to 30% or even less of the original, so users will browse pages much faster. gzip compressed pages require support from both the browser and the server. Essentially, the server compresses the content, and after it is transmitted to the browser, the browser decompresses and parses it. We don’t need to worry about the browser side, because the vast majority of modern browsers support parsing gzipped pages.
Nginx’s compressed output is implemented through a set of gzip compression directives. The relevant directives are placed between the http{….} block.

gzip on;
//This directive enables or disables the gzip module (on/off)

gzip_min_length 1k;
//Sets the minimum page size in bytes allowed for compression. The page size is obtained from the content-length in the header. The default value is 0, meaning pages of any size are compressed. It is recommended to set this to a value greater than 1k, as compressing files smaller than 1k might make them larger.

gzip_buffers 4 16k;
//Sets the number and size of buffers used to store the compressed data stream. 4 16k means apply for memory 4 times the unit size of 16k, based on the original data size in 16k units.

gzip_http_version 1.1;
//Identifies the HTTP protocol version (1.0/1.1)

gzip_comp_level 2;
//gzip compression ratio. 1 provides the smallest compression ratio with the fastest processing speed, while 9 provides the highest compression ratio but the slowest processing speed (faster transfer but more CPU intensive)

gzip_types text/plain application/x-javascript text/css application/xml
//Matches MIME types for compression. Regardless of whether it is specified, the ”text/html” type is always compressed.
gzip_vary on;
//Related to HTTP headers. Adds a Vary header, which is useful for proxy servers. Some browsers support compression while others do not, so to avoid wasting resources by compressing for unsupported ones, the decision whether to compress is made based on the client’s HTTP header.

The Nginx gzip configuration block is as follows:
gzip on;
gzip_min_length 1k;
g

Leave a Comment

Your email address will not be published.