In the Nginx server configuration file can be configured by the use of gzip, can be configured in the HTTP block, server block or location block settings, Nginx Server can be through the Ngx_http_gzip_module module, NGX_HTTP_ These instructions are analyzed and processed by the Static_module module function ngx_http_gunzip_module module.
One: Ngx_http_gzip_module module processing Instructions:
1. Gzip:
To turn gzip compression on or off, use the following:
Gzip on | off; #默认为off
2, Gzip_buffers:
Used to specify the size of the cache space used by the Gzip feature to compress files, the syntax structure is:
Numner | size 0.7. 8, the default number * Siez has a value of 128, where size takes a page of memory pages in the system, 4k or 8k, etc.
Gzip_buffers 4k | 8k;
3, Gzip_comp_level
Specify GZIP compression level, level 1-9, default is 1, the higher the compression rate, the smaller the compressed file, but the more CPU, the smaller the compression rate, the larger the compressed file, but save the CPU.
Gzip_comp_level 4; #推荐先设置为中间的值, like 4 or 5.
4, Gzip_disbale
Select the open or close gzip command for different client initiated requests, followed by the name of the browser, with the following usage:
Gzip_disable msie[1-6]\. #使用正则表达式可以匹配UC字符串中MSIE 1-6 browsers, these three browsers will not compress the packets after initiating the request.
5, Gzip_http_verson
Some early browsers or HTTP clients may not support gzip compression, so some customers will sometimes see garbled, that is, the client is not able to decompress, so can be based on different versions of the HTTP protocol to open or close the GZIP function, the use of the following:
1.0 1.1; Compression of requests #对使用htto协议1.0 and 1.1
6, Gzip_min_length
Set the minimum compressed packet size, and packets smaller than this value will not be compressed, since a large amount of small data compression does not have a noticeable effect and consumes CPU resources, so you can set the minimum amount of data to compress, using the following:
#默认为20, it is recommended to set bit 1KB, if set to 0 means all packets are compressed
7, Gzip_proxine
When the Nginx server acts as a reverse proxy, it is used to set the Nginx server to gzip compress the results returned by the backend, using the following:
Gzip_proxied off | Any #off为关闭, any to compress the data returned by all back-end servers.
8, Gzip_types
Set the Nginx server to open or close the GZIP function selectively depending on the type of response page, using the following:
Gzip_types text/plain application/x-javascript text/css application/xml; #一般情况下如此设置
9, Gzip_vary
Used to set whether to send a response header with the "vary:accept-encoding" header domain when using the gzip feature, the primary function of the header domain is to tell the client that the data has been compressed on the server and is set to off by default, with the following usage:
Gzip_vary on| off; The Add_header directive #默认为off还可以使用Nginx configured enforcesthe same effect by adding "vary:accept-encoding" to the response header of the Nginx server.
II: Ngx_http_gzip_static_module module:
This module assists in searching and sending data that is compressed by gzip, which is stored on the server with *.gz as the suffix, if the data requested by the client is compressed before, and the client's browser supports compression, the compressed data will be fan Hu directly, the module uses static encoding, In the HTTP response header contains the Content-length header field to indicate the length of the message, for the server can determine the length of the response data, and ngx_http_gzip_module using chunked encoding dynamic compression, Mainly used for the server can not determine the length of the response data, such as the download of large files, such as the situation, the real-time generation of data length, the use of the same as Ngx_http_gzip_module, as follows:
1, Gzip_static
Gzip_static off | On | Always #on为开启并检查客户端浏览器是否中吃gzip压缩功能, off is off, always sends gzip compressed files without checking whether the browser supports gzip compression
Three: the Ngx_http_gunzip_module module processing two instructions
1, Gunzip
Set the Nginx server to return the extracted data to clients that do not support gzip, and if the client's browser supports compression still returns the compressed data, and can respond simultaneously to the browser process compression that supports gzip, there is a browser that can respond without gzip support.
Nginx Four: The compression function of Nginx server