一、模拟环境:有客户访问
查看旧版本,下载新版本
[root@rocky86-101 ~]# wget http://nginx.org/download/nginx-1.23.1.tar.gz
[root@rocky86-101 ~]# ls
nginx-1.23.1.tar.gz
解压
[root@rocky86-101 ~]# tar xf nginx-1.23.1.tar.gz
服务器旧版本
[root@rocky86-101 ~]# nginx -v
nginx version: nginx/1.22.0
创建大文件,客户端下载,模拟Nginx有客户在访问
[root@rocky86-101 ~]# cd /apps/nginx/html
[root@rocky86-101 html]# dd if=/dev/zero of=test.img bs=1M count=100
客户端版本
[root@rocky86-100 ~]# curl -I 10.0.0.101
HTTP/1.1 200 OK
Server: nginx/1.22.0
Date: Sat, 17 Sep 2022 12:20:51 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Sat, 17 Sep 2022 11:21:15 GMT
Connection: keep-alive
ETag: "6325adab-267"
Accept-Ranges: bytes
客户端下载
[root@rocky86-100 ~]# wget --help|grep limit
-t, --tries=NUMBER set number of retries to NUMBER (0 unlimits)
--limit-rate=RATE limit download rate to RATE
[root@rocky86-100 ~]# wget --limit-rate=1024 http://10.0.0.101/test.img
平滑升级
[root@rocky86-101 ~]# cd nginx-1.23.1/
[root@rocky86-101 nginx-1.23.1]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
#打印nginx的版本信息、编译信息等
[root@rocky86-101 html]# nginx -V
nginx version: nginx/1.22.0
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-10) (GCC)
built with OpenSSL 1.1.1k FIPS 25 Mar 2021
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
#开始编译新版本
[root@rocky86-101 nginx-1.23.1]# ./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
#只要make无需要make install
[root@rocky86-101 nginx-1.23.1]# make
[root@rocky86-101 nginx-1.23.1]# ls objs/
autoconf.err Makefile nginx nginx.8 ngx_auto_config.h ngx_auto_headers.h ngx_modules.c ngx_modules.o src
[root@rocky86-101 nginx-1.23.1]# objs/nginx -v
nginx version: nginx/1.23.1
#备份旧版
[root@rocky86-101 nginx-1.23.1]# ls /apps/nginx/sbin/nginx
/apps/nginx/sbin/nginx
[root@rocky86-101 nginx-1.23.1]# cp -p /apps/nginx/sbin/nginx /opt/
#强制覆盖旧版文件
[root@rocky86-101 nginx-1.23.1]# cp objs/nginx /apps/nginx/sbin/nginx -f
#此时查看版本已经是新版本
[root@rocky86-101 nginx-1.23.1]# nginx -v
nginx version: nginx/1.23.1
#查看进程
[root@rocky86-101 nginx-1.23.1]# pstree -p |grep nginx
|-nginx(11558)---nginx(11560)
#查看进程
[root@rocky86-101 nginx-1.23.1]# ps auxf |grep nginx
root 14684 0.0 0.0 12140 1104 pts/1 S+ 20:41 0:00 \_ grep --color=auto nginx
root 11558 0.0 0.0 42572 844 ? Ss 19:34 0:00 nginx: master process /apps/nginx/sbin/nginx
nginx 11560 0.0 0.1 74680 5012 ? S 19:34 0:00 \_ nginx: worker process
#进程pid文件
[root@rocky86-101 nginx-1.23.1]# cat /apps/nginx/logs/nginx.pid
11558
#发送信号USR2 平滑升级可执行程序,将存储有旧版本主进程PID的文件重命名为nginx.pid.oldbin,并启动新的nginx
#此时两个master的进程都在运行,只是旧的master不在监听,由新的master监听80
#此时Nginx开启一个新的master进程,这个master进程会生成新的worker进程,这就是升级后的Nginx进程,此时老的进程不会自动退出,但是当接收到新的请求不作处理而是交给新的进程处理。
[root@rocky86-101 nginx-1.23.1]# kill -USR2 `cat /apps/nginx/logs/nginx.pid`
#旧版本pid文件会被备份
[root@rocky86-101 nginx-1.23.1]# ls /apps/nginx/logs/
access.log error.log nginx.pid nginx.pid.oldbin
#可以看到两个master,新的master是旧版master的子进程,并生成新版的worker进程
[root@rocky86-101 nginx-1.23.1]# pstree -p |grep nginx
|-nginx(11558)-+-nginx(14694)---nginx(14695)
| `-nginx(11560)
[root@rocky86-101 nginx-1.23.1]# ps auxf |grep nginx
root 14701 0.0 0.0 12140 1188 pts/1 S+ 20:46 0:00 \_ grep --color=auto nginx
root 11558 0.0 0.0 42572 2844 ? Ss 19:34 0:00 nginx: master process /apps/nginx/sbin/nginx
nginx 11560 0.0 0.1 74680 5012 ? S 19:34 0:00 \_ nginx: worker process
root 14694 0.0 0.2 42580 6112 ? S 20:43 0:00 \_ nginx: master process /apps/nginx/sbin/nginx
nginx 14695 0.0 0.1 74768 5020 ? S 20:43 0:00 \_ nginx: worker process
[root@rocky86-101 nginx-1.23.1]# cat /apps/nginx/logs/nginx.pid
14694
[root@rocky86-101 nginx-1.23.1]# cat /apps/nginx/logs/nginx.pid.oldbin
11558
#此时客户端访问的还是旧版本
[root@rocky86-100 ~]# curl -I 10.0.0.101
HTTP/1.1 200 OK
Server: nginx/1.22.0
#先关闭旧nginx的worker进程,而不关闭nginx主进程方便回滚
#向原Nginx主进程发送WINCH信号,它会逐步关闭旗下的工作进程(主进程不退出),这时所有请求都会由新版Nginx处理
[root@rocky86-101 nginx-1.23.1]# kill -WINCH `cat /apps/nginx/logs/nginx.pid.oldbin`
#新版接受新客户请求
[root@rocky86-100 ~]# curl -I 10.0.0.101
HTTP/1.1 200 OK
Server: nginx/1.23.1
#如果旧版worker进程有用户的请求,会一直等待处理完后才会关闭
#经过一段时间测试,新版本服务没问题,最后发送QUIT信号,退出老的master
[root@rocky86-101 nginx-1.23.1]# kill -QUIT `cat /apps/nginx/logs/nginx.pid.oldbin`
三、回滚
如果老版本的master的进程还存在可以回滚
#如果升级的版本发现问题需要回滚,可以发送HUP信号,重新拉起旧版本的worker
[root@rocky86-101 nginx-1.23.1]# kill -HUP `cat /apps/nginx/logs/nginx.pid.oldbin`
#关闭新版的master
[root@rocky86-101 nginx-1.23.1]# kill -QUIT `cat /apps/nginx/logs/nginx.pid`
#恢复旧版的文件
[root@rocky86-101 nginx-1.23.1]# cp /opt/nginx /apps/nginx/sbin/nginx
如果已经关闭了,可使用平滑升级的过程来回滚