On the Configuration of Nginx Turning on gzip
- 2021-10-16 05:32:32
- OfStack
The principle of resource compression in nginx is to intercept requests through ngx_http_gzip_module module, and compress gzip for types that need gzip. This module is the default basic, and can be opened directly without recompilation.
Basic configuration
# Open gzipgzip on;
# Enable gzip The smallest file to compress. Files smaller than the set value will not be compressed gzip_min_length 1k;
# gzip Compression level, 1-9 The larger the number, the better the compression, and the more occupied it is CPU Time, which will be explained in detail later gzip_comp_level 1;
# The type of file to compress. javascript There are many forms. Where the value can be found in the mime.types Found it in the file. gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png application/vnd.ms-fontobject font/ttf font/opentype font/x-woff image/svg+xml;
# Whether it is in http header Add to Vary: Accept-Encoding , it is recommended to open gzip_vary on;
# Disable IE 6 gzipgzip_disable "MSIE [1-6]\\.";
# Set the buffer size required for compression gzip_buffers 32 4k;
# Settings gzip Compression for HTTP Protocol version, no load can be used # gzip_http_version 1.0;# Turn on the cache location ~* ^.+\\.(ico|gif|jpg|jpeg|png)$ {
access_log off;
expires 2d;
}
location ~* ^.+\\.(css|js|txt|xml|swf|wav)$ {
access_log off;
expires 24h;
}
location ~* ^.+\\.(html|htm)$ {
expires 1h;
}
location ~* ^.+\\.(eot|ttf|otf|woff|svg)$ {
access_log off;
expires max;
}
# Format # expires 30s;# expires 30m;# expires 2h;# expires 30d;
Detection effect
1. response of nginx Content-Encoding in headers is gzip
2. The returned file size is obviously compressed
Compression description gzip on
Turn on or off gzip default off shutdown code blocks http, server, location, if in location
gzip_buffers
Sets the number and size of buffers used to process request compression. For example, 32 4K means applying for 32 times of memory space according to the size of memory pages (one memory page) in units of 4K (that is, memory pages in one system are 4K). It is recommended to leave this item unset and use the default value.
Syntax: gzip_buffers number size;
Default:
gzip_buffers 32 4k|16 8k;
Context: http, server, location
gzip_comp_level
Set gzip compression level, the lower the level, the faster the compression speed, the smaller the file compression ratio, on the contrary, the slower the speed, the greater the file compression ratio
Syntax: gzip_comp_level level;Default:
gzip_comp_level 1;Context: http, server, location
Not that the higher the compression level, the better. In fact, the compression ability of gzip_comp_level 1 is enough. The higher the later level, the compression ratio actually does not increase much, but it eats the processing performance.
On the other hand, compression 1 must be combined with static resource cache to cache the compressed version, otherwise the server will definitely not be able to eat under high load every time.
gzip_disable
Indicate which UA headers do not use gzip compression through expressions
Syntax: gzip_disable regex ...;
Default: -
Context: http, server,locationThis directive appearedinversion 0.6.23.
gzip_min_length
gzip is used for compression when the returned content is greater than this value, in units of K, and when the value is 0, all pages are compressed.
Syntax: gzip_min_length length;Default:
gzip_min_length 20;Context: http, server, location
gzip_http_version
Used to identify the version of http protocol, early browsers do not support gzip compression, users will see garbled code, so this option is added to support the previous version. By default, gzip compression is not turned on under http/1. 0 protocol.
Syntax: gzip_http_version 1.0 | 1.1;
Default:
gzip_http_version 1.1;
Context: http, server, location
Before the application server, if there is a cluster of Nginx in layer 1 as load balancing, if gzip is not turned on on this layer 1.
If we use proxy_pass for reverse proxy, the communication between nginx and the back-end upstreamserver is in the HTTP/1. 0 protocol by default.
If our Cache Server is also nginx, and the front-end nginx does not turn on gzip.
At the same time, gzip_http_version is not set to 1.0 on our back-end nginx, so url of Cache will not be compressed by gzip.
gzip_proxied
Enabled when Nginx is used as a reverse proxy:
1.off In fact, in fact, the Turn off all agent result data compression
2. expired In fact, in fact, the If header Contains the Expires "Header information, enabling compression
3.no-cache In fact, in fact, the If header Contains the Cache-Control:no-cache "Header information, enabling compression
4.no-store In fact, in fact, the If header Contains the Cache-Control:no-store "Header information, enabling compression
5. private In fact, in fact, the If header Contains the Cache-Control:private "Header information, enabling compression
6. no_last_modified In fact, in fact, the Enable compression, if header Contains the Last_Modified "Header information, enabling compression
7. no_etag In fact, in fact, the Enable compression, if header Contains the ETag "Header information, enabling compression
8. auth In fact, in fact, the Enable compression, if header Contains the Authorization "Header information, enabling compression
9.any In fact, in fact, the Unconditionally compress all result data
Syntax: gzip_proxied off |expired |no-cache |no-store |private |no_last_modified |no_etag |auth |any ...;
Default:
gzip_proxied off;
Context: http, server, location
gzip_types
Set the MIME type to be compressed, and do not compress requests that are not within the set type range
Syntax: gzip_types mime-type ...;
Default:
gzip_typestext/html;
Context: http, server, location
gzip_vary
Adding the response header "Vary: Accept-Encoding" tells the receiver that the sent data has been compressed, and the effect after opening is to add Accept-Encoding: gzip in the response header, which is useful for client browsers that do not support gzip compression.
Syntax: gzip_varyon |off;
Default:
gzip_varyoff;
Context: http, server, location