Nginx高级配置-压缩功能

              Nginx高级配置-压缩功能

                                       作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.Nginx压缩相关参数概述

1>.gzip on | off;

  Nginx支持对指定类型的文件进行压缩然后再传输给客户端,而且压缩还可以设置压缩比例,压缩后的文件大小将比源文件显著变小,这样有助于降低出口带宽的利用率,降低企业的IT支出,不过会占用相应的CPU资源。

  开源使用"gzip on;"参数来启用压缩,默认是关闭的。

2>.gzip_comp_level lenvel;

  压缩比例由低到高从1到9,默认为1。但需要注意的是压缩比设置的越高就会越消耗CPU的资源,因此在生产环境中我们会设置该参数的值在3~5之间,最好不要超过5,因为随着压缩比的增大的确会降低传输的带宽成本但发送数据前会占用更多的CPU时间分片。

  具体设置级别为多少,得咱们运维人员对CPU的利用率做一个监控,如果CPU利用率过低则不会使用,可以酌情将压缩级别参数调大,当然调大后依旧需要观察一段业务高峰期时间CPU的利用率,最终会找到一个适合你们公司业务的压缩比例。

3>.gzip_disable "MSIE [1-6].";

  禁用IE6 gzip功能。

4>.gzip_min_length 1k;

  gzip压缩的最小文件,小于设置值的文件将不会压缩

5>. gzip_http_version 1.0|1.1;

  启用压缩功能时,协议的最小版本,默认HTTP/1.1

6>.gzip_buffers number size;

  指定Nginx服务需要向服务器申请的缓存空间的个数*大小,默认32 4k|16 8k;

7>.gzip_types mine-type ...;

  指明仅对哪些类型的资源执行压缩操作;默认为gzip_types text/html,不用显示指定,否则出错。

8>.gzip_vary on| off;

  如果启用压缩,是否在响应报文首部插入“Vary: Accept-Encoding”,建议开启该参数,让用户知道咱们服务端是支持压缩的。

9>.Nginx对文件的压缩功能是依赖于模块ngx_http_gzip_module

博主推荐阅读:
    https://nginx.org/en/docs/http/ngx_http_gzip_module.html

二.未启用压缩时观察传输时文件大小

1>.编辑主配置文件

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf/nginx.conf
worker_processes  4;
worker_cpu_affinity 00000001 00000010 00000100 00001000; 
 
events {
   worker_connections  100000;
   use epoll;
   accept_mutex on;
   multi_accept on; 
}
   
   http {
     include       mime.types;
       
     default_type  text/html;
       
     charset utf-8;
   
     log_format my_access_json '{"@timestamp":"$time_iso8601",' '"host":"$server_addr",' '"clientip":"$remote_addr",' '"size":$body_bytes_sent,' '"responsetime":$request_ti
me,' '"upstreamtime":"$upstream_response_time",' '"upstreamhost":"$upstream_addr",' '"http_host":"$host",' '"uri":"$uri",' '"domain":"$host",' '"xff":"$http_x_forwarded_for",' '"referer":"$http_referer",' '"tcp_xff":"$proxy_protocol_addr",' '"http_user_agent":"$http_user_agent",' '"status":"$status"}';   
    access_log logs/access_json.log my_access_json;
   
    include /yinzhengjie/softwares/nginx/conf.d/*.conf;
}
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 

2>.编辑子配置文件

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/share.conf 
   server {
       listen 80;
       server_name node101.yinzhengjie.org.cn;
   
       location / {
           root /yinzhengjie/data/web/nginx/static;
           index index.html;
       }
   
       location /nginx_status {
           stub_status;
           allow 172.30.1.108;
       deny all;
       }
   
       location /main {
       index index.html;
       default_type text/html;
           set $name jason;
           set $nginx_name $server_name;
           echo "姓名: $name";
           echo "************";
           echo "Nginx服务器名称: $nginx_name";
       }
   
   }
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]#

3>.重新加载nginx配置文件

[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root      9297     1  0 Dec17 ?        00:00:00 nginx: master process nginx
nginx    11890  9297  0 12:57 ?        00:00:00 nginx: worker process
nginx    11891  9297  0 12:57 ?        00:00:00 nginx: worker process
nginx    11892  9297  0 12:57 ?        00:00:00 nginx: worker process
nginx    11893  9297  0 12:57 ?        00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx -s reload
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root      9297     1  0 Dec17 ?        00:00:00 nginx: master process nginx
nginx    11946  9297  1 13:25 ?        00:00:00 nginx: worker process
nginx    11947  9297  1 13:25 ?        00:00:00 nginx: worker process
nginx    11948  9297  0 13:25 ?        00:00:00 nginx: worker process
nginx    11949  9297  1 13:25 ?        00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#

4>.生成测试数据

[root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/data/web/nginx/static/
total 8
-rw-r--r-- 1 root root 46 Dec 17 12:53 default.html
-rw-r--r-- 1 root root 73 Dec 17 12:54 index.html
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll -h /var/log/messages 
-rw-------. 1 root root 787K Dec 20 06:31 /var/log/messages
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tail -10 /var/log/messages > /yinzhengjie/data/web/nginx/static/test.html
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cp /var/log/messages /yinzhengjie/data/web/nginx/static/messages.html
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/data/web/nginx/static/
total 800
-rw-r--r-- 1 root root     46 Dec 17 12:53 default.html
-rw-r--r-- 1 root root     73 Dec 17 12:54 index.html
-rw------- 1 root root 805343 Dec 20 06:40 messages.html
-rw-r--r-- 1 root root    746 Dec 20 06:40 test.html
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# chown -R nginx:nginx /yinzhengjie/data/web/nginx/static/
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# chmod 644 /yinzhengjie/data/web/nginx/static/messages.html 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/data/web/nginx/static/
total 800
-rw-r--r-- 1 nginx nginx     46 Dec 17 12:53 default.html
-rw-r--r-- 1 nginx nginx     73 Dec 17 12:54 index.html
-rw-r--r-- 1 nginx nginx 805343 Dec 20 06:40 messages.html
-rw-r--r-- 1 nginx nginx    746 Dec 20 06:40 test.html
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]#

5>.浏览器访问(小于1k的文件)"http://node101.yinzhengjie.org.cn/test.html"并观察响应报文的“Content-Length”属性值,发现文件时没有被压缩的,如下图所示。

6>.浏览器访问(大于1k的文件)"http://node101.yinzhengjie.org.cn/messages.html"并观察响应报文的“Content-Length”属性值,发现文件时没有被压缩的,如下图所示。

三.启用压缩功能后观察传输文件大小

1>.编辑主配置文件

[root@node101.yinzhengjie.org.cn ~]# cat  -n /yinzhengjie/softwares/nginx/conf/nginx.conf
     1    worker_processes  4;
     2    worker_cpu_affinity 00000001 00000010 00000100 00001000; 
     3    
     4    events {
     5        worker_connections  100000;
     6        use epoll;
     7        accept_mutex on;
     8        multi_accept on; 
     9    }
    10    
    11    http {
    12        include       mime.types;
    13        
    14        default_type  text/html;
    15        
    16        charset utf-8;
    17    
    18        log_format my_default_format '$remote_addr - $remote_user [$time_local] "$request"' '$status $body_bytes_sent "$http_referer"' '"$http_user_agent"' '"$http_x_fo
rwarded_    19    for"' '$server_name:$server_port';
    20        access_log logs/access.log my_default_format;
    21    
    22        #配置压缩功能相关参数
    23        gzip on;
    24        gzip_comp_level 5;
    25        gzip_min_length 1k;
    26        gzip_types text/plain application/javascript application/x-javascript text/cssapplication/xml text/javascript application/x-httpd-php image/jpeg image/gif image
/png;    27        gzip_vary on;
    28    
    29        include /yinzhengjie/softwares/nginx/conf.d/*.conf;
    30    }
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 

2>.编辑子配置文件

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/share.conf 
server {
    listen 80;
    server_name node101.yinzhengjie.org.cn;

    location / {
        root /yinzhengjie/data/web/nginx/static;
        index index.html;
    }
}
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]#

3>.重新加载nginx配置文件

[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root      9297     1  0 Dec17 ?        00:00:00 nginx: master process nginx
nginx    11946  9297  0 Dec18 ?        00:00:11 nginx: worker process
nginx    11947  9297  0 Dec18 ?        00:00:00 nginx: worker process
nginx    11948  9297  0 Dec18 ?        00:00:10 nginx: worker process
nginx    11949  9297  0 Dec18 ?        00:00:11 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx -s reload
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root      9297     1  0 Dec17 ?        00:00:00 nginx: master process nginx
nginx    13241  9297  0 06:56 ?        00:00:00 nginx: worker process
nginx    13242  9297  0 06:56 ?        00:00:00 nginx: worker process
nginx    13243  9297  1 06:56 ?        00:00:00 nginx: worker process
nginx    13244  9297  1 06:56 ?        00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]#

4>.浏览器访问(小于1k的文件)"http://node101.yinzhengjie.org.cn/test.html"并观察响应报文的“Content-Length”属性值,发现文件时没有被压缩的,如下图所示。

5>.浏览器访问(大于1k的文件)"http://node101.yinzhengjie.org.cn/messages.html"并观察响应报文的“Content-Length”属性值,发现文件时没有被压缩的,如下图所示。

  点击"message.html"文件的响应信息,如下图所示。

  第二次访问"http://node101.yinzhengjie.org.cn/messages.html"时,可以看不到大小了,如下图所示。

关闭所有浏览器标签实例,重新打开浏览器访问,如下图所示。

6>.开启压缩功能后,只要打开浏览器访问web服务器,发现物理机的CPU就开始飙升了(因为我的虚拟机是在笔记本中搭建的),如下图所示

  使用任务管理器的进程按照CPU使用率查看,的确是Google占用带宽了,我估计是谷歌浏览器在对压缩的数据进行解压操作,压缩和解压过程均需要占用CPU时间片。

当关闭了所有浏览器标签后,一切回归了正常。CPU使用率始终位置在10%以下了

四.当文件被修改时Etag会发生变化

1>.服务端修改源文件内容

[root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/data/web/nginx/static/
total 800
-rw-r--r-- 1 nginx nginx     46 Dec 17 12:53 default.html
-rw-r--r-- 1 nginx nginx     73 Dec 17 12:54 index.html
-rw-r--r-- 1 nginx nginx 805343 Dec 20 06:40 messages.html
-rw-r--r-- 1 nginx nginx    746 Dec 20 06:40 test.html
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# curl -I http://node101.yinzhengjie.org.cn/messages.html
HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Fri, 20 Dec 2019 00:23:55 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 805343
Last-Modified: Thu, 19 Dec 2019 22:40:43 GMT
Connection: keep-alive
Vary: Accept-Encoding
ETag: "5dfbfc6b-c49df"
Accept-Ranges: bytes

[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tail -500 /var/log/messages > /yinzhengjie/data/web/nginx/static/messages.ht
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# curl -I http://node101.yinzhengjie.org.cn/messages.html
HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Fri, 20 Dec 2019 00:24:43 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 43543
Last-Modified: Fri, 20 Dec 2019 00:24:40 GMT
Connection: keep-alive
Vary: Accept-Encoding
ETag: "5dfc14c8-aa17"
Accept-Ranges: bytes

[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/data/web/nginx/static/
total 56
-rw-r--r-- 1 nginx nginx    46 Dec 17 12:53 default.html
-rw-r--r-- 1 nginx nginx    73 Dec 17 12:54 index.html
-rw-r--r-- 1 nginx nginx 43543 Dec 20 08:24 messages.html
-rw-r--r-- 1 nginx nginx   746 Dec 20 06:40 test.html
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 

2>.浏览器再次访问时并不会使用默认的本地缓存,这是为什么呢?(这需要咱们去看看该文件的响应报文头部)

3>.我们发现当ETag的值发生改变时,并不会使用本地缓存啦,而是会发送新的请求,获取文件的最新状态。

原文地址:https://www.cnblogs.com/yinzhengjie/p/12051519.html