首页 > 系统相关 >Nginx高级篇

Nginx高级篇

时间:2023-02-13 15:22:05浏览次数:54  
标签:application http nginx 高级 Nginx proxy gzip keepalive

目录

二、nginx高级篇 (第一部分)

1.扩容

通过扩容提升整体吞吐量

1.单机垂直扩容:硬件资源增加

https://www.bilibili.com/video/BV1yS4y1N76R?p=54

云服务资源增加
整机:IBM、浪潮、DELL、HP等
CPU/主板:更新到主流
网卡:10G/40G网卡
磁盘:SAS(SCSI)HDD(机械)、HHD(混合)、SATA SSD、PCI-e SSD、MVMe SSD
SSD
  多副本机制
  系统盘/热点数据/数据库存储
HDD
  冷数据存储

磁盘:

2.水平扩展:集群化

会话管理

Nginx高级负载均衡
  1.ip_hash
  2.其他Hash
    hash $cookie_jsessionid;
    hash $request_uri;
  3.使用lua逻辑定向分发

Redis + SpringSession

  upstream httpds {
      ip_hash;
      server 192.168.44.102;
      server 192.168.44.103;
  }

  server {
    listen 80;
    server_name localhost;
    location / {
      proxy_pass http://httpds;
      # root html;
    } 
    
     location ~*/(css|img|js) {
        root /usr/local/nginx/html;
      }
  }

Redis + SpringSession:

ip_hash:

ip_hash案例:

在nginx中通过URI维持会话:https://www.bilibili.com/video/BV1yS4y1N76R?p=57
在nginx中使用Java的cookie负载均衡:https://www.bilibili.com/video/BV1yS4y1N76R?p=58

有以下3台服务器:
  服务器1:192.168.44.101  nginx1
  服务器2:192.168.44.102  nginx2
  服务器3:192.168.44.103  nginx3

现在将服务器1作为代理服务器,通过ip_hash将流量接到服务器2和服务器3上。

服务器1的nginx.conf添加配置:
1.通过ip_hash,转发流量
worker_processes 1;
events {
  worker_connections 1024;
}

http {
  include mime.types;
  default_type application/octet-stream;
  sendfile on;
  keepalive_timeout 65;
  '''
  upstream httpget {
    ip_hash;
    server 192.168.44.102;
    server 192.168.44.103;
  }
  '''
  server {
    listen 80;
      server_name localhost;
      location / {
        root html;
      }
      
      error_page 500 502 503 504 /50x.html;
      location = /50x.html {
        root html;
      }
  }
}

打开我们的浏览器访问192.168.44.101,返回的页面内容一直不变,说明当前电脑ip访问nginx代理服务器,通过ip_hash,访问得是同一台后端服务器。如果换个ip访问,通过ip_hash,就可能分配到其它后端服务器上。

应用场景:一般适用于中小型项目在做快速扩容,可增加几台机器,又不想修改代码,可通过nginx做负载均衡,做定向得请求和转发,就特别合适。
缺点:不适合大型项目,上游服务器或者后端服务器如果宕机了,机器中所保持得会话信息,尤其是一些很关键的这些信息,比如一些银行的系统,出入帐,和钱有关的一些操作,客户正在操作,如果后台机器突然宕机了,如果操作有上下文的关系,所有操作步骤没有走完,数据还没有存到数据库,全在会话中保持着,如果这时宕机了,接下来的会话会转移到另外一台机器上。另外一台机器上又没有这些session会话信息,这样客户的操作都全部丢失了,给用户的体验度也不会太好。


2.通过hash $cookie_jsessionid;转发流量
  比如手机app在不支持cookie的情况下,我们可以在url里面加一个参数,直接把jsessionid写在url上,也可以做到会话的保持。在写的程序中如果支持cookie,url中什么都不用带,直接写想要请求的url地址(比如:http://192.168.44.105:8080/index.jsp?pageNum=100;如果不支持cookie,会话就无法保持了,这样有另外一种办法,比如加个jsessionid(比如:http://192.168.44.105:8080/index.jsp?pageNum=100&jseessionid=??),根据每个用户的jsessionid不一样,也可以达到负载均衡的效果。如下图

3.通过hash $request_uri;转发流量
使用request_uri,在访问相同的uri(http://up/guigu.com/a/b.html)的时候,转发到同一台机器。
  upstream httpds {
    # hash $cookie_jsessionid;  # ip_hash流量分发过于集中,使用cookie_jsessionid每个用户jsessionid不同,访问的机器也不同。
    hash $request_uri;  # 192.168.44.101/index.jsp 和192.168。44.101、index.jsp?xxx 属于不同的uri,通过hash,流量转发到不同的机器上。
    server 192.168.44.102;
    server 192.168.44.103; 
  }
  
  使用场景:1.在不支持cookie的情况下  2.资源不平均分配。比如后端服务器是大文件,或者电影什么的,不可能每台服务器都存一份。这样流量就得倾斜到有保存大文件或者电影的近几期中,这种情况可以使用request_uri。

4.使用lua逻辑定向分发
  通过lua语言对nginx做二次开发,可通过lua做更加复杂一点的定向流量分发。在下面将openresty的时候会详细讲解。

sticky模块完成对静态服务器的会话保持:https://www.bilibili.com/video/BV1yS4y1N76R?p=60

# 使用第三方模块平滑升级
升级Nginx:使用sticky模块完成对静态服务器的会话保持

使用sticky模块完成对Nginx的负载均衡
使用参考
http://nginx.org/en/docs/http/ngx_http_upstream_module.html#sticky

tengine中有session_sticky模块我们通过第三方的方式安装在开源版本中
sticky是第三方模块,需要重新编译Nginx,它可以对Nginx这种静态文件服务器使用基于cookie的负载均衡

1.下载模块
项目官网:
  https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/src/master/
另外一个版本:
  https://github.com/bymaximus/nginx-sticky-module-ng

下载:
  https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/1.2.6.zip

2.上传解压
3.重新编译Nginx
依赖openssl-devel # yum install openssl-devel
进到源码目录重新编译:
  ./configure ... --add-module=/absolute/path/to/nginx-sticky-module-ng
  ./configure --prefix=/usr/local/nginx --add-module=/root/nginx-goodies-nginx-sticky-module-ng-c78b7dd79d0d

执行make
如遇报错修改源码

打开ngx_http_sticky_misc.c文件
在12行添加
  # include <openssl/sha.h>
  # include <openssl/md5.h>

备份之前的程序:
  mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old

把编译好的Nginx程序替换到原来的目录里
  cp objs/nginx /usr/local/nginx/sbin/

升级检测
  make upgrade

检查程序中是否包含新模块
  ./nginx -V # 查看当前nginx编译带了哪些参数
  '''
  nginx version: nginx/1.21.6
  built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
  configure argument: --prefix=/usr/local/nginx/ --add-module=/root/nginx-goodies-nginx-sticky-module-ng-c78b7dd79d0d
  '''

sticky [name=route] [domain=.foo.bar] [path=/] [expires=1h] 
       [hash=index|md5|sha1] [no_fallback] [secure] [httponly];

配置方法:
  upstream httpget {
    sticky name=route expires=6h;  
    
    server 192.168.44.102;
    server 192.168.44.103;
  }

浏览器访问192.168.44.101,F12-->Application-->Cookies key=route 生成一个cookies值,这个cookies是nginx下发的。如果后端服务是tomcat,tomcat也会下发sessionid。所以这里起名不能和tomcat下发sessionid冲突了。

3.细粒度拆分:分布式

1.数据分区
2.上游服务SOA化(原生支持水平/垂直扩容)
3.入口细分
  1.浏览器
  2.移动端原生APP(物联网)
  3.H5内嵌式应用

4.数据异构化

1.多级缓存
  1.客户端缓存
  2.CDN缓存
  3.异地多活
  4.Nginx缓存

5.服务异步化

4.拆分请求
5.消息中间件


扩容原则:
1.无状态原则
2.弹性原则

2.KeepAlive

1.在浏览器中查看是否启用keepalive
2.使用charles工具抓包检测
3.什么时候应该使用/关闭keepalive?
4.对客户端配置
5.对上游服务器配置
KeepAlive
在http协议header中看到当前连接状态

测试工具charles
  下载地址:https://www.charlesproxy.com/assets/release/4.6.2/charles-proxy-4.6.2-win64.msi?k=fc1457e312
  官网:https://www.charlesproxy.com

什么时候使用?
  明显的与之前用户会在当前连接上有下一步操作
  复用连接,有效减少握手次数,尤其是https建立一次连接开销会更大

什么时候不用?
  访问内联资源一般用缓存,不需要keepalive
  长时间的tcp连接容易导致系统资源无效占用

# 对客户端使用keepalive的配置
  keepalive_time
  限制keepalive保持连接的最大时间
  1.19.10新功能

  keepalive_timeout
    用于设置Nginx服务器与客户端保持连接超时时间
    用于踢出不活动连接
    keepalive_timeout=0 即关闭
      send_timeout 10; 10秒
      send_timeout 10 10; 同时下发一个header告诉浏览器
    send_timeout
      两次向客户端写操作(比如:大文件下载)之间的间隔 如果大于这个时间则关闭连接 默认60s
      此处有坑,注意耗时的同步操作有可能会丢弃用户连接
       
      
      该设置表示Nginx服务器与客户端连接后,某次会话中服务器等待客户端响应超过10s,就会自动关闭连接。
      keepalive_request
        默认1000
        单个连接中可处理的请求数
      keepalive_disable
        不对某些浏览器建立长连接
        默认msie6(IE6)
     
  http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65 65; # 超过这个时间没有活动,会让keepalive失效。两个活动之间的时间间隔。
    keepalive_time 1h; # 默认1h。一个tcp连接总时长超过之后,强制失效
    send_timeout 60; # 默认60s 此处有坑!! 系统中若有耗时的同步操作,超过send_timeout强制断开连接。注意:这是准备过程,不是传输过程。
    keepalive_requests 1000; # 一个tcp复用中可以并发接收的请求个数
  } 

对上游服务器使用keepalive:
nginx对上游服务器使用keepalive配置详解:https://www.bilibili.com/video/BV1yS4y1N76R?p=66

# 对上游服务器使用keepalive
首先需要配置使用http1.1协议。以便建立更高效的传输,默认使用http1.0,在http1.0中需要配置header才能在Upstream中所配置的上游服务器默认都是用短连接,即每次请求都会在完成之后断开

# 相关配置
upstream中配置
  # 配置
  keepalive 100; # 向上游服务器的保留连接数  
  keepalive_timeout # 连接保留时间  
  keepalive_requests # 一个tcp复用中,可以并发接收的请求个数 默认1000个
  
 
  # server中配置
  proxy_http_version 1.1; # 配置http版本号
  默认使用http1.0协议,需要在request中增加"connection, keepalive" header 才能够支持。而http1.1默认支持
  proxy_set_header Connection ""; # 清除close信息



worker_processes 1;
events {
  worker_connections 1024;
}

http {
  include mime.types;
  default_type application/octet-stream;
  sendfile on;
  keepalive_timeout 65 65; # 超过这个时间没有活动,会让keepalive失效
  keepalive_time 1h; # 一个tcp连接总时长,超过之后,强制失效
    
  send_timeout 60; # 默认60s 此处有坑!!系统中,若有耗时操作,超过send_timeout强制断开连接。注意:准备过程中,不是传输过程
  keepalive_requests 1000; # 一个tcp复用中,可以并发接收的请求个数
 
  upstream httpget {
    keepalive 100;
    keepalive_requests;
    keepalive_timeout 65; # 此配置在upstream中意义不大
    # sticky;
    server 192.168.44.102;
    server 192.168.44.103;
  }

  server {
    listen 80;
      server_name localhost;
      location / {
        # proxy_http_version 1.1;
        # proxy_set_header Connection "";
        proxy_pass http://httpget;
        # root html;
      }
      
      error_page 500 502 503 504 /50x.html;
      location = /50x.html {
        root html;
      }
  }
}

3.AB压力测试keepalive效果

1.AB(apache benchmark安装与基本使用)安装与基本使用

yum install httpd-tools
参数说明:
  -n 即requests,用于指定压力测试总共的执行次数。
  -c 即concurrency,用于指定并发数。
  -t 即timelimit,等待响应的最大时间(单位:秒)。
  -b 即windowsize, TCP发送/接收的缓冲大小(单位:字节)。
  -p 即postfile,发送POST请求时需要上传的文件,此外还必须设置-T参数。
  -u 即putfile,发送PUT请求时需要上传的文件,此外还必须设置-T参数。
  -T 即content-type,用于设置Content-type请求头信息,例如:application/x-www-form-urlencoded,默认值为text/plain。
  -v 即verbosity,指定打印帮助信息的冗余级别。
  -w 以HTML表格形式打印结果。
  -i 使用HEAD请求代替GET请求。
  -x 插入字符串作为table标签的属性。
  -y 插入字符串作为tr标签的属性。
  -z 插入字符串作为td标签的属性。
  -C 添加cookie信息,例如:“Apache=1234”(可以重复该参数选项以添加多个)。
  -H 添加任意的请求头,例如:“Accept-Encoding:gzip”,请求头将会添加在现有的多个请求头之后(可以重复该参数选项以添加多个)。
  -A 添加一个基本的网络认证信息,用户名和密码之间用英文冒号隔开。
  -P 添加一个基本都得代理认证信息,用户名和密码之间用英文冒号隔开。
  -X 指定使用的代理服务器和端口号,例如“126.10.10.3:88”。
  -V 打印版本号并退出。
  -k 使用HTTP的KeepAlive特性。
  -d 不显示百分比。
  -S 不显示预估和警告信息。
  -g 输出结果信息到gnuplot格式的文件中。
  -e 输出结果信息到CSV格式的文件中。
  -r 指定接收到错误信息时不退出程序。
  -h 显示用法信息,其实就是ab-help。
  -f protocol 指定SSL/TLS协议(SSL3,TLS1.1,TLS1.2 或者全部)

2.测试直连nginx

2.测试直连nginx (启用一台虚拟机,并安装AB工具,压测nginx web服务器)
当前机器是192.168.44.104,压测192.168.44.102和192.168.44.103服务器。
ab -n 10000 -c 30 http://192.168.44.102/

Server software:	nginx/1.21.6
Server Hostname:	192.168.44.102
Server Port:		80
Document Path :		/
Document Length:	16 bytes
Concurrency Leve1:	30
Time taken for tests:	13.035 seconds
Complete requests:	100000
Failed requests:	0
Write errors:	0
Total transferred:	25700000 bytes
HTML transferred:	1600000 bytes
Requests per second:	7671.48[#/sec] (mean)
Time per request:	3.911 [ms] (mean)
Time per request: 0.130 [ms] (mean,across all concurrent requests) # 最低延迟
Transfer rate:	1925.36 [kbytes/sec] received
Connection Times (ms)
           min  mean  [+/-sd]  median    max 
Connect:    0     0     0.4       0       12
Processing: 1     3     1.0       3       14
waiting:    0     3     0.9       3       14
Total:      2     4     0.9       4       14

Percentage of the requests served within a certain time (ms)
  50%    4
  66%    4
  75%    4
  80%    4
  90%    5
  95%    5
  98%    6
  99%    7
  100%   14(longest request)

3.测试nginx反向代理


  192.168.44.101 反向代理服务器
  192.168.44.102 nginx web服务器
  192.168.44.103 nginx web服务器
  192.168.44.104 ab测试服务器

修改反向代理服务器nginx的nginx.conf文件:

  upstream httpget {
#    keepalive 100;
#    keepalive_requests 1000;
#    keepalive_timeout 65; # 此配置在upstream中意义不大
    # sticky;
    server 192.168.44.102;
    server 192.168.44.103;
  }

  server {
    listen 80;
      server_name localhost;
      location / {
        proxy_pass http://httpget;
        # root html;
      }

关掉所有keepalive选项的配置,通过反向代理压测。。
ab -n 100000 -c 30 http://192.168.44.101/

Server Software:    nginx/1.21.6
Server Hostname:    192.168.44.101
Server Port:    80
Document Path    /
Document Length:    16 bytes
Concurrency Level:    30
Time taken for tests:    25.968seconds
Complete requests:    100000
Failed requests:    0
write errors:    0
Total transferred:    25700000 bytes
HTML transferred:    1600000 bytes
Requests per second:    3850.85 [#/sec] (mean)
Time per request:     7.790 [ms] (mean)
Time per request:    0.260 [ms] (mean,across all concurrent requests) # 最低延迟
Transfer rate:    966.47 [Kbytes/sec] received

Connection Times (ms)
           min   mean    [+/-]  median  max
Connect:    0      0      0.2      0     13
Processing: 3      8      1.4      7     22
Waiting:    1      7      1.4      7     22
Total:      3      8      1.4      7     22

Percentage of the requests served within a certain time (ms):
  50%    7
  66%    8
  75%    8
  80%    8
  90%    9
  95%    10
  98%    12
  99%    13
 100%    22 (longest request)

4.测试nginx Upstream keepalive代理

4.测试nginx Upstream keepalive代理

打开上游upstream中的所有keepalive选项。
  upstream httpget {
    keepalive 100;
    keepalive_requests 1000;
    keepalive_timeout 65; # 此配置在upstream中意义不大
    # sticky;
    server 192.168.44.102;
    server 192.168.44.103;
  }

  server {
    listen 80;
      server_name localhost;
      location / {
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        proxy_pass http://httpget;
        # root html;
      }

ab -n 100000 -c 30 http://192.168.44.101/

Server Software:    nginx/1.21.6
Server Hostname:    192.168.44.101
Server Port:    80
Document Path    /
Document Length:    16 bytes
Concurrency Level:    30
Time taken for tests:    18.026
Complete requests:    100000
Failed requests:    0
write errors:    0
Total transferred:    25700000 bytes
HTML transferred:    1600000 bytes
Requests per second:    5547.69 [#/sec] (mean)
Time per request:     5.408 [ms] (mean)
Time per request:    0.180 [ms] (mean,across all concurrent requests) # 最低延迟
Transfer rate:    1392.34 [Kbytes/sec] received

Connection Times (ms)
           min   mean    [+/-]  median  max
Connect:    0      0      0.3      0     13
Processing: 1      5      1.2      5     20
Waiting:    1      5      1.2      5     15
Total:      2      5      1.2      5     20

Percentage of the requests served within a certain time (ms):
  50%    5
  66%    5
  75%    6
  80%    6
  90%    7
  95%    7
  98%    9
  99%    11
 100%    20 (longest request)

5.测试Tomcat直连与代理

5.测试Tomcat直连与代理
# 启动tomcat服务
./catalina.sh run
浏览器访问tomcat服务:192.168.44.105:8080


# (1)压测tomcat直连(单机tomcat) 
ab -n 100000 -c 30 http://192.168.44.105:8080/

Server software:	
Server Hostname:	192.168.44.105
Server Port:		8080
Document Path :		/
Document Length:	7834 bytes
Concurrency Leve1:	30
Time taken for tests:	31.033 seconds
Complete requests:	100000
Failed requests:	0
Write errors:	0
Total transferred:	804300000 bytes
HTML transferred:	783400000 bytes
Requests per second:	3222.38 [#/sec] (mean)
Time per request:	9.310 [ms] (mean)
Time per request: 0.310 [ms] (mean,across all concurrent requests)
Transfer rate:	25310.16 [kbytes/sec] received 
Connection Times (ms)
           min  mean  [+/-sd]  median    max 
Connect:    0     0     0.3       0       15
Processing: 0     9     7.8       7       209
waiting:    0     9     7.2       7       209
Total:      0     9     7.8       7       209

Percentage of the requests served within a certain time (ms)
  50%    7
  66%    9
  75%    11
  80%    13
  90%    18
  95%    22
  98%    27
  99%    36
  100%   209(longest request)

# (2)反向代理 打开keepalive,代理到tomcat
打开上游upstream中的所有keepalive选项。
  upstream httpget {
    keepalive 100;
    keepalive_requests 1000;
    keepalive_timeout 65; # 此配置在upstream中意义不大
    # sticky;
    server 192.168.44.105:8080;
    server 192.168.44.103;
  }

  server {
    listen 80;
      server_name localhost;
      location / {
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        proxy_pass http://httpget;
        # root html;
      }

# ab压测代理服务器转发tomcat
ab -n 100000 -c 30 http://192.168.44.101/

Server Software:    nginx/1.21.6
Server Hostname:    192.168.44.101
Server Port:    80
Document Path    /
Document Length:    7834 bytes
Concurrency Level:    30
Time taken for tests:    23.379 seconds   
Complete requests:    100000
Failed requests:    0
write errors:    0
Total transferred:    806500000 bytes
HTML transferred:    783400000 bytes
Requests per second:    4277.41 [#/sec] (mean)
Time per request:     7.014 [ms] (mean)
Time per request:    0.234 [ms] (mean,across all concurrent requests)
Transfer rate:    33688.77 [Kbytes/sec] received

Connection Times (ms)
           min   mean    [+/-]  median  max
Connect:    0      0      0.2      0     9
Processing: 1      7      4.2      6     143
Waiting:    1      7      4.2      6     143
Total:      1      7      4.2      6     143

Percentage of the requests served within a certain time (ms):
  50%    6
  66%    7
  75%    7
  80%    7
  90%    8
  95%    10
  98%    13
  99%    16
 100%    143 (longest request)

为何直连tomcat压测的性能低于反向代理到tomcat的压测?
ab直连tomcat的QPS是3200,ab如果直连nginx的话,QPS是7000多,这还是没有开启keepalive的QPS;然后nginx再连接tomcat,中间是由keepalive存在的,所以速度更高一点。

现在把nginx代理tomcat的keepalive关掉再次压测:

# (3)反向代理 关闭keepalive,代理到tomcat
关闭上游upstream中的所有keepalive选项。
  upstream httpget {
#    keepalive 100;
#    keepalive_requests 1000;
 #   keepalive_timeout 65; # 此配置在upstream中意义不大
    # sticky;
    server 192.168.44.105:8080;
    server 192.168.44.103;
  }

  server {
    listen 80;
      server_name localhost;
      location / {
 #       proxy_http_version 1.1;
 #       proxy_set_header Connection "";
        proxy_pass http://httpget;
        # root html;
      }

# ab压测代理服务器转发tomcat
ab -n 100000 -c 30 http://192.168.44.101/


Server Software:    nginx/1.21.6
Server Hostname:    192.168.44.101
Server Port:    80
Document Path    /
Document Length:    7834 bytes
Concurrency Level:    30
Time taken for tests:    33.814 seconds   
Complete requests:    100000
Failed requests:    0
write errors:    0
Total transferred:    806500000 bytes
HTML transferred:    783400000 bytes
Requests per second:    2957.32 [#/sec] (mean)
Time per request:     10.144 [ms] (mean)
Time per request:    0.338 [ms] (mean,across all concurrent requests)
Transfer rate:    23291.74 [Kbytes/sec] received

Connection Times (ms)
           min   mean    [+/-]  median  max
Connect:    0      0       0.2      0     9
Processing: 1      10      5.5      9     229
Waiting:    1      10      5.5      9     229
Total:      1      10      5.5      9     229

Percentage of the requests served within a certain time (ms):
  50%    9
  66%    10
  75%    11
  80%    11
  90%    13
  95%    14
  98%    17
  99%    19
 100%    229 (longest request)
6.什么时候在Tomcat前置nginx性能有明显提升?
客户端不支持keepalive,但也尽量让客户端支持连接复用。

4.Nginx反向代理核心流程

反向代理内存与文件缓冲区核心流程:https://www.bilibili.com/video/BV1yS4y1N76R?p=70

Upstream proxy_pass工作流程
内存与文件缓冲区

# Upstream工作流程
proxy_pass 向上游服务器请求数据共有6个阶段
  初始化
  与上游服务器建立连接
  向上游服务器发送请求
  处理响应头 
  处理响应体
  结束

set_header # 设置header
proxy_connect_timeout # 与上游服务器连接超时时间、快速失败 
proxy_send_timeout # 定义nginx向后端服务发送请求的间隔时间(不是耗时)。默认60秒,超过这个时间会关闭连接
proxy_read_timeout # 后端服务给nginx响应的时间,规定时间内后端服务没有给nginx响应,连接会被关闭,nginx返回504Gateway Time-out。默认60秒

缓冲区
proxy_request_buffering # 是否完全读到请求体之后再向上服务器发送请求
proxy_buffering # 是否缓冲上游服务器数据
proxy_buffers 32 64k; # 缓冲区大小 32个 64K大小内存缓冲块
proxy_buffer_size # header缓冲区大小

proxy_request_buffering on;
proxy_buffering on;
proxy_buffer_size 64k;
proxy_buffers 32 128k;
proxy_busy_buffers_size 8k;
proxy_max_temp_file_size 8k;
proxy_max_temp_file 1024m;


proxy_temp_file_write_size 8k
当启用从代理服务器到临时文件的响应的缓冲时,一次限制写入临时文件的数据的大小,默认情况下,大小由proxy_buffer_size和proxy_buffers指令设置的两个缓冲区限制,临时文件的最大大小由proxy_max_temp_file_size设置。

proxy_max_temp_file_size 1024m; #临时文件最大值(proxy_pass 读到向磁盘写入的最大值)
proxy_temp_path 
  proxy_temp_path /spool/nginx/proxy_temp 1 2;

a temporary file might look like this:
  /spool/nginx/proxy_temp/7/45/00000123457

5.Nginx对客户端的缓冲和限制

https://www.bilibili.com/video/BV1yS4y1N76R?p=71

# 对客户端的限制  
可配置位置:
  http
  server
  location

client_body_buffer_size # 对客户端请求中的body缓冲区大小
client_header_buffer_size # 设置读取客户端请求体的缓冲区大小,如果请求体大于缓冲区,则将整个请求体或仅将其部分写入临时文件,默认32位8K,64位平台16K。

client_max_body_size 1000M; # 默认1m,如果一个请求的大小超过配置的值,会返回413(request Entity Too Large)纠错给客户端,将size设置为0,将禁用对客户端请求正文大小的检查。

client_body_timeout  # 指定客户端与服务器建立连接后发送request body的超时时间。如果客户端在指定时间内没有发送任何内容,Nginx返回HTTP 408(Request Timed Out)

client_header_timeout # 客户端向服务端发送一个完整的request header的超时时间,如果客户端在指定时间内没有发送一个完整的request header,Nginx 返回HTTP408(Request Timed Out)。

client_body_temp_path path[level1[level2[level3]]] # 在磁盘上客户端的body临时缓冲区位置

client_body_in_file_only on; # 把body写入磁盘文件,请求结束也不会删除

client_body_in_single_buffer # 尽量缓冲body的时候在内存中使用连续单一缓冲区,在二次开发时使用$request_body读取数据时性能会有所提高。

client_body_buffer_size # 默认32位8k 64位16k  如果请求体大于配置,则写入临时文件

client_header_buffer_size # 设置读取客户端请求头的缓冲区大小  如果一个请求行或者一个请求头字段不能放入这个缓冲区,那么就会使用large_client_header_buffers

large_client_header_buffers # 默认8k  

6.反向代理中的容错机制

# 反向代理中的容错机制
https:docs.nginx.com/nginx/admin-guide/load-balancer/http-load-balancer/
http://nginx.org/en/docs/stream/ngx_stream_proxy_module.html#proxy_bind

proxy_timeout
    
# 重试机制
proxy_next_upstream
作用:
当后端服务器返回指定的错误时,将请求传递到其它服务器。
error与服务器建立连接,向其传递请求或读取响应头时发生错误;
timeout在与服务器建立连接,向其传递请求或读取响应头时发生超时;
invalid_header服务器返回空的或无效响应;
http_500 服务器返回代码为500的响应;
http_502 服务器返回代码为502的响应;
http_503 服务器返回代码为503的响应;
http_504 服务器返回代码为504的响应;
http_403 服务器返回代码为403的响应;
http_404 服务器返回代码为404的响应;

7.获取真实IP

https://www.bilibili.com/video/BV1yS4y1N76R?p=73

# 获取客户端真实IP
X-Real-IP # 额外模块,不推荐使用

setHeader
  proxy_set_header X-Forwarded-For $remote_addr;  # 我们可以把客户端的ip地址写到header当中,我们的上游服务器再去读取这个header。意思就是反向代理服务器读到客户机的真实IP时,在传递请求的时候,可以在header中加上X-forwarded-For,上游服务器在去读客户端地址时,就不要读remote_addr(这个是nginx的地址,不能伪造的)了,。如下图

server {
  listen 80;
  server_name localhost;
  location / {
    proxy_http_version 1.1;
    proxy_set_header Connection "";
    proxy_set_header X-Forwarded-For $remote_addr; # 这个remote_addr是与nginx物理连接的真正的TCP的地址。
    proxy_pass http://httpget;
    #root html;
  }
}

8.Gzip压缩

Gzip原理:

# 浏览器支持的压缩算法:deflate  gzip br
deflate:出现在apache hpptd上的,现在nginx已经看不到这种算法了。
br:谷歌开发的压缩算法
Gzip动态压缩

作用域 http,server,location

gzip on;
开关,默认关闭
gzip_buffers 32 4k(32位操作系统)| 16 8k(64位操作系统) # 缓冲区大小
gzip_comp_level 1; # 压缩等级 1-9,数字越大压缩比越高

gzip_http_version 1.1; # 使用gzip的最小版本
gzip_min_length # 设置将被gzip压缩的响应的最小长度。长度仅由“Content-Length”响应报头字段确定。


gzip_proxied 多选 # 主要针对于反向代理服务器,配置才有意义。对上游服务器返回的header做条件性的压缩配置。
  off为不做限制,也就是把gzip_proxied关闭。 
  作为反向代理时,针对上游服务器返回的头信息进行压缩
  expired - 启用压缩,如果header头中包含“Expires”头信息
  no-cache - 启用压缩,如果header头中包含“Cache-Control:no-cache” 头信息
  no-store - 启用压缩,如果header头中包含“Cache-Control:no-store” 头信息
  private - 启用压缩,如果header头中包含“Cache-Control:private” 头信息
  no_last_modified - 启用压缩,如果header头中不包含“Last_Modified”头信息
  no_etag - 启用压缩,如果header头中不包含“ETag”头信息
  auth - 启用压缩, 如果header头中包含“Authorization”头信息
  any - 无条件启用压缩,不论任何内容都压缩


gzip_vary on; # 增加一个header,适配老的浏览器 vary:Accept-Encoding
gzip_types # 哪些mime类型的文件进行压缩
gzip_disable # 禁止某些浏览器使用gzip

完整实例: 动态压缩
location / {
gzip on;
gzip_buffers 16 8k;
gzip_comp_level 6;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_proxied any;
gzip_vary on;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_types
  text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
  text/javascript application/javascript application/x-javascript
  text/x-json application/json application/x-web-app-mainfest+json
  test/css text/plain text/x-component
  font/opentype application/x-font-ttf applicaiton/vnd.ms-fontobject
  image/x-icon; # 针对浏览器标签中显示的图标要不要压缩
# 针对IE6版本一下的浏览器,禁用gzip压缩
gzip_disable "MSIE [1-6]/.(?!.*SV1)"; # 对于高性能服务器,正则表达式对于服务器性能的消耗是指数级的。尽量不要配。

proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header X-Forwarded-For $remote_addr; # 这个remote_addr是与nginx物理连接的真正的TCP的地址。
proxy_pass http://httpget;
#root html;
}

  gzip on,我们现在已经把它给打开了,现在打开的这种方式叫动态压缩方式,也就是所有的请求,请求到我的服务器的后端,那么我全都会经历一次压缩,然后这种压缩方式,有一个致命的缺点,这个缺点就是它无法再使用我们nginx一个高级特性sendfile,那个sendfile,它是针对于本地磁盘文件的,大家要知道。我磁盘上有一个文件,开启了sendfile,数据零拷贝,直接一个信号儿发送给我的内核,内核直接从磁盘上找到这个文件,不经过用户态,也就是不加载到Ningx的内存里面,而直接通过网络接口或者是网卡驱动,直接把这份数据给传出去。sendfile一旦要开了,传输速度是非常高的,因为少一次内存拷贝。但如果你要开了这个动态的gzip,也是我们现在的默认配置,那个sendfile就无效了,就再也无法使用sendfile技术了。但是我们还是期望使用一下gzip,虽然还没搞明白为什么要用,但很多网站都在用这个gzip。为什么要用?是放弃了sendfile吗?肯定不是。因为还有另外一种方式叫Gzip静态压缩方式,也就是把我们请求的这些资源,事先准备好,放在磁盘上,然后再sendfile传出去。

浏览器可接受的压缩格式:

Gzip静态压缩:https://www.bilibili.com/video/BV1yS4y1N76R?t=175.1&p=78

gunzip_module配置使用:https://www.bilibili.com/video/BV1yS4y1N76R?p=79

Gzip静态压缩

Gzip动态压缩是无法使用sendfile的,而Gzip静态压缩可以说是gzip动态压缩的一个扩展功能,使用Gzip静态压缩可以完美解决这个问题。

Gzip静态压缩是nginx的一个模块,我们只需要在编译的时候将其加入进来就可以了。
ngx_http_gunzip_module:可将本地磁盘上的压缩包解压之后再发送给客户端。所以本地磁盘不需要存储原文件,可节省磁盘空间。
ngx_http_gzip_static_module

  ./configure --prefix=/usr/local/nginx/ --with-http_gzip_static_module
  make  #之后不要make install 不然会把nginx配置文件等全部覆盖掉了。
  mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old1
  cp objs/nginx /usr/local/nginx/sbin/
  systemctl restart nginx
    
使用方法:https://nginx.org/en/docs/http/ngx_http_gzip_static_module.html

作用域 http,server,location

gzip_static on | off | always;
  on:会检查客户端是否支持gzip,如果不支持,就不会发送gzip包了。
  off: 关闭gzip_static模块关闭掉
  always:不论客户端是否支持gzip,都发送这个压缩包。而如果客户端不支持gzip,就无法解包,这时候有个ngx_http_gunzip_module模块可以帮助我们把压缩包的内容解开再发送给客户端,相当于一层拦截。

# 给nginx加入ngx_http_gunzip_module模块
./configure --prefix=/usr/local/nginx/ --with-http_gzip_static_module --with-http_gunzip_module
 make

使用方法:https://nginx.org/en/docs/http/ngx_http_gunzip_module.html

完整实例: 静态压缩+gunzip
location / {
gunzip on;
gzip_static always;

gzip on;
gzip_buffers 16 8k;
gzip_comp_level 6;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_proxied any;
gzip_vary on;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_types
  text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
  text/javascript application/javascript application/x-javascript
  text/x-json application/json application/x-web-app-mainfest+json
  test/css text/plain text/x-component
  font/opentype application/x-font-ttf applicaiton/vnd.ms-fontobject
  image/x-icon; # 针对浏览器标签中显示的图标要不要压缩
# 针对IE6版本一下的浏览器,禁用gzip压缩
gzip_disable "MSIE [1-6]/.(?!.*SV1)"; # 对于高性能服务器,正则表达式对于服务器性能的消耗是指数级的。尽量不要配。

proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header X-Forwarded-For $remote_addr; # 这个remote_addr是与nginx物理连接的真正的TCP的地址。
proxy_pass http://httpget;
# root html;
}

Brotli比gzpi更好的压缩格式模块化安装:https://www.bilibili.com/video/BV1yS4y1N76R?p=80

第三方zip模块Brotli与模块化加载

# Brotli
# 安装
官网:
  https://github.com/google/ngx_brotli
  https://github.com/google/brotli/tree/v1.0.9 # 算法包
下载两个项目
解压缩
tar -xzvf ngx_brotli-1.0.0rc.tar.gz
tar -xzvf brotli-1.0.9.tar.gz
cd brotli-1.0.9
mv ./* /root/ngx_brotli-1.0.0rc/deps/brotli/
# 模块化编译:
cd nginx-1.21.6
./configure  --prefix=/usr/local/nginx/ --with-compat --add-dynamic-module=/root/ngx_brotli-1.0.0rc
或 --add-dynamic-module=brotli目录
make
cd nginx-1.21.6/objs/

# 将ngx_http_brotli_filter_module.so ngx_http_brotli_static_module.so 拷贝到/usr/local/nginx/modules/
mkdir /usr/local/nginx/modules
cp ngx_http_brotli_filter_module.so /usr/local/nginx/modules/
cp ngx_http_brotli_static_module.so /usr/local/nginx/modules/

# 复制nginx主程序
mv /usr/local/nginx/sbin/nginx  /usr/local/nginx/sbin/nginx.old3
cp /root/nginx-1.21.6/objs/nginx /usr/local/nginx/sbin/

systemctl restart nginx
"""
报错:Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
"""
# 查看报错状态
systemctl status nginx
'''
nginx:[emerg] unknown directive "gunzip" in /usr/local/nginx/conf/nginx/conf:41
'''
因为这次configure编译时,没有加入ngx_http_gunzip_module模块,编译后的可执行程序nginx在读取配置文件nginx.conf时还是之前的配置文件(因为我们一直都在做测试,每次编译后make,没有make install,配置文件没被覆盖),配置文件有gunzip on; 的配置,所以报错。。这里我们可以将这个配置删除掉

# 再次启动
systemctl restart nginx
"""
又报错:nginx:[emerg] unknown directive "gzip_static" in /usr/local/nginx/conf/nginx/conf:41
"""
删除配置文件nginx.conf 中的gzip_static always;

# 再次启动
systemctl restart nginx

配置文件中添加
  load_module "/usr/local/nginx/modules/ngx_http_brotli_filter_module.so";
  load_module "/usr/local/nginx/modules/ngx_http_brotli_static_module.so";



# Dynamically loaded
$ cd nginx-1.x.x
$ ./configure --with-compat --add-dynamic-module=/path/to/ngx_brotli
$ make modules
You will need to use exactly the same ./configure arguments as your Nginx configuration and append --with-compat --add-dynamic-module=/path/to/ngx_brotli to the end, otherwise you will get a "module is not binary compatible" error on startup. You can run nginx -V to get the configuration arguments for your Nginx installation.

make modules will result in ngx_http_brotli_filter_module.so and ngx_http_brotli_static_module.so in the objs directory. Copy these to /usr/lib/nginx/modules/ then add the load_module directives to nginx.conf (above the http block):

load_module modules/ngx_http_brotli_filter_module.so;
load_module modules/ngx_http_brotli_static_module.so;

# Statically compiled
$ cd nginx-1.x.x
$ ./configure --add-module=/path/to/ngx_brotli
$ make && make install
This will compile the module directly into Nginx.



# Configuration directives

1.brotli_static

syntax: brotli_static on|off|always
default: off
context: http, server, location
    
Enables or disables checking of the existence of pre-compressed files with.br extension. With the always value, pre-compressed file is used in all cases, without checking if the client supports it.


2.brotli

syntax: brotli on|off
default: off
context: http, server, location, if
    
Enables or disables on-the-fly compression of responses.


3.brotli_types

syntax: brotli_types <mime_type> [..]
default: text/html
context: http, server, location
    
Enables on-the-fly compression of responses for the specified MIME types in addition to text/html. The special value * matches any MIME type. Responses with the text/html MIME type are always compressed.


4.brotli_buffers

syntax: brotli_buffers <number> <size>
default: 32 4k|16 8k
context: http, server, location
    
Deprecated, ignored.


5.brotli_comp_level

syntax: brotli_comp_level <level>
default: 6
context: http, server, location
    
Sets on-the-fly compression Brotli quality (compression) level. Acceptable values are in the range from 0 to 11.


6.brotli_window

syntax: brotli_window <size>
default: 512k
context: http, server, location
    
Sets Brotli window size. Acceptable values are 1k, 2k, 4k, 8k, 16k, 32k, 64k, 128k, 256k, 512k, 1m, 2m, 4m, 8m and 16m.


7.brotli_min_length

syntax: brotli_min_length <length>
default: 20
context: http, server, location
    
Sets the minimum length of a response that will be compressed. The length is determined only from the Content-Length response header field.


# Variables

9.$brotli_ratio

Achieved compression ratio, computed as the ratio between the original and compressed response sizes.

Sample configuration
brotli on;
brotli_comp_level 6;
brotli_static on;
brotli_types application/atom+xml application/javascript application/json application/rss+xml
             application/vnd.ms-fontobject application/x-font-opentype application/x-font-truetype
             application/x-font-ttf application/x-javascript application/xhtml+xml application/xml
             font/eot font/opentype font/otf font/truetype image/svg+xml image/vnd.microsoft.icon
             image/x-icon image/x-win-bitmap text/css text/javascript text/plain text/xml;



完整实例:ngx_brotli
location / {
# gunzip on;
# gzip_static always;

gzip on;
gzip_buffers 16 8k;
gzip_comp_level 6;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_proxied any;
gzip_vary on;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_types
  text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
  text/javascript application/javascript application/x-javascript
  text/x-json application/json application/x-web-app-mainfest+json
  test/css text/plain text/x-component
  font/opentype application/x-font-ttf applicaiton/vnd.ms-fontobject
  image/x-icon; # 针对浏览器标签中显示的图标要不要压缩
# 针对IE6版本一下的浏览器,禁用gzip压缩
gzip_disable "MSIE [1-6]/.(?!.*SV1)"; # 对于高性能服务器,正则表达式对于服务器性能的消耗是指数级的。尽量不要配。

brotli on; # 开关,表示动态压缩,类似gzip_static。 比gzip on压缩效率高20%。
brotli_static on; # 静态压缩。。如果是always就不在启用动态压缩,brotli on无效
brotli_comp_level 6;
brotli_buffers 16 8k;
brotli_min_length 20;
brotli_types application/atom+xml application/javascript application/json application/rss+xml
             application/vnd.ms-fontobject application/x-font-opentype application/x-font-truetype
             application/x-font-ttf application/x-javascript application/xhtml+xml application/xml
             font/eot font/opentype font/otf font/truetype image/svg+xml image/vnd.microsoft.icon
             image/x-icon image/x-win-bitmap text/css text/javascript text/plain text/xml;

proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header X-Forwarded-For $remote_addr; # 这个remote_addr是与nginx物理连接的真正的TCP的地址。
proxy_pass http://httpget;
#root html;
}

# 重新启动nginx
systemctl restart nginx 
'''
报错:nginx:[emerg] unknown directive "brotli" in /usr/local/nginx/conf/nginx.conf:62
'''

找不到这个broti指令,主要是因为我们现在以动态化地加载模块,不是编译完就有的。所以要在nginx.conf中加载这个模块。
将以下两行加到nginx.conf中:# 加在http之外
load_module "/usr/local/nginx/modules/ngx_http_brotli_filter_module.so";
load_module "/usr/local/nginx/modules/ngx_http_brotli_static_module.so";

# 测试
默认http协议是没有br的
curl -H 'accept-Encoding: gzip' -I http://localhost

chrome浏览器默认没有开启br的支持,不支持br的压缩方式,只有在https下才支持br。

9.Concat模块压缩客户端的请求数(淘宝网案例)

视频连接: https://www.bilibili.com/video/BV1yS4y1N76R?t=163.9&p=82

模拟淘宝网和并请求效果:https://www.bilibili.com/video/BV1yS4y1N76R?p=84

合并请求:客户端请求的资源中,一个html页面上有css(文本类)、js(文本类)、jpg/png(多媒体类)等信息,一个页面是n多个请求组成的。如果把一个页面的n多个请求(css、js、jpg/png)合并成一个或几个,总的请求书减少,那么nginx、服务端就能承受更多的并发量。

缺点:有合并操作,会牺牲一部分性能来换取开发和管理上的高效。因为合并后,会读合并的这几个文件,并加载到内存(从内核态到用户态复制,sendfile也使用不了了),所以会有内存区域的复制,性能会略有下降,但请求从计算性能上向网络上妥协了一些,这样会少建立几个请求。大型网站都在使用。concat模块是淘宝网研发出来的,

压缩请求数

Concat模块原理及配置

# 合并客户端请求
Concat模块
nginx的几个主流版本:Tengine(淘宝网发布,被捐赠给apache组织) openresty

Nginx官方关于Concat的介绍和使用方法:
  https://www.nginx.com/resources/wiki/modules/concat/
  git地址:https://github.com/alibaba/nginx-http-concat

安装:下载源码解压缩编译安装
  git clone git://github.com/alibaba/nginx-http-concat.git
  unzip nginx-http-concat-master.zip
  ./configure --prefix=/usr/local/nginx/ --add-module=/root/nginx-http-concat-master
  make 
  mv /usr/local/nginx/sbin/nginx  /usr/local/nginx/sbin/nginx.old4
  cp /root/nginx-1.21.6/objs/nginx /usr/local/nginx/sbin/

配置:
  concat on;
  concat_max_files 30;


资源静态化:
  高并发系统资源静态化方案
  一致性问题
  合并文件输出
  集群文件同步

SSL合并服务器端文件
  官方文档:http://nginx.org/en/docs/http/ngx_http_ssl_module.html
 
配置:
ssi_min_file_chunk # 向磁盘存储并使用sendfile发送,文件大小最小值
ssi_last_modified # 是否保留lastmodified
ssi_silent_errors # 不显示逻辑错误
ssi_value_length # 限制脚本参数最大长度
ssi_types # 默认text/html;如果需要其它mime类型 需要设置

include file
  <!--# include fule="footer.html" -->
静态文件直接引用

include virtual # 可指向location,而不一定是具体文件
include wait
include set
set
block
config errmsg
config timefmt
echo
if

合并js:

合并css:

10.高并发系统资源静态化

高并发系统资源静态化方案

一致性问题
合并文件输出
集群文件同步
item.html
1.内容
2.关联->引用 动态
3.固定公用-> 抽取静态

前端->节约服务器端的计算资源,消耗请求数   后端->SSI

SSI服务器端配置选项: https://www.bilibili.com/video/BV1yS4y1N76R?t=427.1&p=87

SSI合并服务器端文件(官方nginx内置模块,不需要额外添加和编译。)

官方文档:http://nginx.org/en/docs/http/ngx_http_ssi_module.html

配置:
ssi_min_file_chunk # 向磁盘存储并使用sendfile发送,文件大小最小值

ssi_last_modified # 是否保留last modified

ssi_value_length # 限制脚本参数最大长度

ssi_silent_errors on | off # index.html页面中配置的include语法错误,是否屏蔽掉。

ssi_types # 文件类型


location / {
  ssi on; ##############################
  concat on;
  concat_max_files 20;
  proxy_http_version 1.1;
  proxy_set_header Connection "";
  proxy_set_header X-Forwarded-For $remote_addr;
  #proxy_pass http://httpget;
  root html;
}


在/usr/local/nginx/html 目录下添加两个文件:top.html bottom.html
在index.html中按照ssi使用说明进行配置:

<!--# include file="top.html"-->
<link href="??font.css,bg.css" rel="stylesheet">

hello workd!hello workd!hello workd!hello workd!hello workd!hello workd!hello workd!hello workd!hello workd!hello workd!hello workd!hello workd!hello workd!hello workd!hello workd!hello workd!hello workd!hello workd!hello workd!hello workd!hello workd!hello workd!hello workd!hello workd!hello workd!hello workd!hello workd!hello workd!hello workd!hello workd!hello workd!hello workd!hello workd!hello workd!hello workd!

<!--# include file="bottom.html"-->

11.rsync

https://www.samva.org/ftp/rsync/rsync.html

# 集群文件同步
remote synchronize是一个远程数据同步工具,可通过LAN/WAN快速同步堕胎主机之间的文件,也可以使用rsync同步本地磁盘中的不同目录。
 rsync 是用于替代rcp的一个工具,rsync使用所谓的rsync算法进行数据同步,这种算法只传送两个文件的不同部分,而不是每次都整份传送,因此速度相当快。
rsync基于inotify开发

Rsync有三种模式:
  本地模式(类似于cp命令)
  远程模式(类似于scp命令)
  守护进程(socket进程:是rsync的重要功能)

rsync常用选项:
-a  包含-rtplgoD
-r  同步目录时要加上,类似cp时的-r选项
-v  同步时显示一些信息,让我们知道同步的过程
-l  保留软连接
-L  加上该选项后,同步软连接时会把源文件给同步
-p  保持文件的权限属性
-o  保持文件的属主
-g  保持文件的属组
-D  保持设备文件信息
-t  保持文件的时间属性


inotify常用参数:

-r    --recursive    # 递归查询目录
-q    --quiet        # 打印很少的信息,仅仅打印监控事件信息
-m    --monitor      # 始终保持事件监听状态
--exclude            # 排除文件或目录时,不区分大小写
--timefmt            # 指定事件输出格式
--format             # 打印使用指定的输出类似格式字符串
-e  --event[-e | --event...] # 通过此参数可以指定要监控的事件#文件或目录被读取#文件或目录的内容被修改#文件或目录属性被改变#文件或目录封闭,无论读/写模式#文件或目录被打开#文件或目录被移动至另外一个目录#文件或目录被移动另一个目录或从另一个目录移动至当前目录#文件或目录被创建在当前目录#文件或目录被删除#文件系统被卸载。

标签:application,http,nginx,高级,Nginx,proxy,gzip,keepalive
From: https://www.cnblogs.com/zccoming/p/17116506.html

相关文章

  • 从原理到实战,彻底搞懂Nginx
    什么是Nginx?Nginx代码完全用C语言从头写成,已经移植到许多体系结构和操作系统,包括:Linux、FreeBSD、Solaris、MacOSX、AIX以及MicrosoftWindows。Nginx有自己的函数库,......
  • Keepalived+Nginx实现高可用负载均衡
    最近刚学习了负载均衡的知识,昨天实战了一下,但是遇到了一些奇怪的问题,查找无方之后,自己探索摸出了原因,所以今天就带大家实战一遍,避免走坑。提前准备VMware、CentOS7(两台......
  • 使用Docker来配置Nginx映射静态H5页面
    前提:需要预装docker1、拉取Nginx镜像$dockerpullnginx2、挂载镜像$dockerrun--namenginx-p80:80-dnginx3、查看挂载的镜像,获取CONTAINERID$dockerps......
  • 提高代码质量的 11 个高级 JavaScript 函数
    通过使用包括Debounce、Once和Memoize的函数,以及Pipe、Pick和Zip,来提高代码质量!介绍JavaScript是一种强大而多功能的编程语言,具有许多内置特性,可以帮助您编写......
  • docker 安装nginx1.22.1
    1,拉取镜像:dockerpullnginx:1.22.12,创建映射目录mkdir-p/docker/nginx3,创建临时容器用于复制配置信息dockerrun--namenginx-p80:80-dnginx:1.22.14,拷贝......
  • Nginx日志割接,生成周期日志
    #!/bin/bashlogs_path="/usr/local/nginx-1.14.2/logs/any/"pid_path="/usr/local/nginx-1.14.2/logs/nginx.pid"mv${logs_path}access.log${logs_path}access_$(dat......
  • CesiumJS PrimitiveAPI 高级着色入门 - 从参数化几何与 Fabric 材质到着色器 - 下篇
    目录3.使用GLSL着色器3.1.为Fabric材质添加自定义着色代码-Fabric材质的本质3.2.社区实现案例-泛光墙体和流动线材质3.3.直接定义外观对象的两个着色器3.4.*......
  • 详解Nginx配置文件nginx.conf的每行含义
     Nginx配置文件的位置随着安装方式的不同,所在的位置也会不同,通过yum/dnf方式安装,那配置文件是在/etc/nginx/nginx.conf;通过手动编译安装的话可以指定配置文件位置,不指定的......
  • centos下安装部署nginx
    1.在安装Nginx之前,要确保已经安装了需要的软件:gcc、pcre-devel、zlib-devel、openssl-devel。如果没有安装,执行下面命令。  yum-yinstallgccpcre-develzlib-deve......
  • 63. C高级-面向接口编程
    14面向接口编程14.1案例背景一般的企业信息系统都有成熟的框架。软件框架一般不发生变化,能自由的集成第三方厂商的产品。14.2案例需求要求在企业信息系统框架中集成第......