Nginx Gzip module activation and configuration instructions, nginxgzip
1. Set gzip parameters in Nginx
# Enable gzip compression service gzip on; # gzip compression requires applying for temporary memory space, assuming that the size after compression is smaller than or equal to the size before compression. # For example, if the size of the original file is 10 KB, it exceeds 8 KB, so the allocated memory is 8*2 = 16 KB. For example, # The size of the original file is 18 K, and 16 K is obviously not enough. Therefore, the memory size is 8*2*2 = 32 K. # If it is not set, the default value is to apply for a memory space of the same size as the original data to store the gzip compression results. # Set the system to obtain several units of cache for storing gzip compressed result data streams. # For example, 4 kb indicates that the memory is 4 kb, and 4 times the size of the raw data is 4 kb. #4 8 k indicates that the memory is 4 times the size of raw data in 8 k. # If it is not set, the default value is to apply for a memory space of the same size as the original data to store the gzip compression results. Gzip_buffers 2 8 k; # nginx processing module for static files. # This module can read pre-compressed gz files, which can reduce the CPU resource consumption for gzip compression each request. # After this module is enabled, nginx first checks whether there is a file ending with gz requesting a static file. If yes, the content of this gz file is directly returned. # To be compatible with browsers that do not support gzip, the original static files and gz files must be retained when the gzip_static module is enabled. # In this case, disk space will be greatly increased when a large number of static files exist. We can use nginx's reverse proxy function to retain only the gz file. Gzip_static on | off # The minimum file that enables gzip compression. files smaller than the set value will not be compressed by gzip_min_length 1 k; # the http protocol version based on gzip compression. The default value is HTTP 1.1 gzip_http_version 1.1; # gzip compression level, 1-10. The larger the number, the better the compression, and the more CPU usage, the more detailed descriptions will be provided later; # Type of the Content-Type Header that requires gzip compression. It is recommended that JavaScript, text, css, xml, and json files be compressed. # The image is unnecessary. The gif and jpge files are compressed well, and the effect is not good even if they are pressed, it also consumes cpu. # Javascript has multiple forms. The values can be found in the mime. types 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; # default value: off # enable Nginx when it is used as the reverse proxy. enable or disable the results returned by the backend server, and ensure that the backend server must return headers containing ". # Off-Disable compression of All proxy result data # expired-enable compression. If the header contains "Expires" header information # no-cache-enable compression, if the header contains "Cache-Control: no-cache" header information # no-store-enable compression, if the header contains "Cache-Control: no-store "header information # private-enable compression. If the header contains" Cache-Control: private "header information # no_last_modified-enable compression, if the header does not contain the "Last-Modified" header information # no_etag-enable compression, if the header does not contain the "ETag" header information # auth-enable compression, if the header contains "Authorization" header information # any-enable compression unconditionally gzip_proxied [off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any]... # Whether to add Vary: Accept-Encoding to the http header. It is recommended to enable it. # It is related to the http header. Add a vary header for the proxy server. Some browsers support compression, # Some do not support gzip_vary on. Therefore, you can determine whether to compress gzip_vary on based on the HTTP header of the client; # Disable IE 6 gzipgzip_disable "MSIE [1-6] \. ";
After the configuration is complete, restart nginx to take effect.
2. Request and processing of gzip compression by browsers and servers
During the entire request process, when gzip is enabled and gip is not enabled, the http request is consistent with the return process. The difference is that the parameters. When the HTTP gzip function is enabled and the client sends an http request, it will tell the server through the Accept-Encoding attribute in headers that "I support gzip decompression, decompress the format (algorithm) deflate, and sdch is: ". Accept-Encoding: gzip, deflate, and sdch. Note that if the request does not support decompression, Nginx will compress the response data. This also depends on the format of the data returned by Nginx. If the original data format of the returned data matches the configured gzip_types, Nginx will compress the data. Nginx returns response headers. If the data is compressed, gzip will be indicated in the Content-Encoding attribute, indicating the response
The content is compressed, and the original format of the data is indicated in the Content-Type attribute. Finally, the compressed response content is returned to the client, and the client unzips it. Note that there is a deflate and sdch In the headers sent by the client. These are two compression algorithms. If you are interested, you can check the relevant information (I suggest you check them to learn about the Haffman compression algorithm, which is helpful for expanding your architecture)
Finally, the webmasters tool Gzip is used to check whether the compression is successful. You can also check whether gzip is enabled for all types of files on the website with one click.
3. For more information about gzip_comp_level, see. From serverfault
It can be seen that gzip_comp_level is not very effective when it is greater than 2. Therefore, you can set the value to 1 or 2.
4. Handling Fonts
You only need to enable gzip for ttf, otf, and svg fonts to compress gzip for other font formats.
gzip_types font/ttf font/otf image/svg+xml
For the compression effects of various Font types, refer to the following test results:
1. eot font Compression Effect
2. otf font Compression Effect
3. svg font Compression Effect
4. ttf font Compression Effect
5. woff font Compression Effect
We can see that gzip compression on woff and eot is not effective.
Extension |
Compression or not |
Content-type |
. Eot |
No |
Application/vnd. ms-fontobject |
. Ttf |
Yes |
Font/ttf |
. Otf |
Yes |
Font/opentype |
. Woff |
No |
Font/x-woff |
. Svg |
Yes |
Image/svg + xml |