[Nginx] gzip Compression
There is such a configuration file gzip on # default value: gzip off # enable or disable the gzip module gzip_static off; # nginx's processing module for static files # This module can read pre-compressed gz files, this reduces the CPU resource consumption for gzip compression for each request. After this module is enabled, nginx first checks whether there are files ending with the gz request for static files. If yes, the content of the 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 way, the disk space will be greatly increased when there are a large number of static files. We can use nginx's reverse proxy function to retain only the gz file. # Google "nginx gzip_static" to learn more about gzip_comp_level 4; # default value: 1 (4 is recommended) # gzip compression ratio/compression level, compression level 1-9, the higher the level, the higher the compression ratio, and the longer the compression time (fast transmission but cpu consumption ). Gzip_buffers 4 16 k; # default value: gzip_buffers 4 4 k/8 k # set the system to obtain several units of cache for storing gzip compressed result data streams. For example, 4 k indicates that 4 k is used as the unit, and 4 times the size of raw data is used as the unit to apply for memory. 4 8 k represents 8 k as the unit, according to the size of raw data 4 times the size of 8 k applied for memory. # 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_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; # gzip_types mime-type [mime-type...]; # default value: gzip_types text/html (js/css files are not compressed by default) # compression type, compression matching MIME type # wildcard text/* cannot be used # (whether or not specified) text/html has been compressed by default. # For details about which types of compressed text files are set, refer to conf/mime. types gzip_min_length 1 k; # default value: 0, no matter how many pages are compressed # set the minimum number of page bytes that can be compressed, the number of page bytes is from the Content-Leng in the header Th. # It is recommended to set the size to 1 kb. smaller than 1 kb may increase the pressure. That is, gzip_min_length 1024 gzip_http_version 1.0 # default value: gzip_http_version 1.1 (that is, gzip compression is performed for HTTP/1.1 requests) # identify the http protocol version. Some early browsers or http clients may not support gzip self-extracting, and users will see garbled characters, so it is necessary to make some judgments. # Note: 99.99% of browsers basically support gzip decompression, so you do not need to set this value to keep the system default value. # Suppose we use the default value 1.1. If we use proxy_pass for reverse proxy, the nginx and the backend upstream server communicate with each other using the HTTP/1.0 protocol, if we use nginx as the Cache Server through reverse proxy, and the front-end nginx does not enable gzip, at the same time, we do not set gzip_http_version to 1.0 on the backend nginx, then the Cache url will not be gzip compressed gzip_proxied any # gzip_proxied [off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any]...; # 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 the "Authorization" header information # any-unconditional start Use compressed gzip_vary on; # It is related to the http header. Add a vary header for the proxy server. Some browsers support compression, while others do not. Therefore, avoid wasting and compressing unsupported files, therefore, based on the HTTP header of the client, determine whether to compress gzip_disable "MSIE [1-6]. "; # disable gzip compression for IE6 because ie6. Of course, IE6 is still widely used, so you can set it to "MSIE [1-5]. "# Some versions of IE6 do not support gzip compression very well, which may lead to dead pages. To ensure that other IE6 versions do not have any problems, therefore, we recommend that you add gzip_disable settings 1, gzip_comp_level text/html-phpinfo (): 0 55.38 KiB (100.00% of original size) 1 11.22 KiB (20.26% of original size) 2 10.89 KiB (19.66% of original size) 3 10.60 KiB (19.14% of original size) 4 10.17 KiB (18.36% of original size) 5 9.79 KiB (17.68% of original size) 6 9.62 KiB (17.37% Of original size) 7 9.50 KiB (17.15% of original size) 8 9.45 KiB (17.06% of original size) 9 9.44 KiB (17.05% of original size) application/x-javascript-jQuery 1.8.3 (Uncompressed): 0 261.46 KiB (100.00% of original size) 1 95.01 KiB (36.34% of original size) 2 90.60 KiB (34.65% of original size) 3 87.16 KiB (33.36% of original size) 4 81.89 KiB (31.32% of original size) 5 79.33 KiB (30.34% Original size) 6 78.04 KiB (29.85% of original size) 7 77.85 KiB (29.78% of original size) 8 77.74 KiB (29.73% of original size) 9 77.75 KiB (29.74% of original size) the CPU usage is not considered here. The ideal compression level is between 4-6. 2. gzip_static 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 are files ending with the gz request for static files. If yes, the content of the 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 way, the disk space will be greatly increased when there are a large number of static files. We can use nginx's reverse proxy function to retain only the gz file. # Google "nginx gzip_static" to learn more about using PHP pre-compression:
function gzip_static($path){ if ((extension_loaded('zlib') === true) && (is_file($path) === true)) { $levels = array(); $content = file_get_contents($path); foreach (range(1, 9) as $level) { $levels[$level] = strlen(gzencode($content, $level)); } if ((count($levels = array_filter($levels)) > 0) && (min($levels) < strlen($content))) { if (file_put_contents($path . '.gz', gzencode($content, array_search(min($levels), $levels)), LOCK_EX) !== false) { return touch($path . '.gz', filemtime($path), fileatime($path)); } } } return false;}
3. Enable Cache
location ~* \.(ico|jpe?g|gif|png|bmp|swf|flv)(\?[0-9]+)?$ { valid_referers none blocked www.xx.com xx.com expires 30d; log_not_found off; access_log off;} location ~* \.(js|css)$ { expires 7d; log_not_found off; access_log off;} location = /(favicon.ico|roboots.txt) { access_log off; log_not_found off;} location ~* \.(htacess|svn|tar.gz|tar|zip|sql) { return 404;}location ~* \.(eot|ttf|otf|woff|svg)$ { access_log off; expires max;}
4. gzip_vary to understand the role of Vary, you must first understand the HTTP content negotiation mechanism. Sometimes, the same URL can provide multiple different documents, which requires a mechanism to select the most appropriate version between the server and the client, this is the new feature of the content negotiation vary mechanism in http1.1 protocol, so that the server can return different content to the user according to the user request, currently, the most widely used vary in the Internet is the support for compression. For example, when a user request carries the header "Accept-Encoding: gzip, deflate ", indicates that the user expects to get the compressed content from the server, so that the server will give the user a file containing the Compressed Content and carry the vary Header "vary: Accept-Encoding" header, and the "Content-Encoding: gzip" header. If a user requests non-compressed Content, the origin site can also give the user non-compressed Content. This problem is often caused by the cache service. For example, after squid is enabled, Vary: Accept-Encoding gzip_vary on will be added to the response header, and curl is used for verification.
[root@AY ~]# curl -I -v http://common.cnblogs.com/script/jquery.js* About to connect() to common.cnblogs.com port 80 (#0)* Trying 42.121.254.191... connected* Connected to common.cnblogs.com (42.121.254.191) port 80 (#0)> HEAD /script/jquery.js HTTP/1.1> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2> Host: common.cnblogs.com> Accept: */*>< HTTP/1.1 200 OKHTTP/1.1 200 OK< Date: Sun, 22 Nov 2015 11:21:55 GMTDate: Sun, 22 Nov 2015 11:21:55 GMT< Content-Type: application/javascriptContent-Type: application/javascript< Content-Length: 94020Content-Length: 94020< Connection: keep-aliveConnection: keep-alive< Vary: Accept-EncodingVary: Accept-Encoding< Cache-Control: public,max-age=25920000Cache-Control: public,max-age=25920000< Last-Modified: Fri, 15 Feb 2013 03:06:57 GMTLast-Modified: Fri, 15 Feb 2013 03:06:57 GMT< Accept-Ranges: bytesAccept-Ranges: bytes< ETag: "7468b58329bce1:0"ETag: "7468b58329bce1:0"<* Connection #0 to host common.cnblogs.com left intact* Closing connection #0