74人参与 • 2026-05-09 • Mysql
前情提要:本篇博客将详细介绍nginx的版本平滑升级和回退的操作流程,通过本篇博客你可以实现nginx业务不暂停的情况下完成nginx的版本升级或回退。
系统:rhel9.3
nginx版本:nginx version: nginx/1.28.2
有时候我们需要对nginx版本进行升级以满足对其功能的需求,例如添加新模块,需要新功能,而此时 nginx又在跑着业务无法停掉,这时我们就可能选择平滑升级
平滑升级流程


[root@nginx ~]# wget https://nginx.org/download/nginx-1.29.6.tar.gz
[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
# 将旧的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
# 查看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
# 如果升级的新版本发现问题需要回滚,可以重新拉起旧版本的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 版本平滑升级和回退内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论