Nginx 平滑升级与回滚

时间:2022-07-27
本文章向大家介绍Nginx 平滑升级与回滚,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

环境

首先准备两个版本不一样Nginx

https://nginx.org/download/nginx-1.18.0.tar.gz
https://nginx.org/download/nginx-1.14.2.tar.gz

下载Nginx

[root@localhost down]# https://nginx.org/download/nginx-1.18.0.tar.gz
[root@localhost down]# https://nginx.org/download/nginx-1.14.2.tar.gz
[root@localhost down]# ll 
总用量 2008
-rw-r--r--. 1 root root 1015384 12月  4 2018 nginx-1.14.2.tar.gz
-rw-r--r--. 1 root root 1039530 4月  21 22:33 nginx-1.18.0.tar.gz

安装依赖包

yum install -y wget gcc gcc-c++ make pcre pcre-deve zilib zlib-devel openssl-devel

编译安装旧版本Nginx

tar zxf nginx-1.14.2.tar.gz 
cd nginx-1.14.2
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module
make && make install

编译新版本Nginx

tar zxf nginx-1.18.0.tar.gz 
cd nginx-1.18.0
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module
make  #千万不要输入make install命令,否则会覆盖

Nginx 平滑升级

启动旧版本Nginx

/usr/local/nginx/sbin/nginx 

备份旧版本Nginx二进制文件

[root@localhost /]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# cp nginx{,.bak}
[root@localhost sbin]# ls
nginx  nginx.bak
#复制新版本二进制文件到当前目录
[root@localhost sbin]# cp /down/nginx-1.18.0/objs/nginx ./nginx

发送USR2信号

[root@localhost sbin]# ps -ef|grep nginx
root     21951     1  0 00:30 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody   21952 21951  0 00:30 ?        00:00:00 nginx: worker process
root     21954  1436  0 00:30 pts/1    00:00:00 grep --color=auto nginx

#向主进程master发送USR2信号,Nginx会启动一个新版本的master进程和对应工作进程,和旧版一起处理请求
[root@localhost sbin]# kill -USR2 21951

[root@localhost sbin]# ps -ef|grep nginx
root     21951     1  0 00:30 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody   21952 21951  0 00:30 ?        00:00:00 nginx: worker process
root     21961 21951  0 00:31 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody   21962 21961  0 00:31 ?        00:00:00 nginx: worker process
root     21964  1436  0 00:31 pts/1    00:00:00 grep --color=auto nginx

发送WINCH信号

#向旧的Nginx主进程master发送WINCH信号,它会逐步关闭自己的工作进程(主进程不退出),这时所有请求都会由新版Nginx处理
[root@localhost sbin]# kill -WINCH 21951
[root@localhost sbin]# ps -ef|grep nginx
root     21951     1  0 00:30 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
root     21961 21951  0 00:31 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody   21962 21961  0 00:31 ?        00:00:00 nginx: worker process
root     21993  1436  0 00:41 pts/1    00:00:00 grep --color=auto nginx
[root@localhost sbin]# curl -I 127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.18.0
Date: Sat, 10 Oct 2020 16:43:13 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Sat, 10 Oct 2020 16:28:21 GMT
Connection: keep-alive
ETag: "5f81e125-264"
Accept-Ranges: bytes

版本回滚

#如果这时需要回退继续使用旧版本,可向旧的Nginx主进程发送HUP信号,它会重新启动工作进程,仍使用旧版配置文件,然后可以将新版Nginx进程杀死
[root@localhost sbin]# kill -HUP 21951

[root@localhost sbin]# curl -I 127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Sat, 10 Oct 2020 16:53:22 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Sat, 10 Oct 2020 16:28:21 GMT
Connection: keep-alive
ETag: "5f81e125-264"
Accept-Ranges: bytes