Nginx的版本平滑升級(jí)和回退的實(shí)現(xiàn)
前情提要:本篇博客將詳細(xì)介紹Nginx的版本平滑升級(jí)和回退的操作流程,通過本篇博客你可以實(shí)現(xiàn)Nginx業(yè)務(wù)不暫停的情況下完成Nginx的版本升級(jí)或回退。
系統(tǒng):RHEL9.3
nginx版本:nginx version: nginx/1.28.2
一、平滑升級(jí)流程介紹
有時(shí)候我們需要對(duì)Nginx版本進(jìn)行升級(jí)以滿足對(duì)其功能的需求,例如添加新模塊,需要新功能,而此時(shí) Nginx又在跑著業(yè)務(wù)無(wú)法停掉,這時(shí)我們就可能選擇平滑升級(jí)
平滑升級(jí)流程


- 將舊Nginx二進(jìn)制文件換成新Nginx程序文件(注意先備份)
- 向master進(jìn)程發(fā)送USR2信號(hào)
- master進(jìn)程修改pid文件名加上后綴.oldbin,成為nginx.pid.oldbin
- master進(jìn)程用新Nginx文件啟動(dòng)新master進(jìn)程成為舊master的子進(jìn)程,系統(tǒng)中將有新舊兩個(gè)Nginx主進(jìn)程共同提供Web服務(wù),當(dāng)前新的請(qǐng)求仍然由舊Nginx的worker進(jìn)程進(jìn)行處理,將新生成的master進(jìn) 程的PID存放至新生成的pid文件nginx.pid
- 向舊的Nginx服務(wù)進(jìn)程發(fā)送WINCH信號(hào),使舊的Nginx worker進(jìn)程平滑停止
- 向舊master進(jìn)程發(fā)送QUIT信號(hào),關(guān)閉老master,并刪除Nginx.pid.oldbin文件
- 如果發(fā)現(xiàn)升級(jí)有問題,可以回滾∶向老master發(fā)送HUP,向新master發(fā)送QUIT
二、平滑升級(jí)和回滾案例
2.1 平滑升級(jí)流程
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 # 查看兩個(gè)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命令復(fù)制過去 [root@Nginx sbin]# cp -p ~/nginx-1.29.6/objs/nginx ./nginx -f cp:是否覆蓋'./nginx'? yes # 輸入yes確認(rèn) [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 # 檢測(cè) [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 平滑升級(jí)
# 查看nginx的進(jìn)程 [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 平滑升級(jí)可執(zhí)行程序,將存儲(chǔ)有舊版本主進(jìn)程PID的文件重命名為nginx.pid.oldbin,并啟動(dòng)新的nginx # 此時(shí)兩個(gè)master的進(jìn)程都在運(yùn)行,只是舊的master不在監(jiān)聽,由新的master監(jiān)聽80 # 此時(shí)Nginx開啟一個(gè)新的master進(jìn)程,這個(gè)master進(jìn)程會(huì)生成新的worker進(jìn)程,這就是升級(jí)后的Nginx進(jìn)程,此時(shí)老的進(jìn)程不會(huì)自動(dòng)退出,但是當(dāng)接收到新的請(qǐng)求不作處理而是交給新的進(jìn)程處理。 [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 # 停止舊的進(jìn)程 # 向舊的master進(jìn)程發(fā)送WINCH信號(hào),讓它關(guān)閉自己的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進(jìn)程已經(jīng)被關(guān)閉 # 檢測(cè) [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 平滑回滾
# 如果升級(jí)的新版本發(fā)現(xiàn)問題需要回滾,可以重新拉起舊版本的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確認(rèn)覆蓋 # nginx重新加載配置文件 [root@Nginx sbin]# kill -HUP 5199 # 舊的主進(jìn)程ID,nginx主進(jìn)程收到信號(hào)后會(huì)重新加載配置文件并且逐個(gè)重啟worker進(jìn)程,使用新配置,不會(huì)中斷服務(wù) # 該命令等同于 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進(jìn)程又重新啟動(dòng) [root@Nginx sbin]# kill -WINCH 5293 # 回收新版本的worker進(jìn)程 [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 # 檢測(cè) [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平滑升級(jí)和回滾均完畢,更多相關(guān)Nginx 版本平滑升級(jí)和回退內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Nginx核心參數(shù)worker_processes的設(shè)置策略
在 Nginx 的配置文件中,worker_processes 可能是最不起眼的一個(gè)參數(shù),但它卻是決定服務(wù)器性能的基石,今天,我們就把這個(gè)參數(shù)徹底講透:它到底是什么?怎么設(shè)才最科學(xué),需要的朋友可以參考下2026-01-01
keepalived?+?nginx?實(shí)現(xiàn)高可用方案
這篇文章主要介紹了keepalived?+?nginx?實(shí)現(xiàn)高可用方案的相關(guān)資料,需要的朋友可以參考下2022-12-12
Nginx中404頁(yè)面的配置及AJAX請(qǐng)求返回404頁(yè)面的方法
404是請(qǐng)求頁(yè)面不存在的錯(cuò)誤代碼,在Nginx中有時(shí)處理jQuery中的ajax方法雖然能返回404頁(yè)面但錯(cuò)誤代碼卻返回200,針對(duì)此問題我們具體來(lái)看一下Nginx中404頁(yè)面的配置及AJAX請(qǐng)求返回404頁(yè)面的方法2016-05-05
Nginx服務(wù)器中配置404錯(cuò)誤頁(yè)面時(shí)一些值得注意的地方
這篇文章主要介紹了Nginx服務(wù)器中配置404錯(cuò)誤頁(yè)面時(shí)一些值得注意的地方,包括隱藏Nginx出錯(cuò)頁(yè)面及Header上的版本號(hào)的安全方法,需要的朋友可以參考下2016-01-01

