Nginx 平滑升级与回滚


参考

1分钟搞定 Nginx 版本的平滑升级与回滚
nginx 的平滑升级

版本

旧版本:nginx-1.16.0
新版本:nginx-1.20.1

Nginx 一般有两种情况需要升级

  • 一种是要为 nginx 添加新的模块
  • 另一种是确实要升级 nginx 的版本

原理
Nginx 的升级和回滚本质就是对 nginx 可执行文件的重新编译替换。

安装旧版本

编译安装旧版本

[root@localhost ~]# tar -zxvf nginx-1.16.0.tar.gz
[root@localhost ~]# cd nginx-1.16.0/
[root@localhost nginx-1.16.0]# ./configure --prefix=/opt/nginx/
[root@localhost nginx-1.16.0]# make && make install
[root@localhost nginx-1.16.0]# ll /opt/nginx/
总用量 4
drwxr-xr-x. 3 root root 4096 9月   2 10:08 conf
drwxr-xr-x. 2 root root   40 9月   2 10:07 html
drwxr-xr-x. 2 root root    6 9月   2 10:07 logs
drwxr-xr-x. 2 root root   19 9月   2 10:07 sbin

启动旧版本

[root@localhost ~]# /opt/nginx/sbin/nginx

查看 Nginx 版本

[root@localhost ~]# curl  -I 127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.16.0
Date: Thu, 02 Sep 2021 02:10:38 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Thu, 02 Sep 2021 02:07:56 GMT
Connection: keep-alive
ETag: "613031fc-264"
Accept-Ranges: bytes

旧版本添加新模块

查看旧版本的编译参数,configure arguments 后面是编译参数。

[root@localhost ~]# /opt/nginx/sbin/nginx -V
nginx version: nginx/1.16.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
configure arguments: --prefix=/opt/nginx/

重新解压旧版本

[root@localhost ~]# rm -rf nginx-1.16.0/
[root@localhost ~]# tar -zxvf nginx-1.16.0.tar.gz
[root@localhost ~]# cd nginx-1.16.0/

重新带参数编译(参数包括已安装的参数和将要添加的模块)

[root@localhost nginx-1.16.0]# ./configure --prefix=/opt/nginx/ --with-http_stub_status_module

make 编译,仅 make 编译,切记不能 make install,否则覆盖老版本安装了,我们这里只取编译后的新 nginx 执行文件。

[root@localhost nginx-1.16.0]# make

将编译后 objs 目录下的新 nginx 可执行文件替换掉旧版本的(替换之前记得先备份,养成好习惯)

[root@localhost nginx-1.16.0]# mv /opt/nginx/sbin/nginx /opt/nginx/sbin/nginx_old
[root@localhost nginx-1.16.0]# cp objs/nginx /opt/nginx/sbin/nginx

平滑升级命令

[root@localhost ~]# kill -USR2 `cat /opt/nginx/logs/nginx.pid`

查看当前 Nginx 进程,会有新旧两个 Nginx 进程

[root@localhost ~]# ps -ef | grep nginx
root      55606      1  0 11:44 ?        00:00:00 nginx: master process /opt/nginx/sbin/nginx
nobody    55607  55606  0 11:44 ?        00:00:00 nginx: worker process
root      58152  55606  0 11:45 ?        00:00:00 nginx: master process /opt/nginx/sbin/nginx
nobody    58153  58152  0 11:45 ?        00:00:00 nginx: worker process

优雅停止老 worker 进程,老进程的 worker 进程关闭

[root@localhost ~]# kill -WINCH `cat /opt/nginx/logs/nginx.pid.oldbin`
[root@localhost ~]# ps -ef | grep nginx
root      55606      1  0 11:44 ?        00:00:00 nginx: master process /opt/nginx/sbin/nginx
root      58152  55606  0 11:45 ?        00:00:00 nginx: master process /opt/nginx/sbin/nginx
nobody    58153  58152  0 11:45 ?        00:00:00 nginx: worker process

确定测试无误后,优雅退出老 master 进程

[root@localhost ~]# kill -QUIT `cat /opt/nginx/logs/nginx.pid.oldbin`
[root@localhost ~]# ps -ef | grep nginx
root      58152      1  0 11:45 ?        00:00:00 nginx: master process /opt/nginx/sbin/nginx
nobody    58153  58152  0 11:45 ?        00:00:00 nginx: worker process

查看新增加的模块

[root@localhost ~]# /opt/nginx/sbin/nginx -V
nginx version: nginx/1.16.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
configure arguments: --prefix=/opt/nginx/ --with-http_stub_status_module

旧版本升级到新版本

查看旧版本的编译参数(重要!!! 要保证新旧版本所安装的模块相同,这样 nginx 功能才不会出问题)

[root@localhost ~]# /opt/nginx/sbin/nginx -V
nginx version: nginx/1.16.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
configure arguments: --prefix=/opt/nginx/ --with-http_stub_status_module

解压新版本的 Nginx

[root@localhost ~]# tar -zxvf nginx-1.20.1.tar.gz
[root@localhost ~]# cd nginx-1.20.1/

带旧版本的参数编译

[root@localhost nginx-1.20.1]# ./configure --prefix=/opt/nginx/ --with-http_stub_status_module

make 编译,仅 make 编译,切记不能 make install,否则覆盖老版本安装了,我们这里只取编译后的新 nginx 执行文件。

[root@localhost nginx-1.20.1]# make

将编译后 objs 目录下的新 nginx 可执行文件替换掉旧版本的(替换之前记得先备份,养成好习惯)

[root@localhost nginx-1.20.1]# mv /opt/nginx/sbin/nginx /opt/nginx/sbin/nginx_1.16.0
[root@localhost nginx-1.20.1]# cp objs/nginx /opt/nginx/sbin/nginx

平滑升级命令

[root@localhost ~]# kill -USR2 `cat /opt/nginx/logs/nginx.pid`

优雅停止老工作进程

[root@localhost ~]# kill -WINCH `cat /opt/nginx/logs/nginx.pid.oldbin`
[root@localhost ~]# kill -QUIT `cat /opt/nginx/logs/nginx.pid.oldbin`

查看 Nginx 版本

[root@localhost ~]# curl  -I 127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.20.1
Date: Thu, 02 Sep 2021 03:54:20 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Thu, 02 Sep 2021 03:43:21 GMT
Connection: keep-alive
ETag: "61304859-264"
Accept-Ranges: bytes

新版本回滚到旧版本

回滚老版本无非就是将老版本的 nginx 执行文件替换成当前的,然后执行停止命令

[root@localhost ~]# mv /opt/nginx/sbin/nginx /opt/nginx/sbin/nginx_1.20.1
[root@localhost ~]# mv /opt/nginx/sbin/nginx_1.16.0 /opt/nginx/sbin/nginx

平滑升级命令

[root@localhost ~]# kill -USR2 `cat /opt/nginx/logs/nginx.pid`
[root@localhost ~]# kill -WINCH `cat /opt/nginx/logs/nginx.pid.oldbin`
[root@localhost ~]# kill -QUIT `cat /opt/nginx/logs/nginx.pid.oldbin`

查看 Nginx 版本

[root@localhost ~]# curl  -I 127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.16.0
Date: Thu, 02 Sep 2021 03:56:11 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Thu, 02 Sep 2021 03:43:21 GMT
Connection: keep-alive
ETag: "61304859-264"
Accept-Ranges: bytes

猜你喜欢

转载自blog.csdn.net/qq_39680564/article/details/120041775