实验环境
在8系统里已经安装了 nginx 1.18.0版本的nginx
平滑升级
[root@Centos8 ~]#tar xf nginx-1.20.1.tar.gz #下载后直接上传最新稳定版 进行解压
[root@Centos8 ~]#cd nginx-1.20.1 #切换到最新版本目录下
[root@Centos8 nginx-1.20.1]#/apps/nginx/sbin/nginx -V #查看当前使用的版本及编译选项。结果如下:
nginx version: nginx/1.18.0
built by gcc 8.4.1 20200928 (Red Hat 8.4.1-1) (GCC)
built with OpenSSL 1.1.1g FIPS 21 Apr 2020
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
#configure arguments后面是以前编译时的参数。现在编译使用一样的参数
#开始编译新版本
[root@Centos8 nginx-1.20.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 #按回车
[root@Centos8 nginx-1.20.1]#make #只要make无需要make install
[root@Centos8 nginx-1.20.1]#objs/nginx -v
nginx version: nginx/1.20.1
#查看两个版本
[root@Centos8 nginx-1.20.1]#ll objs/nginx /apps/nginx/sbin/nginx
-rwxr-xr-x 1 nginx nginx 7595800 Jun 12 10:18 /apps/nginx/sbin/nginx
-rwxr-xr-x 1 root root 7727952 Jun 12 10:33 objs/nginx
#把之前的旧版的nginx命令备份
[root@Centos8 nginx-1.20.1]#mv /apps/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
#把新版本的nginx命令复制过去
[root@Centos8 nginx-1.20.1]#cp ./objs/nginx /apps/nginx/sbin/
#检测一下有没有问题
[root@Centos8 nginx-1.20.1]#/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
#USR2 平滑升级可执行程序,将存储有旧版本主进程PID的文件重命名为nginx.pid.oldbin,并启动新的
nginx
#此时两个master的进程都在运行,只是旧的master不在监听,由新的master监听80
#此时Nginx开启一个新的master进程,这个master进程会生成新的worker进程,这就是升级后的Nginx进
程,此时老的进程不会自动退出,但是当接收到新的请求不作处理而是交给新的进程处理。
#可以看到两个master,新的master是旧版master的子进程,并生成新版的worker进程
[root@Centos8 nginx-1.20.1]#kill -USR2 `cat /apps/nginx/run/nginx.pid`
[root@Centos8 nginx-1.20.1]#ps auxf|grep nginx
root 14235 0.0 0.0 12132 1100 pts/0 S+ 10:39 0:00 | \_ grep --color=auto nginx
root 9010 0.0 0.1 42440 2760 ? Ss 10:20 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 9011 0.0 0.2 77096 5064 ? S 10:20 0:00 \_ nginx: worker process
root 14231 0.0 0.3 42444 6112 ? S 10:39 0:00 \_ nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 14232 0.0 0.2 77180 4672 ? S 10:39 0:00 \_ nginx: worker process
[root@Centos8 nginx-1.20.1]#lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 9010 root 8u IPv4 40011 0t0 TCP *:http (LISTEN)
nginx 9011 nginx 8u IPv4 40011 0t0 TCP *:http (LISTEN)
nginx 14231 root 8u IPv4 40011 0t0 TCP *:http (LISTEN)
nginx 14232 nginx 8u IPv4 40011 0t0 TCP *:http (LISTEN)
**#先关闭旧nginx的worker进程,而不关闭nginx主进程方便回滚
#向原Nginx主进程发送WINCH信号,它会逐步关闭旗下的工作进程(主进程不退出),这时所有请求都会由新
版Nginx处理**
[root@Centos8 nginx-1.20.1]#kill -WINCH `cat /apps/nginx/run/nginx.pid.oldbin`
#如果旧版worker进程有用户的请求,会一直等待处理完后才会关闭
[root@Centos8 nginx-1.20.1]#ps auxf|grep nginx
root 14548 0.0 0.0 12132 1056 pts/0 S+ 10:44 0:00 | \_ grep --color=auto nginx
root 9010 0.0 0.1 42440 2760 ? Ss 10:20 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
root 14231 0.0 0.3 42444 6112 ? S 10:39 0:00 \_ nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 14232 0.0 0.2 77180 4672 ? S 10:39 0:00 \_ nginx: worker process
[root@centos8 nginx-1.20.1]#pstree -p|grep nginx
|-nginx(8814)---nginx(12014)-+-nginx(12015)
| `-nginx(12016)
#经过一段时间测试,新版本服务没问题,最后退出老的maste
#查看版本是不是已经是新版
[root@centos8 nginx-1.20.1]#nginx -v
nginx version: nginx/1.20.1
[root@centos8 nginx-1.20.1]#curl -I 127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.20.1
Date: Mon, 07 Jun 2021 09:48:48 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 07 Jun 2021 08:28:12 GMT
Connection: keep-alive
ETag: "60bdd89c-264"
Accept-Ranges: bytes
**#回滚
#如果升级的版本发现问题需要回滚,可以重新拉起旧版本的worker**
[root@centos8 nginx-1.20.1]#kill -HUP `cat /apps/nginx/run/nginx.pid.oldbin`
[root@centos8 nginx-1.20.1]#pstree -p |grep nginx
|-nginx(8814)-+-nginx(12014)-+-nginx(12015)
| | `-nginx(12016)
| |-nginx(12090)
| `-nginx(12091)
#最后关闭新版的master
[root@centos8 nginx-1.20.1]#kill -QUIT `cat /apps/nginx/run/nginx.pid`