Explain how to enable file Gzip compression in the configuration of the Nginx server

  • 2020-05-10 23:26:36
  • OfStack

gzip(GNU-ZIP) is a compression technique. After gzip compression, the page size can be reduced to 30% or less, so that users can browse the page more quickly. The compressed pages of gzip need to be supported by both the browser and the server, which is actually server-side compression, which is unzipped and parsed by the browser after it is sent to the browser. Browsers don't need to worry about that, because most of today's huge browsers support parsing gzip pages.
The compressed output of Nginx is implemented by a set of gzip compressed instructions. The relevant instruction is located in http{... .} between two curly braces.
Here's how to configure gzip compression:

1. Vim opens 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: open 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 the explanation, don't change 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, I'll see what happens

Line 8: IE6 is not very friendly to Gzip. Don't give it Gzip

Here we will talk about the configuration parameters of gzip_proxied:


syntax: gzip_proxied off |expired | no-cache |no-store | private |no_last_modified | no_etag |auth | any ...;
default: 
gzip_proxied off;


When Nginx is enabled as a reverse proxy, it turns on or off the result returned by the back-end server. The matching premise is that the back-end server must return the header header containing "Via".

off
Turn 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 the header header does not contain the "Last-Modified" header information
no_etag
Enable compression if the header header does not contain "ETag" header information
auth
Use compression if the header header contains "Authorization" header information
any
Unconditional compression enabled

4, :wq save exit, 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" "//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: //www.slyar.com/blog/xmlrpc.php
Content-Encoding: gzip

Page compression successful


curl -I -H "Accept-Encoding: gzip, deflate" "//www.ofstack.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" "//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" "//www.ofstack.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" "//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


Related articles: