Centos8平滑升级nginx1.20.2和回滚方案

#下载最新稳定版
[root@centos8 ~]# wget http://nginx.org/download/nginx-1.20.2.tar.gz
[root@centos8 ~]# tar xf nginx-1.20.2.tar.gz 
[root@centos8 ~]# cd nginx-1.20.2
#查看当前使用的版本及编译选项。
[root@centos8 nginx-1.20.2]# nginx -V
nginx version: nginx/1.18.0
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (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
#configure arguments后面是以前编译时的参数。现在编译使用一样的参数
[root@centos8 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
#只要make无需要make install
[root@centos8 nginx-1.20.2]# make
[root@centos8 nginx-1.20.2]# objs/nginx -v
nginx version: nginx/1.20.2
#查看两个版本
[root@centos8 nginx-1.20.2]# ll objs/nginx /apps/nginx/sbin/nginx 
-rwxr-xr-x 1 nginx nginx 7596448 Apr 10 21:17 /apps/nginx/sbin/nginx
-rwxr-xr-x 1 root  root  7731496 Apr 11 16:17 objs/nginx
#把之前的旧版的nginx命令备份
[root@centos8 nginx-1.20.2]# mv /apps/nginx/sbin/nginx{,.bak}
#把新版本的nginx命令复制过去
[root@centos8 nginx-1.20.2]# cp ./objs/nginx /apps/nginx/sbin/
#检测一下有没有问题
[root@centos8 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
#USR2 平滑升级可执行程序,将存储有旧版本主进程ID的文件重命名为nginx.pid.oldbin,跟着启动新nginx
[root@centos8 nginx-1.20.2]# kill -USR2 `cat /apps/nginx/run/nginx.pid`
[root@centos8 nginx-1.20.2]# ps aux|grep nginx
root       16532  0.0  0.1  42588  3860 ?        Ss   15:24   0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nobody     16918  0.0  0.2  77120  5168 ?        S    15:42   0:00 nginx: worker process
nobody     16919  0.0  0.2  77120  5168 ?        S    15:42   0:00 nginx: worker process
root       20335  0.0  0.2  42484  5368 ?        S    16:23   0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nobody     20336  0.0  0.2  77240  4956 ?        S    16:23   0:00 nginx: worker process
nobody     20337  0.0  0.2  77240  4996 ?        S    16:23   0:00 nginx: worker process
#先关闭旧nginx的worker进程,而不关闭老nginx master方便回滚(这时候有问题,可以选择回滚)
#向原Nginx主进程发送WINCH信号,它会逐步关闭旗下的worker(master不退出),这时所有请求都会由新版Nginx处理
[root@centos8 nginx-1.20.2]# kill -WINCH `cat /apps/nginx/run/nginx.pid.oldbin`
[root@centos8 nginx-1.20.2]# ps aux|grep nginx
root       16532  0.0  0.1  42588  3860 ?        Ss   15:24   0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
root       20335  0.0  0.2  42484  5368 ?        S    16:23   0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nobody     20336  0.0  0.2  77240  4956 ?        S    16:23   0:00 nginx: worker process
nobody     20337  0.0  0.2  77240  4996 ?        S    16:23   0:00 nginx: worker process
root       20645  0.0  0.0  12136  1144 pts/0    S+   16:34   0:00 grep --color=auto nginx
[root@centos8 nginx-1.20.2]# pstree -p|grep nginx
           |-nginx(16532)---nginx(20335)-+-nginx(20336)
           |                             `-nginx(20337)
#经过一段时间测试,新版本服务没问题,最后退出老的master
[root@centos8 nginx-1.20.2]# kill -QUIT `cat /apps/nginx/run/nginx.pid.oldbin`
#查看版本是不是已经是新版了
[root@centos8 nginx-1.20.2]# nginx -v
nginx version: nginx/1.20.2
[root@centos8 nginx-1.20.2]# curl -I 127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.20.2
Date: Mon, 11 Apr 2022 08:42:57 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Sun, 10 Apr 2022 13:17:26 GMT
Connection: keep-alive
ETag: "6252d8e6-264"
Accept-Ranges: bytes

#回滚
#如果升级的版本发现问题需要回滚,可以重新拉起旧版本的worker
[root@centos8 nginx-1.20.2]# kill -HUP `cat /apps/nginx/run/nginx.pid.oldbin`
#最后关闭新版的master
[root@centos8 nginx-1.20.2]# kill -QUIT `cat /apps/nginx/run/nginx.pid`

你可能感兴趣的:(nginx,centos,运维)