Introduction to Gzip compression configuration in Nginx

  • 2020-05-07 20:58:04
  • OfStack

With the development of nginx, more and more websites use nginx, so the optimization of nginx is becoming more and more important.

gzip(GNU-ZIP) is a compression technology. 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 require support from both the browser and the server, which is essentially server-side compression, which is then unzipped and parsed by the browser. Browsers don't need to worry about that, because most of today's huge browsers support parsing gzip pages.


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 command is used to turn on or off gzip The module (on/off) gzip_min_length 1k;
// Sets the minimum number of page bytes allowed to be compressed and the number of page bytes from header Head to content-length Get in. The default value is 0 , no matter how many pages are compressed. It is recommended to set to greater than 1k The number of bytes is less than 1k It's going to get more and more stressful. gzip_buffers 4 16k;
// Set up the system to get several units of cache for storage gzip Compression results data stream. 4 16k Representatives to 16k Is the unit that installs the raw data size 16k As the unit of 4 Multiple request memory. gzip_http_version 1.1;
// identify http Protocol version (1.0/1.1) gzip_comp_level 2;
//gzip Compression ratio, 1 Minimum compression ratio, fastest processing speed, 9 The compression ratio is the largest but the processing speed is the slowest ( Fast transmission but expensive cpu) gzip_types text/plain application/x-javascript text/css application/xml
// matching mime Type is compressed, whether or not specified , " text/html "Types are always compressed.
gzip_vary on;
// and http Heads matter. Add one vary Header for proxy server, some browsers support compression, some do not, so avoid wasting unsupported also compression, so according to the client HTTP Head to determine if compression is needed


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 6;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;


Related articles: