1. http_gzip_module

1.1 gzip

  • 指令

    1. Syntax: gzip on | off ;
    2. Default: gizp off ;
    3. Context: http,server,location,if in location
  • 说明

控制是否打开gzip压缩功能

1.2. gzip_buffers

  • 指令

    1. Syntax: gzip_buffers number size ;
    2. Default: gzip_buffers 32 4k|16 8k ;
    3. Context: http,server,location
  • 说明

指定用于压缩响应的缓冲区大小,建议为内存页大小的整数倍,在0.7.28之后,默认为32 4k或者16 8k,具体取决于平台。一般不用设置。

1.3. gzip_comp_level

  • 指令

    1. Syntax: gzip_comp_level [1-9] ;
    2. Default: gzip_comp_level 1 ;
    3. Context: http,server,location
  • 说明

压缩比例可以是 1-9,数值越大,压缩率越高,压缩后文件也越小,但是CPU资源消耗越大。如果并发小,CPU资源充足,设置为6-8,反之2-3即可。

1.4. gzip_disable

  • 指令

    1. Syntax: gzip_disable regex ... ;
    2. Default: Close
    3. Context: http,server,location
  • 说明

    该指令在0.6.23版本之后出现。根据请求头中的 User-Agent 字段的值来判断是否开启压缩,一般针对 IE 6及以下版本关闭压缩,也可以不写,毕竟使用IE6情况过少。
    一般使用 “MISE [4-6].“ 表示IE 6及更老的版本,使用”msie6” 代替”MISE [4-6].“速度更快,减少匹配时间。

    1.5. gzip_http_version

  • 指令

    1. Syntax: gzip_http_version 1.0|1.1 ;
    2. Default: gzip_http_version 1.1 ;
    3. Context: http,server,location
  • 说明

默认指定HTTP/1.0不开启gzip压缩,一般无需配置。

1.6. gzip_min_length

  • 指令

    1. Syntax: gzip_min_length length ;
    2. Default: gzip_min_length 20 ;
    3. Context: http,server,location
  • 说明

设置被压缩报文的最小长度,其值取自于Context-Length.建议设置为1024,过小的文件可能会越压越大!

1.7. gzip_proxied

  • 指令

    1. Syntax: gzip_proxied off|expired|no-cache|no-store|private|no_last_modified|no_etag|auth|any
    2. Default: gzip_prixied off ;
    3. Context: http,server,location
  • 说明

指定代理中的gzip配置,在测试环境中未搞明白机制,暂时先放一放。

1.8. gzip_types

  • 指令

    1. Syntax: gzip_types mime-type ... ;
    2. Default: gzip_types text/html ;
    3. Context: http,server,location
  • 说明

指定压缩的MIME类型,* 表示所有类型都压缩,一般建议压缩文本类型,比如html,css,js,xml,json等。图片类压缩效果比较差

1.9. gzip_vary

  • 指令

    1. Syntax: gzip_vary on | off ;
    2. Default: gzip_vary off ;
    3. Context: http,server,location
  • 说明

如果开启了gzip,gzip_static,gunzip等指令,将在响应头中显示 “Vary: Accept-Encoding” 字段

2. http_gzip_static_module

2.1. gzip_static

  • 指令

    1. Syntax: gzip_static on|off|always ;
    2. Default: gzip_static off ;
    3. Context: http,server,location
  • 说明

该指令对于大文件下载非常有用,通过预先在资源目录下对需要下载的资源进行gzip压缩,生成.gz结尾的压缩文件,nginx直接将.gz文件返回给客户端。
该指令在使用时,会考虑到 gzip_http_version,gzip_proxied,gzip_disable和gzip_vary指令。
在1.3.6版本后,增加了always选项,设置该选项后,将不再检查客户端是否支持gzip压缩,对于目录下没有未压缩的文件或者开启了http_gunzip_module时很有用。

3. http_gunzip_module

3.1. gunzip

  • 指令

    1. Syntax: gunzip on|off ;
    2. Default: gunzip off ;
    3. Context: http,server,location
  • 说明

针对不支持gzip压缩的客户端,nginx服务器将解压响应资源,并将解压后的内容返回给客户端。
如果启用,该指令还会通过以下指令判断客户端是否支持gzip压缩:gzip_http_version,gzip_proxied,gzip_disable

3.2. gunzip_buffers

  • 指令

    1. Syntax: gunzip_buffers number size ;
    2. Default: gunzip_buffers 32 4k|16 8k ;
    3. Context: http,server,location
  • 说明

与gzip_buffers类似,一般无需设置

3.2. 反向代理中gunzip效果

配置: 后端的nginx,httpd均开启压缩;前端gzip on时,对IE 6及以下版本不压缩

| nginx | proxy | gzip off ; gunzip off | gzip on ; gunzip off | gzip off ; gunzip on | gzip on ; gunzip on | | —- | —- | —- | —- | —- | —- | | nginx | IE 6 | gunzip | gunzip | gunzip | gunzip | | nginx | IE 9 | gunzip | gzip | gunzip | gzip | | httpd | IE 6 | gzip | gzip | gunzip | gunzip | | httpd | IE 9 | gzip | gzip | gzip | gzip |

从上述结论可以看到: 针对反向代理的情况,直接使用 gunzip on ;一键解决问题。或者后端服务器不压缩,全部交由代理服务器压缩。

4. 案例

4.1. gzip案例

4.1.1. 配置项

[root@centos-81 ~]# cat /etc/nginx/nginx.conf

  1. gzip on ;
  2. gzip_min_length 1024 ;
  3. gzip_types text/css text/plain application/javascript application/xml image/gif image/jpeg image/png;
  4. gzip_disable msie6 ;
  • 开启gzip压缩
  • 最小压缩长度 1024
  • 支持图片和文本(text/html 没必要写,因为写了会提示nginx: [warn] duplicate MIME type “text/html” in /etc/nginx/nginx.conf)
  • IE 6及以下版本不压缩
  • 压缩级别1
  • 对http/1.1压缩,http/1.0不压缩

    4.1.2. 针对资源类型测试

    [root@centos-50 ~]# curl -I -H “Accept-Encoding: gzip, deflate” -A “User-Agent:Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0” 192.168.1.81/index.htm ## text/html is compress defalut.

    1. HTTP/1.1 200 OK
    2. Server: nginx/1.14.2
    3. Date: Sat, 29 Dec 2018 03:46:00 GMT
    4. Content-Type: text/html
    5. Last-Modified: Tue, 14 Nov 2017 10:36:39 GMT
    6. Connection: keep-alive
    7. ETag: W/"5a0ac737-5dc8"
    8. Content-Encoding: gzip

    [root@centos-50 ~]# curl -I -H “Accept-Encoding: gzip, deflate” -A “User-Agent:Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0” http://192.168.1.81/gtzx/tpxw/201705/W020170801791558756653.jpeg

    1. HTTP/1.1 200 OK
    2. Server: nginx/1.14.2
    3. Date: Sat, 29 Dec 2018 03:50:16 GMT
    4. Content-Type: image/jpeg
    5. Last-Modified: Tue, 01 Aug 2017 14:08:35 GMT
    6. Connection: keep-alive
    7. ETag: W/"59808b63-385800"
    8. Content-Encoding: gzip

    [root@centos-50 ~]# curl -I -H “Accept-Encoding: gzip, deflate” -A “User-Agent:Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1;Trident/5.0” http://192.168.1.81/a.pdf

    1. HTTP/1.1 200 OK
    2. Server: nginx/1.14.2
    3. Date: Sat, 29 Dec 2018 03:53:49 GMT
    4. Content-Type: application/pdf
    5. Content-Length: 192088019
    6. Last-Modified: Thu, 01 Mar 2018 12:22:50 GMT
    7. Connection: keep-alive
    8. ETag: "5a97f09a-b7307d3"
    9. Accept-Ranges: bytes

    4.1.3. 针对user-agent测试

    [root@centos-50 ~]# curl -I -H “Accept-Encoding: gzip, deflate” -A “User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)” 192.168.1.81/gtzx/tpxw/201705/W020170801791558756653.jpeg # IE 6

    1. HTTP/1.1 200 OK
    2. Server: nginx/1.14.2
    3. Date: Sat, 29 Dec 2018 03:56:11 GMT
    4. Content-Type: image/jpeg
    5. Content-Length: 3692544
    6. Last-Modified: Tue, 01 Aug 2017 14:08:35 GMT
    7. Connection: keep-alive
    8. ETag: "59808b63-385800"
    9. Accept-Ranges: bytes

    [root@centos-50 ~]# curl -I -H “Accept-Encoding: gzip, deflate” -A “User-Agent:Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0” 192.168.1.81/gtzx/tpxw/201705/W020170801791558756653.jpeg # IE 9

    1. HTTP/1.1 200 OK
    2. Server: nginx/1.14.2
    3. Date: Sat, 29 Dec 2018 03:56:27 GMT
    4. Content-Type: image/jpeg
    5. Last-Modified: Tue, 01 Aug 2017 14:08:35 GMT
    6. Connection: keep-alive
    7. ETag: W/"59808b63-385800"
    8. Content-Encoding: gzip

    [root@centos-50 ~]# curl -I -H “Accept-Encoding: gzip, deflate” -A “User-Agent:Opera/9.80 (Windows NT 6.1; U; zh-cn) Presto/2.9.168 Version/11.50” 192.168.1.81/gtzx/tpxw/201705/W020170801791558756653.jpeg # Opera

    1. HTTP/1.1 200 OK
    2. Server: nginx/1.14.2
    3. Date: Sat, 29 Dec 2018 03:57:24 GMT
    4. Content-Type: image/jpeg
    5. Last-Modified: Tue, 01 Aug 2017 14:08:35 GMT
    6. Connection: keep-alive
    7. ETag: W/"59808b63-385800"
    8. Content-Encoding: gzip

    [root@centos-50 ~]# curl -I -H “Accept-Encoding: gzip, deflate” -A “User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1” 192.168.1.81/gtzx/tpxw/201705/W020170801791558756653.jpeg # chrome

    1. HTTP/1.1 200 OK
    2. Server: nginx/1.14.2
    3. Date: Sat, 29 Dec 2018 03:58:37 GMT
    4. Content-Type: image/jpeg
    5. Last-Modified: Tue, 01 Aug 2017 14:08:35 GMT
    6. Connection: keep-alive
    7. ETag: W/"59808b63-385800"
    8. Content-Encoding: gzip

    4.1.4. gzip_proxied测试

    在官方文档中存在以下描述:
    未命名图片.png
    理论上来说:默认情况下针对反向代理的响应,应该不会进行gzip压缩,但是测试结果与其相悖:
    [root@centos-50 ~]# curl -I -H “Accept-Encoding: gzip, deflate” -A “User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1” http://192.168.1.81/gtwzqxxgk/imagesls/xxgk_sxzy.css

    1. HTTP/1.1 200 OK
    2. Server: nginx/1.14.2
    3. Date: Sat, 29 Dec 2018 04:05:33 GMT
    4. Content-Type: text/css
    5. Connection: keep-alive
    6. Last-Modified: Tue, 09 Jan 2018 03:07:43 GMT
    7. ETag: W/"106f-5624f37e13dc0"
    8. Access-Control-Allow-Origin: http://www.jsmlr.gov.cn:80/gtxx/
    9. Content-Encoding: gzip

    目前能想到的解决方案: 在反向代理的location中使用 gzip off
    [root@centos-81 ~]# cat /etc/nginx/conf.d/localhost.conf

    1. location ~ ^/gt[a-z]+ {
    2. gzip off ;
    3. proxy_pass http://www.jsmlr.gov.cn ;
    4. }

    [root@centos-50 ~]# curl -I -H “Accept-Encoding: gzip, deflate” -A “User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1” http://192.168.1.81/gtwzqxxgk/imagesls/xxgk_sxzy.css

    1. HTTP/1.1 200 OK
    2. Server: nginx/1.14.2
    3. Date: Sat, 29 Dec 2018 04:07:54 GMT
    4. Content-Type: text/css
    5. Content-Length: 4207
    6. Connection: keep-alive
    7. Last-Modified: Tue, 09 Jan 2018 03:07:43 GMT
    8. ETag: "106f-5624f37e13dc0"
    9. Accept-Ranges: bytes
    10. Access-Control-Allow-Origin: http://www.jsmlr.gov.cn:80/gtxx/

    4.2. gzip static module案例

    针对附件下载的location使用该方式可以降低cpu消耗,服务端直接将预压缩文件(.gz)传给客户端。

    4.2.1. 配置

    [root@centos-81 ~]# cat /etc/nginx/conf.d/localhost.conf

    1. location /download {
    2. root /usr/share/nginx/html ;
    3. gzip_static on ;
    4. sendfile on ;
    5. }

    [root@centos-81 download]# gzip -c a.pdf -9 > a.pdf.gz ## 测试文件

    4.2.2. 测试

  • 支持gzip的user-agent

[root@centos-50 ~]# curl -H “Accept-Encoding: gzip, deflate” -A “User-Agent:Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0” http://192.168.1.81/download/a.pdf | gunzip > a.pdf ## receive compress file.

  1. % Total % Received % Xferd Average Speed Time Time Time Current
  2. Dload Upload Total Spent Left Speed
  3. 100 178M 100 178M 0 0 98.9M 0 0:00:01 0:00:01 --:--:-- 98.9M
  • 不支持gzip的user-agent

[root@centos-50 ~]# curl -H “Accept-Encoding: gzip, deflate” -A “User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)” http://192.168.1.81/download/a.pdf > b.pdf ## receive uncompress file.

  1. % Total % Received % Xferd Average Speed Time Time Time Current
  2. Dload Upload Total Spent Left Speed
  3. 100 183M 100 183M 0 0 162M 0 0:00:01 0:00:01 --:--:-- 162M
  • 移除源文件的后,不支持的user-agent将收到404,nignx不会解压.gz的资源,即使使用gunzip

[root@centos-50 ~]# curl -H “Accept-Encoding: gzip, deflate” -A “User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)” http://192.168.1.81/download/a.pdf ## raise 404.

  1. <html>
  2. <head><title>404 Not Found</title></head>
  3. <body bgcolor="white">
  4. <center><h1>404 Not Found</h1></center>
  5. <hr><center>nginx/1.14.2</center>
  6. </body>
  7. </html>
  8. <!-- a padding to disable MSIE and Chrome friendly error page -->
  9. <!-- a padding to disable MSIE and Chrome friendly error page -->
  10. <!-- a padding to disable MSIE and Chrome friendly error page -->
  11. <!-- a padding to disable MSIE and Chrome friendly error page -->
  12. <!-- a padding to disable MSIE and Chrome friendly error page -->
  13. <!-- a padding to disable MSIE and Chrome friendly error page -->

4.3. gunzip案例

4.3.1. 代理服务的结构与配置

前端 location 后端服务器 后端服务器gzip开关
nginx proxy/ nginx gzip on
nginx httpd/ httpd gzip on

[root@centos-81 conf.d]# cat /etc/nginx/nginx.conf # 前端nginx.conf配置

  1. gzip on ;
  2. gzip_min_length 1024 ;
  3. gzip_types text/css text/plain application/javascript application/xml image/gif image/jpeg image/png;
  4. gzip_disable msie6 ;
  5. include /etc/nginx/conf.d/*.conf;

4.3.2. gzip off ; gunzip off

  • 前端location中gizp与gunzip配置

    1. location /proxy {
    2. proxy_pass http://192.168.1.50 ;
    3. gzip off ;
    4. gunzip off ;
    5. }
    6. location /httpd {
    7. proxy_pass http://192.168.1.50:8080 ;
    8. gzip off ;
    9. gunzip off ;
    10. }
  • 后端nginx

[root@centos-81 conf.d]# curl -I -H “Accept-Encoding: gzip, deflate” -A “User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)” 192.168.1.81/proxy/a.txt ## IE 6 (unzip) —OK

  1. HTTP/1.1 200 OK
  2. Server: nginx/1.14.2
  3. Date: Sun, 30 Dec 2018 08:33:36 GMT
  4. Content-Type: text/plain
  5. Content-Length: 99727
  6. Connection: keep-alive
  7. Last-Modified: Fri, 28 Dec 2018 01:48:35 GMT
  8. ETag: "5c2580f3-1858f"
  9. Accept-Ranges: bytes

[root@centos-81 conf.d]# curl -I -H “Accept-Encoding: gzip, deflate” -A “User-Agent:Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1 Trident/5.0” 192.168.1.81/proxy/a.txt## IE 9 (unzip) —OK

  1. HTTP/1.1 200 OK
  2. Server: nginx/1.14.2
  3. Date: Sun, 30 Dec 2018 08:04:49 GMT
  4. Content-Type: text/plain
  5. Content-Length: 99727
  6. Connection: keep-alive
  7. Last-Modified: Fri, 28 Dec 2018 01:48:35 GMT
  8. ETag: "5c2580f3-1858f"
  9. Accept-Ranges: bytes
  • 后端httpd:

[root@centos-81 conf.d]# curl -I -H “Accept-Encoding: gzip, deflate” -A “User-Agent:Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0” 192.168.1.81/httpd/a.txt ## IE 6 (zip) —ERROR

  1. HTTP/1.1 200 OK
  2. Server: nginx/1.14.2
  3. Date: Sun, 30 Dec 2018 08:13:41 GMT
  4. Content-Type: text/plain; charset=UTF-8
  5. Content-Length: 13110
  6. Connection: keep-alive
  7. Last-Modified: Fri, 28 Dec 2018 15:57:19 GMT
  8. ETag: "100b25-1858f-57e171e30b7c5"
  9. Accept-Ranges: bytes
  10. Vary: Accept-Encoding
  11. Content-Encoding: gzip

[root@centos-81 conf.d]# curl -I -H “Accept-Encoding: gzip, deflate” -A “User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)” 192.168.1.81/httpd/a.txt ## IE 9 (zip) —OK

  1. HTTP/1.1 200 OK
  2. Server: nginx/1.14.2
  3. Date: Sun, 30 Dec 2018 08:15:36 GMT
  4. Content-Type: text/plain; charset=UTF-8
  5. Content-Length: 13110
  6. Connection: keep-alive
  7. Last-Modified: Fri, 28 Dec 2018 15:57:19 GMT
  8. ETag: "100b25-1858f-57e171e30b7c5"
  9. Accept-Ranges: bytes
  10. Vary: Accept-Encoding
  11. Content-Encoding: gzip

4.3.3. gzip on ; gunzip off

  • 前端location中gizp与gunzip配置

    1. location /proxy {
    2. proxy_pass http://192.168.1.50 ;
    3. # gzip off ;
    4. gunzip off ;
    5. }
    6. location /httpd {
    7. proxy_pass http://192.168.1.50:8080 ;
    8. # gzip off ;
    9. gunzip off ;
    10. }
  • 后端nginx: [root@centos-81 conf.d]# curl -I -H “Accept-Encoding: gzip, deflate” -A “User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)” 192.168.1.81/proxy/a.txt ## IE 6 (unzip) —OK

    1. HTTP/1.1 200 OK
    2. Server: nginx/1.14.2
    3. Date: Sun, 30 Dec 2018 08:18:14 GMT
    4. Content-Type: text/plain
    5. Content-Length: 99727
    6. Connection: keep-alive
    7. Last-Modified: Fri, 28 Dec 2018 01:48:35 GMT
    8. ETag: "5c2580f3-1858f"
    9. Accept-Ranges: bytes

    [root@centos-81 conf.d]# curl -I -H “Accept-Encoding: gzip, deflate” -A “User-Agent:Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0” 192.168.1.81/proxy/a.txt ## IE 9 (zip) —OK

    1. HTTP/1.1 200 OK
    2. Server: nginx/1.14.2
    3. Date: Sun, 30 Dec 2018 08:19:10 GMT
    4. Content-Type: text/plain
    5. Connection: keep-alive
    6. Last-Modified: Fri, 28 Dec 2018 01:48:35 GMT
    7. ETag: W/"5c2580f3-1858f"
    8. Content-Encoding: gzip
  • 后端httpd:

[root@centos-81 conf.d]# curl -I -H “Accept-Encoding: gzip, deflate” -A “User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)” 192.168.1.81/httpd/a.txt ## IE 6 (zip) —ERROR

  1. HTTP/1.1 200 OK
  2. Server: nginx/1.14.2
  3. Date: Sun, 30 Dec 2018 08:20:48 GMT
  4. Content-Type: text/plain; charset=UTF-8
  5. Content-Length: 13110
  6. Connection: keep-alive
  7. Last-Modified: Fri, 28 Dec 2018 15:57:19 GMT
  8. ETag: "100b25-1858f-57e171e30b7c5"
  9. Accept-Ranges: bytes
  10. Vary: Accept-Encoding
  11. Content-Encoding: gzip

[root@centos-81 conf.d]# curl -I -H “Accept-Encoding: gzip, deflate” -A “User-Agent:Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0” 192.168.1.81/httpd/a.txt ## IE 9 (zip) —OK

  1. HTTP/1.1 200 OK
  2. Server: nginx/1.14.2
  3. Date: Sun, 30 Dec 2018 08:21:18 GMT
  4. Content-Type: text/plain; charset=UTF-8
  5. Content-Length: 13110
  6. Connection: keep-alive
  7. Last-Modified: Fri, 28 Dec 2018 15:57:19 GMT
  8. ETag: "100b25-1858f-57e171e30b7c5"
  9. Accept-Ranges: bytes
  10. Vary: Accept-Encoding
  11. Content-Encoding: gzip

4.3.4. gzip off ; gunzip on

  • 前端location中gizp与gunzip配置

    1. location /proxy {
    2. proxy_pass http://192.168.1.50 ;
    3. gzip off ;
    4. gunzip on ;
    5. }
    6. location /httpd {
    7. proxy_pass http://192.168.1.50:8080 ;
    8. gzip off ;
    9. gunzip on ;
    10. }
  • 后端nginx

[root@centos-81 conf.d]# curl -I -H “Accept-Encoding: gzip, deflate” -A “User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)” 192.168.1.81/proxy/a.txt ## IE 6 (unzip) —OK

  1. HTTP/1.1 200 OK
  2. Server: nginx/1.14.2
  3. Date: Sun, 30 Dec 2018 08:24:26 GMT
  4. Content-Type: text/plain
  5. Content-Length: 99727
  6. Connection: keep-alive
  7. Last-Modified: Fri, 28 Dec 2018 01:48:35 GMT
  8. ETag: "5c2580f3-1858f"
  9. Accept-Ranges: bytes

[root@centos-81 conf.d]# curl -I -H “Accept-Encoding: gzip, deflate” -A “User-Agent:Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0” 192.168.1.81/proxy/a.txt ## IE 9 (unzip) —OK

  1. HTTP/1.1 200 OK
  2. Server: nginx/1.14.2
  3. Date: Sun, 30 Dec 2018 08:24:58 GMT
  4. Content-Type: text/plain
  5. Content-Length: 99727
  6. Connection: keep-alive
  7. Last-Modified: Fri, 28 Dec 2018 01:48:35 GMT
  8. ETag: "5c2580f3-1858f"
  9. Accept-Ranges: bytes
  • 后端httpd

[root@centos-81 conf.d]# curl -I -H “Accept-Encoding: gzip, deflate” -A “User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)” 192.168.1.81/httpd/a.txt ## IE 6 (unzip) —OK

  1. HTTP/1.1 200 OK
  2. Server: nginx/1.14.2
  3. Date: Sun, 30 Dec 2018 08:25:48 GMT
  4. Content-Type: text/plain; charset=UTF-8
  5. Connection: keep-alive
  6. Last-Modified: Fri, 28 Dec 2018 15:57:19 GMT
  7. ETag: W/"100b25-1858f-57e171e30b7c5"
  8. Vary: Accept-Encoding

[root@centos-81 conf.d]# curl -I -H “Accept-Encoding: gzip, deflate” -A “User-Agent:Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0” 192.168.1.81/httpd/a.txt ## IE 9 (zip) —OK

  1. HTTP/1.1 200 OK
  2. Server: nginx/1.14.2
  3. Date: Sun, 30 Dec 2018 08:25:57 GMT
  4. Content-Type: text/plain; charset=UTF-8
  5. Content-Length: 13110
  6. Connection: keep-alive
  7. Last-Modified: Fri, 28 Dec 2018 15:57:19 GMT
  8. ETag: "100b25-1858f-57e171e30b7c5"
  9. Accept-Ranges: bytes
  10. Vary: Accept-Encoding
  11. Content-Encoding: gzip

4.3.4. gzip on ; gunzip on

  • 前端location中gizp与gunzip配置

    1. location /proxy {
    2. proxy_pass http://192.168.1.50 ;
    3. gunzip on ;
    4. }
    5. location /httpd {
    6. proxy_pass http://192.168.1.50:8080 ;
    7. gunzip on ;
    8. }
  • 后端ngin

[root@centos-81 conf.d]# curl -I -H “Accept-Encoding: gzip, deflate” -A “User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)” 192.168.1.81/proxy/a.txt ## IE 6 (unzip) —OK

  1. HTTP/1.1 200 OK
  2. Server: nginx/1.14.2
  3. Date: Sun, 30 Dec 2018 08:29:50 GMT
  4. Content-Type: text/plain
  5. Content-Length: 99727
  6. Connection: keep-alive
  7. Last-Modified: Fri, 28 Dec 2018 01:48:35 GMT
  8. ETag: "5c2580f3-1858f"
  9. Accept-Ranges: bytes

[root@centos-81 conf.d]# curl -I -H “Accept-Encoding: gzip, deflate” -A “User-Agent:Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0” 192.168.1.81/proxy/a.txt ## IE 9 (zip) —OK

  1. HTTP/1.1 200 OK
  2. Server: nginx/1.14.2
  3. Date: Sun, 30 Dec 2018 08:30:11 GMT
  4. Content-Type: text/plain
  5. Connection: keep-alive
  6. Last-Modified: Fri, 28 Dec 2018 01:48:35 GMT
  7. ETag: W/"5c2580f3-1858f"
  8. Content-Encoding: gzip
  • 后端httpd

[root@centos-81 conf.d]# curl -I -H “Accept-Encoding: gzip, deflate” -A “User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)” 192.168.1.81/httpd/a.txt ## IE 6 (unzip) —OK

  1. HTTP/1.1 200 OK
  2. Server: nginx/1.14.2
  3. Date: Sun, 30 Dec 2018 08:30:39 GMT
  4. Content-Type: text/plain; charset=UTF-8
  5. Connection: keep-alive
  6. Last-Modified: Fri, 28 Dec 2018 15:57:19 GMT
  7. ETag: W/"100b25-1858f-57e171e30b7c5"
  8. Vary: Accept-Encoding

[root@centos-81 conf.d]# curl -I -H “Accept-Encoding: gzip, deflate” -A “User-Agent:Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0” 192.168.1.81/httpd/a.txt ## IE 9 (zip) —OK

  1. HTTP/1.1 200 OK
  2. Server: nginx/1.14.2
  3. Date: Sun, 30 Dec 2018 08:31:04 GMT
  4. Content-Type: text/plain; charset=UTF-8
  5. Content-Length: 13110
  6. Connection: keep-alive
  7. Last-Modified: Fri, 28 Dec 2018 15:57:19 GMT
  8. ETag: "100b25-1858f-57e171e30b7c5"
  9. Accept-Ranges: bytes
  10. Vary: Accept-Encoding
  11. Content-Encoding: gzip

4.3.5. 实验结论

| nginx | proxy | gzip off ; gunzip off | gzip on ; gunzip off | gzip off ; gunzip on | gzip on ; gunzip on | | —- | —- | —- | —- | —- | —- | | nginx | IE 6 | gunzip | gunzip | gunzip | gunzip | | nginx | IE 9 | gunzip | gzip | gunzip | gzip | | httpd | IE 6 | gzip | gzip | gunzip | gunzip | | httpd | IE 9 | gzip | gzip | gzip | gzip |