nginx在线热升级

本文详细介绍了如何在不中断服务的情况下对Nginx进行热升级,包括查看原编译参数、配置编译安装新版本、生成新master进程、优雅退出老worker进程以及在出现问题时如何回滚到原版本。整个过程确保了Nginx服务的连续性和稳定性。
摘要由CSDN通过智能技术生成

nginx不停机升级

查看原编译参数:

升级一般是添加新的模块,或者升级版本,所以要参考以前编译的模块,把模块信息预编译进去;如果不添加,那么以前的模块就不能使用了

[root@localhost ~]# /usr/local/nginx/sbin/nginx -V

配置/编译/安装

./configure  --prefix=/usr/local/nginx --with-http_stub_status_module

make && make install

#make install会帮助/usr/local/nginx/sbin/nginx替换nginx文件,这时候存在两个二进制文件nginx和nginx.old

生成新的master进程

给正在运行nginx的master进程发送一个信号,告诉master要热部署,做一次版本升级,这个信号是USER2,USER2信号用来传递热部署的信号

kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`

[root@localhost nginx-1.16.0]# ps -ef |grep "[n]ginx"
root 8054 1 0 21:07 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 8097 8054 0 21:09 ? 00:00:00 nginx: worker process
root 8134 8054 0 21:13 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 8135 8134 0 21:13 ? 00:00:00 nginx: worker process

#此时Nginx开启一个新的master进程,这个master进程会生成新的worker进程,这就是升级后的Nginx进程,此时老的进程不会自动退出,但是当接收到新的请求不作处理而是交给新的进程处理。

优雅退出老worker进程:

向老的master进程发信号,告诉它关闭其worker进程:

[root@localhost nginx-1.16.0]# kill -WINCH 8054

 

#看到老的worker进程已经退出,老的master进程还在:说明所有的请求已经切换到新升级好的nginx中

[root@localhost nginx-1.16.0]# ps -ef |grep "[n]ginx"
root 8054 1 0 21:07 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
root 8134 8054 0 21:13 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 8135 8134 0 21:16 ? 00:00:00 nginx: worker process

升级完成

经过一段时间测试,服务器没问题,退出老的master:

[root@localhost nginx-1.16.0]# kill -QUIT 8054

 

[root@localhost nginx-1.16.0]# ps -ef |grep "[n]ginx"

root 8134 1 0 21:24 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx

nginx 8135 8134 0 21:24 ? 00:00:00 nginx: worker process

回滚:升级过程有问题,退回原版本

旧版本的主进程不要kill关闭,以防万一升级失败还可以进行回滚,向老的master进程发送命令,再将新版本关掉。所以第五步的老的master进程是不会自动退出的,保留着,允许我们做版本的回退

重新拉起老的worker进程:

注意回滚是在老master未退出时才能做的
如果完全升级后,需要停止nginx服务,将nginx.old重命名为nginx在启动nginx服务

[root@localhost nginx-1.16.0]# kill -HUP 8054

 

[root@localhost nginx-1.16.0]# ps -ef |grep "[n]ginx"
root 8054 1 0 21:07 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
root 8134 8054 0 21:13 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 8135 8134 0 21:16 ? 00:00:00 nginx: worker process
nginx 8154 8054 1 21:19 ? 00:00:00 nginx: worker process
退出新的master进程:
 [root@localhost nginx-1.16.0]# kill -QUIT `cat /usr/local/nginx/logs/nginx.pid`

[root@localhost nginx-1.16.0]# ps -ef |grep "[n]ginx"
root 8054 1 0 21:07 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 8154 8054 0 21:19 ? 00:00:00 nginx: worker process
换回nginx文件
先删除新的nginx二进制文件:

rm -rf /usr/local/nginx/sbin/nginx

还原老的nginx 二进制文件:

mv /usr/local/nginx/sbin/nginx.old /usr/local/nginx/sbin/nginx
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WYQXLGLM

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值