nginx平滑升级及信号使用

1信号

nginx 命令支持向其发送信号,实现不同功能

nginx 当做单独命令使用有以下选项

[root@localhost ~]# nginx -h   显示帮助信息
nginx version: nginx/1.18.0
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
​
​
Options:
  -?,-h         : 打印帮助信息
  -v            : 显示 Nginx 的版本信息
  -V            : 显示详细的 Nginx 版本信息
  -t            : 检测配置文件语法的正确性并打印结果
  -q            : 禁止打印错误信息到标准输出
  -s signal     : 向主进程发送信号,signal 可以是 stop、quit、reopen 或 reload
  -p prefix     : 设置 Nginx 的安装路径(默认为 /usr/local/nginx/)
  -c filename   : 设置 Nginx 的配置文件路径(默认为 /usr/local/nginx/conf/nginx.conf)
  -g directives : 设置全局指令(例如设置 worker_processes 和 worker_connections)
​

image-20230824193103338

1 显示版本

[root@localhost ~]# nginx -v
nginx version: nginx/1.18.0
​

image-20230824193244891

2显示编译详细情况 模块等信息

[root@localhost ~]# nginx -V
# -V 大v
​

image-20230824193353571

3发送信号

kill -l  看信号大全
nginx -h   中可以看到的信号较少
s signal     : send signal to a master process: stop, quit, reopen, reload
​
可以使用man手册来查看详细的信号 如果没安装,去源码包里找到man文件
man   路径/nginx.8      不加路径打不开man帮助
stop        SIGTERM        直接停止
quit        SIGQUIT        优雅的退出:有人在访问不会结束进程
reopen      SIGUSR1        分割日志
reload      SIGHUP         重新加载配置文件
            SIGHUP           Reload configuration, start the new worker process with a new configuration, and
                             gracefully shut down old worker processes.
            SIGQUIT          Shut down gracefully.  优雅的关闭:有人在访问不会结束进程
            SIGUSR1          Reopen log files.       重新分割日志
            SIGUSR2          Upgrade the nginx executable on the fly.  运行中升级
            SIGWINCH         Shut down worker processes gracefully.    优雅的关闭worker进程,work进程负责处理请求,还有请求不会关闭

image-20230824193732956

实例:

nginx -s   stop   #立即关闭nginx
nginx -s   quit   #优雅退出   不影响业务的状态下退出  等待业务结束后退出
nginx -s   reload #重新加载  

USR1分割日志

[root@localhost logs]# cd /apps/nginx/logs/
[root@localhost logs]# ls
access.log  error.log  nginx.pid
[root@localhost logs]# mv access.log access.log.bak
[root@localhost logs]# touch access.log
#此时日志不会写入到新文件
​
#需要给master 进程发送  USR1信号
​
[root@localhost logs]# cd /apps/nginx/logs/
#却换到日志目录下,默认在安装目录的logs下
[root@localhost logs]# ls
access.log  error.log  nginx.pid
[root@localhost logs]# mv access.log access.log.bak
#将原来的日志文件移动改名
[root@localhost logs]# touch access.log
#创建同名文件
[root@localhost logs]# ps aux |grep nginx
root      18808  0.0  0.0  46156  1144 ?        Ss   12:56   0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx     18809  0.0  0.1  48688  1992 ?        S    12:56   0:00 nginx: worker process
root      79419  0.0  0.0 112680   980 pts/0    S+   13:55   0:00 grep --color=auto nginx
[root@localhost logs]# kill -s USR1 18808
使用kill需要进程id
[root@localhost logs]# nginx -s reopen
使用nginx不需要进程号
​

image-20230825140003077

4 指定配置 不已配置文件中的为准

nginx -g 指定配置 不已配置文件中的为准

nginx -g 'user zhangsan;'   已张三身份运行,默认是以nginx身份
要先关闭nginx服务,执行nginx -g 'user zhangsan;' 执行后服务自动开启
nginx -g 'daemon off;'    前台运行命令

5 检查语法格式

nginx -t

2升级 nginx1.18 nginx1.20

 

image-20230214152020207

  1. 将旧Nginx文件换成新Nginx文件(注意备份)

  2. 向master进程发送USR2信号

  3. master进程修改pid文件名,加后缀.oldbin

  4. master进程用新Nginx文件启动新master进程,系统中将有新旧两个Nginx主进程共同提供Web服务

  5. 向旧的Nginx服务进程发送WINCH信号,使旧的Nginx worker进程平滑停止,并删除Nginx.pid.oldbin文件

  6. 向旧master进程发送QUIT信号,关闭老master

  7. 如果发现升级有问题,可以回滚向老master发送HUP,向新master发送QUIT

[root@localhost ~]#ps aux |grep nginx
#先查看是否开启nginx

image-20230825145024487

[root@localhost ~]#vim /apps/nginx/conf/nginx.conf
#开启 两核
#user  nobody;
worker_processes  2;  第三行
#worker_processes  1 原来是1核
​
[root@localhost ~]#nginx -s reload
#重新加载配置文件

image-20230825145248479

[root@localhost ~]#ps aux |grep nginx
##此处多了一个子进程

image-20230825145530380

[root@localhost ~]#ps auxf |grep nginx
#查看进程树

image-20230825145705769

[root@localhost ~]#wget https://nginx.org/download/nginx-1.20.2.tar.gz -P /usr/local/src/
#下载安装包到src目录
​
[root@localhost ~]#cd /usr/local/src/
[root@localhost src]#ls
nginx-1.20.2.tar.gz
[root@localhost src]#tar xf nginx-1.20.2.tar.gz 
[root@localhost src]#cd nginx-1.20.2/
[root@localhost nginx-1.20.2]#ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src

image-20230825151101542

这时需要重新编译安装   ./configur   安装参数基本一致  这时可以使用 nginx -V  查看
​
#如果 有新模块在后添加即可
[root@localhost nginx-1.20.2]# nginx -V
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
​

image-20230825151248898

[root@localhost nginx-1.20.2]#./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
#重新编译

image-20230825152554902

[root@localhost nginx-1.20.2]#make
###########注意不要执行  make install

image-20230825152713806

[root@localhost objs]#cd objs    
#此文件夹中有新版本的nginx  运行程序
[root@localhost nginx-1.20.2]#objs/nginx -v
#查看版本
nginx version: nginx/1.20.2
​

image-20230825153039027

[root@localhost nginx-1.20.2]#mv /apps/nginx/sbin/nginx   /apps/nginx/sbin/nginx.bak
#将低版本的nginx主程序改名
​
[root@localhost nginx-1.20.2]#cp objs/nginx /apps/nginx/sbin/
#将新版本 拷入进去
[root@localhost nginx-1.20.2]# ls /apps/nginx/sbin/
nginx  nginx.bak
​
​
[root@localhost nginx-1.20.2]#/apps/nginx/sbin/nginx -t
#检查下语法问题
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful

image-20230825155439530

[root@localhost nginx-1.20.2]#kill -USR2 `cat /apps/nginx/logs/nginx.pid`
#发送 2 信号   信号在 man手册中可以看到
`cat /apps/nginx/logs/nginx.pid`是存放master pid的位置
#kill -USR2  SIGUSR2动态升级nginx可执行文件。
​
SIGNALS
     The master process of nginx can handle the following signals:
​
     SIGINT, SIGTERM  Shut down quickly.
     SIGHUP           Reload configuration, start the new worker process with a new configuration,
                      and gracefully shut down old worker processes.
     SIGQUIT          Shut down gracefully.
     SIGUSR1          Reopen log files.
     SIGUSR2          Upgrade the nginx executable on the fly.
     #                飞行中升级
     SIGWINCH         Shut down worker processes gracefully.
​
     While there is no need to explicitly control worker processes normally, they support some sig‐
     nals too:
​
     SIGTERM          Shut down quickly.
     SIGQUIT          Shut down gracefully.
     SIGUSR1          Reopen log files.
​

image-20230825160930809

 

[root@localhost nginx-1.20.2]#ps auxf|grep nginx
#生成新的master

image-20230825161353651

[root@localhost nginx-1.20.2]#lsof -i :80
#查看谁在监听 80

image-20230825161507632

检验
[root@localhost html]#dd if=/dev/zero of=/apps/nginx/html/m.img bs=1G count=10
记录了10+0 的读入
记录了10+0 的写出
10737418240字节(11 GB)已复制,16.1164 秒,666 MB/秒
[root@localhost html]#ls
50x.html  index.html  m.img
[root@localhost html]#pwd
/apps/nginx/html

image-20230825161855248

#开启新机器下载
[root@localhost data]#wget --limit-rate=1M http://192.168.1.100/m.img
--2023-08-25 16:37:01--  http://192.168.1.100/m.img
正在连接 192.168.1.100:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:10737418240 (10G) [application/octet-stream]
正在保存至: “m.img”
​
 1% [                                                                             ] 135,512,064 1.00MB/s 剩余 2h 48m
正在保存至: “m.img”

image-20230825163838075

#回到网页服务器
[root@localhost html]#ss -ntap|grep 80
#查看那个进程在管理 下载

image-20230825164246951

 

[root@localhost html]# ls /apps/nginx/logs/
access.log  error.log  nginx.pid  nginx.pid.oldbin
#会有 新老两个进程
nginx.pid  nginx.pid.oldbin
[root@localhost logs]# cat nginx.pid
111129
#新版本nginxpid
[root@localhost logs]# cat nginx.pid.oldbin
18808
#旧版本nginxpid
[root@localhost logs]#
​
​

 

image-20230825164705427

[root@localhost logs]# kill -WINCH `cat /apps/nginx/logs/nginx.pid.oldbin`
#优雅关闭老进程的  worker 进程
​
#再开启一台服务器测试 是否是新的进程 在下载
[root@localhost ~]# wget --limit-rate=1M http://192.168.1.100/m.img
--2023-08-25 16:53:46--  http://192.168.1.100/m.img
正在连接 192.168.1.100:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:10737418240 (10G) [application/octet-stream]
正在保存至: “m.img.2”
​
 2% [=>                                                                                                 ] 296,308,152 1.00MB/s 剩余 2h 45m 
 
 #回到网页服务器可以看到  
 [root@localhost logs]# ss -natp | grep *:80
​
测试一段时间无问题  就可以了,如果断掉第一个 下载 ,老的进程就关闭了
[root@localhost html]#pstree -p |grep nginx
#查看进程关系     1.18   
[root@localhost sbin]# ps aux |grep nginx
​

image-20230825170943799

image-20230825170929651

3回滚

[root@localhost man]#kill -HUP `cat /apps/nginx/run/nginx.pid.oldbin`
#唤起老的进程
[root@localhost man]#ps aux|grep nginx
#又有两个master

image-20230825171253834

#优雅退出新进程
kill -QUIT `cat /apps/nginx/run/nginx.pid`   新的进程
[root@localhost sbin]# curl -I 127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.18.0
Date: Fri, 25 Aug 2023 09:14:47 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Thu, 24 Aug 2023 10:40:27 GMT
Connection: keep-alive
ETag: "64e7339b-264"
Accept-Ranges: bytes
​
#旧的nginx服务被唤醒使用
​

image-20230825171619242