Nginx的版本平滑升级和回退的实现
更新时间:2026年05月09日 09:32:26 作者:两只龙宝
有时候我们需要对Nginx版本进行升级以满足对其功能的需求,而此时 Nginx又在跑着业务无法停掉,这时我们就可能选择平滑升级平滑升级,本文就来详细的介绍一下Nginx的版本平滑升级和回退的实现,感兴趣的可以了解一下
前情提要:本篇博客将详细介绍Nginx的版本平滑升级和回退的操作流程,通过本篇博客你可以实现Nginx业务不暂停的情况下完成Nginx的版本升级或回退。
系统:RHEL9.3
nginx版本:nginx version: nginx/1.28.2
一、平滑升级流程介绍
有时候我们需要对Nginx版本进行升级以满足对其功能的需求,例如添加新模块,需要新功能,而此时 Nginx又在跑着业务无法停掉,这时我们就可能选择平滑升级
平滑升级流程


- 将旧Nginx二进制文件换成新Nginx程序文件(注意先备份)
- 向master进程发送USR2信号
- master进程修改pid文件名加上后缀.oldbin,成为nginx.pid.oldbin
- master进程用新Nginx文件启动新master进程成为旧master的子进程,系统中将有新旧两个Nginx主进程共同提供Web服务,当前新的请求仍然由旧Nginx的worker进程进行处理,将新生成的master进 程的PID存放至新生成的pid文件nginx.pid
- 向旧的Nginx服务进程发送WINCH信号,使旧的Nginx worker进程平滑停止
- 向旧master进程发送QUIT信号,关闭老master,并删除Nginx.pid.oldbin文件
- 如果发现升级有问题,可以回滚∶向老master发送HUP,向新master发送QUIT
二、平滑升级和回滚案例
2.1 平滑升级流程
2.1.1 下载高版本nginx
[root@Nginx ~]# wget https://nginx.org/download/nginx-1.29.6.tar.gz
2.1.2 解压并编译nginx隐藏版本
[root@Nginx ~]# tar zxf nginx-1.29.6.tar.gz [root@Nginx ~]# cd nginx-1.29.6/ [root@Nginx nginx-1.29.6]# ./configure --prefix=/usr/local/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@Nginx nginx-1.29.6]# make # 查看两个nginx版本 [root@Nginx nginx-1.29.6]# ll objs/nginx /usr/local/nginx/sbin/nginx -rwxr-xr-x 1 root root 6097248 3月 22 15:53 objs/nginx -rwxr-xr-x 1 root root 5893792 3月 22 14:03 /usr/local/nginx/sbin/nginx
2.1.3 nginx命令切换
# 将旧的nginx命令备份 [root@Nginx nginx-1.29.6]# cd /usr/local/nginx/sbin/ [root@Nginx sbin]# cp nginx nginx.28.bak # 将新的nginx命令复制过去 [root@Nginx sbin]# cp -p ~/nginx-1.29.6/objs/nginx ./nginx -f cp:是否覆盖'./nginx'? yes # 输入yes确认 [root@Nginx sbin]# ll 总用量 11712 -rwxr-xr-x 1 root root 6097248 3月 22 15:53 nginx -rwxr-xr-x 1 root root 5893792 3月 22 16:49 nginx.28.bak # 检测 [root@Nginx sbin]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
2.1.4 平滑升级
# 查看nginx的进程 [root@Nginx sbin]# ls /usr/local/nginx/logs/ access.log error.log nginx.pid [root@Nginx sbin]# ps aux | grep nginx root 5199 0.0 0.0 9916 932 ? Ss 16:47 0:00 nginx: master process /usr/local/nginx/sbin/nginx nginx 5200 0.0 0.1 13812 5268 ? S 16:47 0:00 nginx: worker process root 5292 0.0 0.0 6636 2216 pts/0 S+ 16:50 0:00 grep --color=auto nginx [root@Nginx sbin]# kill -USR2 5199 # USR2 平滑升级可执行程序,将存储有旧版本主进程PID的文件重命名为nginx.pid.oldbin,并启动新的nginx # 此时两个master的进程都在运行,只是旧的master不在监听,由新的master监听80 # 此时Nginx开启一个新的master进程,这个master进程会生成新的worker进程,这就是升级后的Nginx进程,此时老的进程不会自动退出,但是当接收到新的请求不作处理而是交给新的进程处理。 [root@Nginx sbin]# ps aux | grep nginx root 5199 0.0 0.0 9916 2528 ? Ss 16:47 0:00 nginx: master process /usr/local/nginx/sbin/nginx nginx 5200 0.0 0.1 13812 5268 ? S 16:47 0:00 nginx: worker process root 5293 0.0 0.1 9964 6152 ? S 16:50 0:00 nginx: master process /usr/local/nginx/sbin/nginx nginx 5294 0.0 0.1 13872 4996 ? S 16:50 0:00 nginx: worker process root 5296 0.0 0.0 6636 2212 pts/0 S+ 16:51 0:00 grep --color=auto nginx [root@Nginx sbin]# ls /usr/local/nginx/logs/ access.log error.log nginx.pid nginx.pid.oldbin # 停止旧的进程 # 向旧的master进程发送WINCH信号,让它关闭自己的worker [root@Nginx sbin]# kill -WINCH 5199 [root@Nginx sbin]# ps aux | grep nginx root 5199 0.0 0.0 9916 2528 ? Ss 16:47 0:00 nginx: master process /usr/local/nginx/sbin/nginx root 5293 0.0 0.1 9964 6152 ? S 16:50 0:00 nginx: master process /usr/local/nginx/sbin/nginx nginx 5294 0.0 0.1 13872 4996 ? S 16:50 0:00 nginx: worker process root 5307 0.0 0.0 6636 2192 pts/0 S+ 16:51 0:00 grep --color=auto nginx # 可见旧的worker进程已经被关闭 # 检测 [root@Nginx sbin]# curl -I localhost HTTP/1.1 200 OK Server: nginx/1.29.6 # 新版本生效 Date: Sun, 22 Mar 2026 08:32:16 GMT Content-Type: text/html Content-Length: 615 Last-Modified: Sun, 22 Mar 2026 06:03:42 GMT Connection: keep-alive ETag: "69bf863e-267" Accept-Ranges: bytes
2.2 平滑回滚
# 如果升级的新版本发现问题需要回滚,可以重新拉起旧版本的worker # 备份新版nginx [root@Nginx sbin]# cp nginx nginx.29.bak [root@Nginx sbin]# ls nginx nginx.28.bak nginx.29.bak [root@Nginx sbin]# mv nginx.28.bak nginx mv:是否覆盖'nginx'? yes # 输入yes确认覆盖 # nginx重新加载配置文件 [root@Nginx sbin]# kill -HUP 5199 # 旧的主进程ID,nginx主进程收到信号后会重新加载配置文件并且逐个重启worker进程,使用新配置,不会中断服务 # 该命令等同于 nginx -s reload 或者 systemctl reload nginx [root@Nginx sbin]# ps aux | grep nginx root 5199 0.0 0.0 9916 2528 ? Ss 16:47 0:00 nginx: master process /usr/local/nginx/sbin/nginx root 5293 0.0 0.1 9964 6152 ? S 16:50 0:00 nginx: master process /usr/local/nginx/sbin/nginx nginx 5294 0.0 0.1 13872 4996 ? S 16:50 0:00 nginx: worker process nginx 5325 0.0 0.1 13812 4756 ? S 16:58 0:00 nginx: worker process root 5329 0.0 0.0 6636 2212 pts/0 S+ 16:58 0:00 grep --color=auto nginx # 可见旧版本的worker进程又重新启动 [root@Nginx sbin]# kill -WINCH 5293 # 回收新版本的worker进程 [root@Nginx sbin]# ps aux | grep nginx root 5199 0.0 0.0 9916 2528 ? Ss 16:47 0:00 nginx: master process /usr/local/nginx/sbin/nginx root 5293 0.0 0.1 9964 6152 ? S 16:50 0:00 nginx: master process /usr/local/nginx/sbin/nginx nginx 5325 0.0 0.1 13812 4756 ? S 16:58 0:00 nginx: worker process root 5331 0.0 0.0 6636 2212 pts/0 S+ 17:00 0:00 grep --color=auto nginx # 检测 [root@Nginx sbin]# curl -I localhost HTTP/1.1 200 OK Server: nginx/1.28.2 # 可见版本回退成功 Date: Sun, 22 Mar 2026 09:01:15 GMT Content-Type: text/html Content-Length: 615 Last-Modified: Sun, 22 Mar 2026 08:47:28 GMT Connection: keep-alive ETag: "69bfaca0-267" Accept-Ranges: bytes
至此nginx平滑升级和回滚均完毕,更多相关Nginx 版本平滑升级和回退内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
nginx中$host、$http_host和$proxy_host区别小结
本文主要介绍了nginx中$host、$http_host和$proxy_host区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2024-09-09
Debian系统下为PHP程序配置Nginx服务器的基本教程
这篇文章主要介绍了Debian系统下为PHP程序配置Nginx服务器的基本教程,这里使用到了FastCGI和php-fpm,需要的朋友可以参考下2015-12-12


最新评论