In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Recently, there have been some problems in production. Originally, all static resource files were compressed by gzip, but they were suddenly not compressed these days. After a meal of investigation, it was found that there was something wrong with the configuration of Nginx. I took this opportunity to learn more about the Gzip configuration of Nginx.
1. Enable Nginx Gzip1.1 configuration instructions 1.2Parameters explain gzip ongzip_buffersgzip_comp_levelgzip_disablegzip_min_lengthgzip_http_versiongzip_proxiedgzip_typesgzip_vary in detail
1. Nginx enables Gzip
The principle of Nginx to achieve resource compression is to intercept requests through the ngx_http_gzip_module module, and to do gzip,ngx_http_gzip_module for the types that need to do gzip is integrated by Nginx by default, which does not need to be recompiled and can be turned on directly.
1.1 configuration description
The configuration for Nginx to enable Gzip is as follows:
# $gzip_ratio calculates the compression ratio of the request. $body_bytes_sent request size log_format main'$remote_addr-$remote_user [$time_local] "$host"-"$request"'$gzip_ratio-$body_bytes_sent-$request_time'; access_log logs/access.log main; # enables gzip gzip off # enable gzip compression of the minimum files, files less than the set value will not compress gzip_min_length 1k; # gzip compression level, 1-9, the larger the number, the better compression, and the more CPU time it takes. Gzip_comp_level 1; # the types of files to be compressed will be described later. Javascript comes in many 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 application/vnd.ms-fontobject font/ttf font/opentype font/x-woff image/svg+xml; # whether to add Vary: Accept-Encoding to http header, it is recommended to enable gzip_vary on; # disable IE 6 gzip gzip_disable "MSIE [1-6]\."; # set the buffer size required for compression gzip_buffers 324k # set the HTTP protocol version gzip_http_version 1.0 for gzip compression; 1234567891011121314151617181920212223242527282930
The instructions and parameter configuration of gzip are described one by one below.
1.2Parametric detail gzip on
There's nothing to say. Turn gzip on or off.
Syntax: gzip on | off; Default: gzip off;Context: http, server, location, if in location 12345gzip_buffers
Sets the number and size of buffers used to handle request compression. For example, 324K means to apply for 32 times the memory space according to the memory page (one memory page) size of 4K (that is, 4K of memory pages in a system). It is recommended that this item is not set and use the default value.
Syntax: gzip_buffers number size; Default: gzip_buffers 324k | 168k; Context: http, server, location 1234gzip_comp_level
Set the gzip compression level. The lower the compression level, the faster the compression speed, the smaller the file compression ratio, and vice versa, the slower the speed, the greater the file compression ratio.
Syntax: gzip_comp_level level; Default: gzip_comp_level 1; Context: http, server, location 12345
Let's take a script file with a size of 92.6K as an example, as shown below. The last three values represent compression ratio, packet size and average processing time (using ab stress test, 100 users concurrently, / / 10.27.180.75/jquery.js gzip_comp_level 0: 0 ms 94840,63 [ms], 29% gzip_comp_level 1: 2.43 ms 39005,248 [ms], 100% gzip_comp_level 2: 2.51 gzip_comp_level 37743,273 [ms], 100% gzip_comp_level 3 2.57 gzip_comp_level 36849,327 [ms], 100% gzip_comp_level 4; 2.73 Magi 34807,370 [ms], 100% gzip_comp_level 5; 2.80 Magi 33688,491 [ms], 100% gzip_comp_level 6; 2.82 gzip_comp_level 33686,604 [ms], 100% gzip_comp_level 7; 2.82 Jing 33626,659 [ms], 100% gzip_comp_level 8; 2.82 Jing 33626,698 [ms], 100% gzip_comp_level 9 2.82 million 33626,698 [ms], 100%
1234567891011gzip_disable
Use expressions to indicate which UA headers do not use gzip compression
Syntax: gzip_disable regex...; Default:-Context: http, server, locationThis directive appeared in version 0.6.23. 1234gzip_min_length
Gzip is used to compress when the returned content is greater than this value, in K units, and when the value is 0, all pages are compressed.
Syntax: gzip_min_length length; Default: gzip_min_length 20; Context: http, server, location 1234gzip_http_version
Used to identify the version of the http protocol, early browsers do not support gzip compression, users will see garbled, so this option is added to support the previous version. Gzip compression is not enabled by default under the http/1.0 protocol.
Syntax: gzip_http_version 1.0 | 1.1; Default: gzip_http_version 1.1; Context: http, server, location 1234
I think this is out of date in many articles on the Internet, because browsers basically support HTTP/1.1. However, there is a pit that is easy to fall into, which the author found in a strange problem in the production environment:
Why is that?
I believe that some people will enter the pit in the future, for example, if you use Apache ab to do stress testing, if you do not set gzip_http_version to 1.0, you will not be able to suppress the effect of gzip (by the same token). I hope it will be helpful for you to write here.
Gzip_proxied
Enabled when Nginx is used as a reverse proxy:
Off-turn off all proxy result data compression expired-enable compression no-cache if header contains "Expires" header information-enable compression no-store if header contains "Cache-Control:no-cache" header information-enable compression private if header contains "Cache-Control:no-store" header information-enable compression no_last_modified-enable compression if header contains "Cache-Control:private" header information If header contains "Last_Modified" header information, enable compression no_etag-enable compression, if header contains "ETag" header information, enable compression auth-enable compression, if header contains "Authorization" header information, enable compression any-enable unconditional compression of all result data Syntax: gzip_proxied off | expired | no-cache | private | no_last_modified | no_etag | auth | any. Default: gzip_proxied off;Context: http, server, location 1234gzip_types
Set the MIME type that needs to be compressed. If the request is not within the set type, it will not be compressed.
Syntax: gzip_types mime-type...; Default: gzip_types text/html;Context: http, server, location 1234
Some special types need to be explained here, such as the author's company will use "font type" resources, and these resource types are often ignored, and these resources are relatively large, so it is not cost-effective not to be compressed. (please refer to http://www.darrenfang.com/2015/01/setting-up-http-cache-and-gzip-with-nginx/):
Font type extension Content-type.eotapplication/vnd.ms-fontobject.ttffont/ttf.otffont/opentype.wofffont/x-woff.svgimage/svg+xmlgzip_vary
Add the response header "Vary: Accept-Encoding"
Syntax: gzip_vary on | off; Default: gzip_vary off;Context: http, server, location 12345
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.