nginx configures gzip compressed pages

  • 2020-05-06 12:16:16
  • OfStack

gzip(GNU-ZIP) is a compression technique. After gzip compression, the page size can be reduced to 30% or less, which makes it much faster for users to browse the page. gzip's compressed pages need to be supported by both the browser and the server, which is actually server-side compression, which is unzipped and parsed by the browser after being sent to the browser.

The compressed output of Nginx is implemented by a set of gzip compression instructions. The relevant instruction is located in http{... .} between two braces.

gzip on;
This directive is used to turn on or off the gzip module (on/off)

gzip_min_length 1k;
Set the minimum number of page bytes allowed to be compressed, which is obtained from content-length in the header header. The default value is 0, regardless of page size. It is recommended to set the number of bytes greater than 1k.

gzip_buffers 4 16k;
The setup system gets several units of cache to store the stream of compressed results from gzip. 4 16k represents the requested memory with 16k as the unit, and the installed original data size is 4 times that of 16k.

gzip_http_version 1.1;
Identify http protocol version (1.0/1.1)

gzip_comp_level 2;
gzip compression ratio, 1 compression ratio minimum processing speed is the fastest, 9 compression ratio maximum but the slowest processing speed (fast transmission but relatively expensive cpu)

gzip_types text/plain application/x-javascript text/css application/xml

Match the mime type for compression, and the "text/html" type will always be compressed whether or not specified.

gzip_vary on;
It is related to http header, add an vary header, for proxy server, some browsers support compression, some do not support, so avoid wasting the unsupported also compression, so according to the client HTTP header to judge, whether to compress.

At the same time, IE6 does not support gizp decompression, so gzip compression function should be turned off under IE6. Using


gzip_disable  " MSIE [1-6]\. " ;

The nginx configuration gzip segment is as follows:


gzip on;
gzip_min_length 1k;
gzip_buffers 16 64k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
gzip_disable  " MSIE [1-6]\. " ;


Related articles: