Nginx turns on Gzip compression to dramatically speed up page loading
- 2020-05-15 03:30:08
- OfStack
I just added a 500px photo album plugin to my blog. lightbox introduces many js files and css files.
Environment: Debian 6
1. Vim open the Nginx configuration file
vim /usr/local/nginx/conf/nginx.conf
2. Find the following paragraph and modify it
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
#gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary off;
gzip_disable "MSIE [1-6]\.";
3. Explain 1
Line 1: turn on Gzip
Line 2: no compression critical value, only compression greater than 1K, 1 generally need not be changed
Line 3: buffer, which means, well, forget it
Line 4: if the reverse proxy is used, the terminal communication is HTTP/1.0, and those who need it should not read my popular science. Just comment it out. The default is HTTP/1.1
The fifth line: compression level, 1-10, the greater the number of compression, the better, the longer the time, see the mood to change it
There are two ways to write JavaScript. It is better to write both. Some people always complain that js files are not compressed
Line 7: related to caching services like Squid, on will add "Vary: Accept-Encoding "to Header. I don't need it
Line 8: IE6 is not very friendly to Gzip, don't give it Gzip
4. Save and exit wq and reload Nginx
/usr/local/nginx/sbin/nginx -s reload
5. Test whether Gzip is successfully opened with curl
curl -I -H "Accept-Encoding: gzip, deflate" "http://www.slyar.com/blog/"
HTTP/1.1 200 OK
Server: nginx/1.0.15
Date: Sun, 26 Aug 2012 18:13:09 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.2.17p1
X-Pingback: http://www.slyar.com/blog/xmlrpc.php
Content-Encoding: gzip
Page compression successful
curl -I -H "Accept-Encoding: gzip, deflate" "http://www.slyar.com/blog/wp-content/plugins/photonic/include/css/photonic.css"
HTTP/1.1 200 OK
Server: nginx/1.0.15
Date: Sun, 26 Aug 2012 18:21:25 GMT
Content-Type: text/css
Last-Modified: Sun, 26 Aug 2012 15:17:07 GMT
Connection: keep-alive
Expires: Mon, 27 Aug 2012 06:21:25 GMT
Cache-Control: max-age=43200
Content-Encoding: gzip
The css file was successfully compressed
curl -I -H "Accept-Encoding: gzip, deflate" "http://www.slyar.com/blog/wp-includes/js/jquery/jquery.js"
HTTP/1.1 200 OK
Server: nginx/1.0.15
Date: Sun, 26 Aug 2012 18:21:38 GMT
Content-Type: application/x-javascript
Last-Modified: Thu, 12 Jul 2012 17:42:45 GMT
Connection: keep-alive
Expires: Mon, 27 Aug 2012 06:21:38 GMT
Cache-Control: max-age=43200
Content-Encoding: gzip
The js file was successfully compressed
curl -I -H "Accept-Encoding: gzip, deflate" "http://www.slyar.com/blog/wp-content/uploads/2012/08/2012-08-23_203542.png"
HTTP/1.1 200 OK
Server: nginx/1.0.15
Date: Sun, 26 Aug 2012 18:22:45 GMT
Content-Type: image/png
Last-Modified: Thu, 23 Aug 2012 13:50:53 GMT
Connection: keep-alive
Expires: Tue, 25 Sep 2012 18:22:45 GMT
Cache-Control: max-age=2592000
Content-Encoding: gzip
Image compressed successfully
curl -I -H "Accept-Encoding: gzip, deflate" "http://www.slyar.com/blog/wp-content/plugins/wp-multicollinks/wp-multicollinks.css"
HTTP/1.1 200 OK
Server: nginx/1.0.15
Date: Sun, 26 Aug 2012 18:23:27 GMT
Content-Type: text/css
Content-Length: 180
Last-Modified: Sat, 02 May 2009 08:46:15 GMT
Connection: keep-alive
Expires: Mon, 27 Aug 2012 06:23:27 GMT
Cache-Control: max-age=43200
Accept-Ranges: bytes
Finally, I got a file less than 1K. Since my threshold is 1K, I did not compress it
gzip parameter explanation
gzip on|off
Default: gzip off
Turn on or off the gzip module
gzip_static on|off
nginx for static file processing module
This module can read pre-compressed gz files, which reduces the consumption of CPU resources per request for gzip compression. When this module is enabled, nginx first checks to see if there is an gz ending file requesting a static file, and if so, directly returns the contents of the gz file. In order to be compatible with browsers that do not support gzip, the gzip_static module must be enabled with both the original static file and the gz file. This will greatly increase disk space if you have a large number of static files. We can use the reverse proxy function of nginx to keep only gz files.
You can learn more about google "nginx gzip_static"
gzip_comp_level 4
Default value: 1(suggested to be 4)
gzip compression ratio/compression level, compression level 1-9, the higher the level, the higher the compression rate, of course, the longer the compression time (fast transmission but relatively consuming cpu).
gzip_buffers 4 16k
Default: gzip_buffers 4 4k/8k
Set up the system to get several units of cache to store the compressed result data stream of gzip. For example, 4 4k represents a memory request in units of 4 k and 4 times the original data size in units of 4 k. 4 8k represents the memory request in units of 8k, 4 times the original data size in units of 8k.
If not, the default is to request the same amount of memory space as the original data to store the gzip compression results.
gzip_types mime - type [mime - type...].
Default: gzip_types text/html (js/css files are not compressed by default)
Compression type, match MIME type for compression
You cannot use the wildcard text/* (whether specified or not)text/html is compressed by default
You can refer to conf/ mime.types to set which text file to compress
gzip_min_length 1k
Default: 0, no matter how many pages are compressed
Set the minimum number of page bytes allowed to be compressed, which is obtained from Content-Length in the header header.
It is recommended to set it to the number of bytes greater than 1k. If it is less than 1k, the more pressing it will be. Namely: gzip_min_length 1024
gzip_http_version 1.0|1.1
Default: gzip_http_version 1.1(that is, requests for the HTTP/1.1 protocol are gzip compressed)
Identify the protocol version of http. Since some early browsers or http clients may not support gzip self-decompression, users will see the garbled code, so it is necessary to make some judgments.
Note: gzip is basically unzipped in 99.99% of browsers, so you can leave it at the default.
Suppose we use the default value of 1.1, if we use proxy_pass for reverse proxy, then nginx and the back-end upstream server are communicating with HTTP/1.0 protocol. If we use nginx for Cache Server through reverse proxy, and nginx in the front is not turned on gzip, meanwhile, gzip_http_version is not set to 1.0 on our back-end nginx, so url of Cache will not be gzip compressed
gzip_proxied [off | expired | no - cache | no - store | private | no_last_modified | no_etag | auth | any]...
Default: off
When Nginx is enabled as a reverse proxy, it turns on or off the results returned by the back-end server. The matching premise is that the back-end server must return the header header containing "Via".
off - turns off compression of all proxy result data
expired - enable compression if the header header contains "Expires" header information
no-cache - enable compression if the header header contains "Cache-Control: no-cache" header information
no-store - enable compression if the header header contains "Cache-Control: no-store" header information
private - enable compression if the header header contains "Cache-Control :private" header information
no_last_modified - enable compression if header header does not contain "Last-Modified" header information
no_etag - enable compression if the header header does not contain "ETag" header information
auth - enable compression if the header header contains "Authorization" header information
any - unconditionally enables compression
gzip_vary on
It is related to http header. Add an vary header for proxy server. Some browsers support compression, and some do not
gzip_disable "MSIE [1-6]."
Disable gzip compression for IE6, and IE6 for cup. Of course, IE6 is still widely available, so you can also set it to "MSIE [1-5]."
Some versions of IE6 have poor compression support for gzip, which can cause the page to be suspended. Today, students of the product tested this problem
Later, after debugging, it was found that IE6 was suspended to death after gzip was applied to img. gzip of img was compressed and removed, and then it was normal
In order to ensure that other versions of IE6 do not fail, it is recommended to add the setting of gzip_disable