As the content of the site continues to grow, and the content and functionality of our site become colorful, there will be a problem----our website will be significantly slower to load, which is not a pleasant thing for visitors to the site, so how can we optimize the site and speed up the site access? It's impossible to reduce the file content of a website, but we can use a "magic" to make these files smaller, so let's take a look at this wonderful narrowing-----gzip.
Gzip (Gnu-zip) is a compression technology, after its compression, the page size will be reduced to a considerable proportion, it is the principle of compression on the server side, when the compressed packet arrives at the client browser, will be decompressed, restore the original content, compression process, Users don't feel it when they visit the site. So gzip compression requires both browser and server support. However, we do not need to worry about it, because most browsers such as IE, Firefox, Opera, and Google support the resolution of gzip pages.
The compressed output of Nginx is implemented by a set of gzip compression instructions. The relevant instructions for gzip compression output are located in http{...} Between the two curly braces:
gzip on;
Gzip_min_length 1k;
Gzip_buffers 4 16k;
Gzip_http_version 1.1;
Gzip_comp_level 2;
Gzip_types text/plain application/x-javascript text/css application/xml;
directive meaning:
GZIP Instructions:
Syntax: gzip On|off
Default value: gzip off
Instructions are used to turn the Gzip module on or off.
Gzip_buffers directive:
Syntax: gzip_buffers number Size
Default value: Gzip_buffer 4 4k/8k
Sets the system to obtain a compressed result data stream for several units of cached user storage gzip. For example, 4 4k represents 4 times times the original data size in 4k, in 4k, for memory. 4 8k represents the memory in 8k, 4 times times the original data in 8k units.
If not set, the default value is to request memory space of the same size as the original data to store gzip compression results.
Gzip_comp_level directive
Syntax: Gzip_comp_level [1-9]
Default value: Gzip_comp_level 1
gzip compression ratio, 1: Compression ratio is minimal, processing speed is fastest, 9: Compression ratio is maximum but processing speed is slowest (transmission is faster but consumes CPU). The general setting is 3.
GZIP_MIN_LENGTH directive
Syntax: gzip_min_length length
Default value: Gzip+min_length 0
Sets the minimum number of bytes of pages allowed to compress, and the number of page bytes is obtained from the header's content-length. The default value is 0, regardless of the number of pages in most compression. It is recommended to set the number of bytes greater than 1k, which is less than 1k, and may be more pressing. namely: Gzip_min_length 1024.
gzip_http_version directive
Syntax: Gzip_http_version 1.0|1.1
Default value: Gzip_http_version 1.1
Identifies the protocol version of HTTP. Because of some early browsers or HTTP clients, Ken can not support gzip self-extracting, users will see garbled, so make some judgments or necessary.
gzip_proxied directive
Syntax: Gzip_proxied[off|expired|no-cache|no-store|private|no_last_modified|no_etag|auth|any ...]
Default value: Gzip_proxied off
Nginx is used as a reverse proxy, to turn on or off the results returned by the backend server, matching the premise that the backend server must return header headers that contain "Via".
off--turns off compression of all proxy result data.
experied--enables compression if header header contains "Expires" headers.
no_cache--enables compression if header header contains "Cache-control:no-cache" headers.
no-store--enables compression if header header contains "Cache-control:no-store" headers.
private--enables compression if header header contains "Cache-control:private" headers.
no_last_modified--enables compression if header header does not contain "last-modified" headers.
no_etag--enables compression if the header header does not contain "etag" headers.
auth--enables compression if header header contains "Authorization" headers.
any--enable compression unconditionally.
Gzip_types directive
Syntax: Gzip_types mime-type[mime-type ...]
Default value: Gzip_types text/html
The match MIME type is compressed, and the "text/html" type is always compressed (whether or not specified).
Note: If you are using as an HTTP server, wish to include the file type profile in the configuration file
http
{
Include Conf/mime.types;
......
}
If you want to compress the regular file types, you can write the code like this:
http
{
Include Conf/mime.types;
gzip on;
Gzip_mim_length 1000;
Gzip_buffers 4 8k;
Gzip_http_version 1.1;
Gzip_types text/plain application/x-javascript text/css text/html application/xml;
......
}
Gzip_vary on command
and HTTP header has a relationship, add a vary header, to the proxy server, some browsers support compression, and some do not support, so avoid wasting does not support also compression, so according to the client's HTTP header to determine whether the need to compress
References: "Real Nginx"
Nginx Compressed Output configuration