nginx gzip configuration parameters

  • 2020-05-12 06:29:57
  • OfStack

Nginx own http gzip module: / / wiki nginx. org/NginxChsHttpGzipModule, this module supports online real-time compression of output data stream. After a good configuration optimization, can greatly improve the output efficiency of the site.

Example 1


gzip on;
gzip_min_length 1000;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain application/xml;

The built-in variable $gzip_ratio can get the compression ratio of gzip

Instructions:


[#gzip gzip]
[#gzip_buffers gzip_buffers]
[#gzip_comp_level gzip_comp_level]
[#gzip_min_length gzip_min_length]
[#gzip_http_version gzip_http_version]
[#gzip_proxied gzip_proxied]
[#gzip_types gzip_types]

gzip

Syntax: gzip on|off
Default: gzip off
Scope: http, server, location, if (x) location
Turn on or off the gzip module

gzip_buffers

Syntax: gzip_buffers number size
Default: gzip_buffers 4 4k/8k
Scope: http, server, location
The system is set to get several units of cache to store the stream of compressed results from gzip. For example, 4 4k stands for applying memory in units of 4k and 4 times the original data size in units of 4k. 4 8k represents an application for memory in units of 8k, 4 times the original data size in units of 8k.

If not set, the default is to request the same size of memory space as the original data to store the gzip compression results.

gzip_comp_level

Syntax: gzip_comp_level 1.. 9
Default: gzip_comp_level 1
Scope: http, server, location
gzip compression ratio: 1 compression ratio: minimum processing speed; 9 compression ratio: maximum but slowest processing speed (fast transmission but relatively consuming cpu).

gzip_min_length

Syntax: gzip_min_length length
Default: gzip_min_length 0
Scope: http, server, location
Sets 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, no matter how many pages are compressed.
It is recommended to set it to the number of bytes greater than 1k. If it is less than 1k, the more pressing it will be. Namely: gzip_min_length 1024

gzip_http_version

Syntax: gzip_http_version 1.0|1.1
Default: gzip_http_version 1.1
Scope: http, server, location
Identify the protocol version of http. Since some early browsers or http clients may not support gzip self-extracting, users will see the garbled code, so it is necessary to make some judgments. Note: the 21st century is coming, now, except for things like baidu's spiders do not support self-decompression, 99.99% of browsers basically support gzip decompression, so you can not set this value, keep the default system can.

gzip_proxied

Syntax: gzip_proxied [off|expired| no store|private|no_last_modified| auth|any]...
Default: gzip_proxied off
Scope: http, server, location
When Nginx is enabled as a reverse proxy, it turns on or off the results returned by the back-end server. The matching premise is that the back-end server must return the header header containing "Via".

All proxy result data compression is turned off in off
The expired wok enables compression if the header header contains "Expires" header information
Compression is enabled for no-cache, if the header header contains the "Cache-Control: no-cache" header
Compression is enabled for no-store, if the header header contains the "Cache-Control: no-store" header
Compression is enabled for private, if the header header contains the "Cache-Control :private" header
Compression is enabled for no_last_modified, if the header header does not contain "Last-Modified" header information
Compression is enabled for no_etag, if the header header does not contain "ETag" header information
Compression is enabled for auth, if the header header contains "Authorization" header information
Compression is unconditionally enabled in any

gzip_types

Syntax: gzip_types mime-type [mime-type...]
Default: gzip_types text/html
Scope: http, server, location
Match the MIME type for compression, and (whether or not specified) the "text/html" type will always be compressed.
Note: if used as http server, include the file type profile in the master profile


http
{
include conf/mime.types;
...
}

If you want to compress normal file types, write it like this

http
{
include conf/mime.types; gzip on;
gzip_min_length 1000;
gzip_buffers 4 8k;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css text/html application/xml; ...
}

By default, gzip compression for Nginx is turned off
Also, Nginx only compresses text/html by default
So, the instructions to turn on gzip are as follows:

gzip on;
gzip_http_version 1.0;
gzip_disable " MSIE [1-6]. " ;
gzip_types text/plain application/x-javascript text/css text/javascript;

For gzip_types, if you want the image to also turn on gzip compression, use the following:

gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php image/jpeg image/gif image/png;

Note:

1. For the setting of gzip_http_version, the default value is 1.1, which means that the request of HTTP/1.1 protocol will be gzip compressed. If we use proxy_pass for reverse proxy, nginx and the back-end upstream server will communicate using HTTP/1.0 protocol


This module makes it possible to transfer requests to another server.
It is an HTTP/1.0 proxy without the ability for keep-alive requests yet. (As a result, backend connections are created and destroyed on every request.) Nginx talks HTTP/1.1 to the browser and HTTP/1.0 to the backend server. As such it handles keep-alive to the browser.

If we use nginx to do Cache Server through reverse proxy, and the front-end nginx is not turned on gzip, meanwhile, gzip_http_version is not set to 1.0 on our back-end nginx, then url of Cache will not be gzip compressed

2. The setting of gzip_disable is to disable gzip compression of IE6, again because of IE6
IE6 some version of gzip compression support is very bad, will cause the suspended animation of the page, the classmate of the product is tested the problem today, then after debugging, after is found to gzip img IE6 feign death, remove the gzip compression of img after normal, in order to ensure that other IE6 version is not a problem, so I added gzip_disable Settings


Related articles: