1分鐘搞定Nginx版本的平滑升級(jí)與回滾的方法
今天,我們來(lái)聊一聊,在企業(yè)實(shí)際生產(chǎn)環(huán)境中經(jīng)常遇到的一個(gè)情況,升級(jí)Nginx到新的版本和如何回滾至舊版本。
1、環(huán)境介紹
今天準(zhǔn)備的兩個(gè)nginx版本如下:
[root@nginx ~]# cd /download/nginx/ [root@nginx nginx]# ll total 1952 -rw-r--r-- 1 root root 981687 Oct 17 2017 nginx-1.12.2.tar.gz -rw-r--r-- 1 root root 1015384 Dec 4 09:58 nginx-1.14.2.tar.gz
2、編譯安裝新舊版本
編譯安裝nginx-1.12.2
[root@nginx nginx]# tar zxf nginx-1.12.2.tar.gz [root@nginx nginx]# cd nginx-1.12.2 [root@nginx nginx-1.12.2]# ./configure --prefix=/usr/local/nginx-1.12.2 [root@nginx nginx-1.12.2]# echo $? 0 [root@nginx nginx-1.12.2]# make && make install [root@nginx nginx-1.12.2]# echo $? 0 [root@nginx nginx-1.12.2]# ll /usr/local/nginx-1.12.2/ total 0 drwxr-xr-x 2 root root 333 Mar 1 09:01 conf drwxr-xr-x 2 root root 40 Mar 1 09:01 html drwxr-xr-x 2 root root 6 Mar 1 09:01 logs drwxr-xr-x 2 root root 19 Mar 1 09:01 sbin
編譯安裝nginx-1.14.2
[root@nginx ~]# cd /download/nginx/ [root@nginx nginx]# tar zxf nginx-1.14.2.tar.gz [root@nginx nginx]# cd nginx-1.14.2 [root@nginx nginx-1.14.2]# ./configure --prefix=/usr/local/nginx-1.14.2 [root@nginx nginx-1.14.2]# echo $? 0 [root@nginx nginx-1.14.2]# make && make install [root@nginx nginx-1.14.2]# echo $? 0 [root@nginx nginx-1.14.2]# ls -l /usr/local/nginx-1.14.2/ total 0 drwxr-xr-x 2 root root 333 Mar 1 09:03 conf drwxr-xr-x 2 root root 40 Mar 1 09:03 html drwxr-xr-x 2 root root 6 Mar 1 09:03 logs drwxr-xr-x 2 root root 19 Mar 1 09:03 sbin
到這里,兩個(gè)版本的nginx軟件已經(jīng)部署完成。
3、啟動(dòng)舊版本nginx
[root@nginx ~]# /usr/local/nginx-1.12.2/sbin/nginx -t nginx: the configuration file /usr/local/nginx-1.12.2/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx-1.12.2/conf/nginx.conf test is successful [root@nginx ~]# /usr/local/nginx-1.12.2/sbin/nginx [root@nginx ~]# ps -ef|grep nginx root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx nobody 6325 6324 0 09:06 ? 00:00:00 nginx: worker process root 6327 1244 0 09:06 pts/0 00:00:00 grep --color=auto nginx [root@nginx ~]# lsof -i :80 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 6324 root 6u IPv4 26324 0t0 TCP *:http (LISTEN) nginx 6325 nobody 6u IPv4 26324 0t0 TCP *:http (LISTEN)
4、升級(jí)到新版本
版本升級(jí)其實(shí)就是針對(duì)二進(jìn)制文件的升級(jí),過(guò)程如下:
[root@nginx ~]# /usr/local/nginx-1.12.2/sbin/nginx -v nginx version: nginx/1.12.2 [root@nginx ~]# cd /usr/local/nginx-1.12.2/sbin/ [root@nginx sbin]# mv nginx nginx-1.12.2 #首先備份原來(lái)的舊版本nginx二進(jìn)制文件 [root@nginx sbin]# cp /usr/local/nginx-1.14.2/sbin/nginx ./ #拷貝新版本的二進(jìn)制文件到當(dāng)前目錄
接下來(lái)進(jìn)行平滑升級(jí)操作
[root@nginx ~]# ps -ef|grep nginx root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx nobody 6325 6324 0 09:06 ? 00:00:00 nginx: worker process root 6338 1244 0 09:11 pts/0 00:00:00 grep --color=auto nginx [root@nginx ~]# kill -USR2 6324 [root@nginx ~]# ps -ef|grep nginx root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx nobody 6325 6324 0 09:06 ? 00:00:00 nginx: worker process root 6340 6324 0 09:12 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx nobody 6341 6340 0 09:12 ? 00:00:00 nginx: worker process root 6343 1244 0 09:12 pts/0 00:00:00 grep --color=auto nginx
這時(shí)新的master進(jìn)程已經(jīng)正常開(kāi)啟,但老的work進(jìn)程也存在,所以我們使用下面的命令,將老的work進(jìn)程發(fā)出平滑停止的信號(hào),如下:
[root@nginx ~]# kill -WINCH 6324 [root@nginx ~]# ps -ef|grep nginx root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx root 6340 6324 0 09:12 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx nobody 6341 6340 0 09:12 ? 00:00:00 nginx: worker process root 6346 1244 0 09:14 pts/0 00:00:00 grep --color=auto nginx
此時(shí),老的work進(jìn)程已經(jīng)停止,接下來(lái)我們測(cè)試是否能正常訪問(wèn):

可以正常訪問(wèn),其實(shí)這一平滑升級(jí)的動(dòng)作,對(duì)訪問(wèn)用戶來(lái)說(shuō)是完全感知不到,所以nginx熱部署就已經(jīng)完成了。
[root@nginx ~]# /usr/local/nginx-1.12.2/sbin/nginx -v nginx version: nginx/1.14.2
查看版本也是最新的版本,升級(jí)完成。
注:如果在版本升級(jí)完成后,沒(méi)有任何問(wèn)題,需要關(guān)閉老的master進(jìn)程的話,可以使用下面的命令:
kill -QUIT old_master_PID
5、版本回滾
對(duì)于升級(jí)來(lái)說(shuō),最難的不是升級(jí),而是回滾,因?yàn)樵趯?shí)際生產(chǎn)環(huán)境回滾的機(jī)率是存在,比如:新版本由于某些未知bug導(dǎo)致與現(xiàn)有應(yīng)用不兼容、或出現(xiàn)運(yùn)行不穩(wěn)定的情況等等。
所以,對(duì)運(yùn)維工程師來(lái)說(shuō),故障回滾是重點(diǎn)。
在上面的結(jié)果中,我們也能看到老的master進(jìn)程是一直存在,在沒(méi)有手工關(guān)閉前,它是不會(huì)自已關(guān)閉的,這種設(shè)計(jì)是有好處的,好處就是為了升級(jí)新版本后,如果出現(xiàn)問(wèn)題能及時(shí)快速的回滾到上一個(gè)穩(wěn)定版本。
[root@nginx ~]# ps -ef|grep nginx root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx root 6340 6324 0 09:12 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx nobody 6341 6340 0 09:12 ? 00:00:00 nginx: worker process root 6350 1244 0 09:23 pts/0 00:00:00 grep --color=auto nginx [root@nginx ~]# cd /usr/local/nginx-1.12.2/sbin/ [root@nginx sbin]# mv nginx nginx-1.14.2 [root@nginx sbin]# mv nginx-1.12.2 nginx [root@nginx sbin]# kill -USR1 6324 [root@nginx sbin]# ps -ef|grep nginx root 6324 1 0 09:06 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx root 6340 6324 0 09:12 ? 00:00:00 nginx: master process /usr/local/nginx-1.12.2/sbin/nginx nobody 6341 6340 0 09:12 ? 00:00:00 nginx: worker process root 6355 1244 0 09:24 pts/0 00:00:00 grep --color=auto nginx [root@nginx sbin]# ./nginx -v nginx version: nginx/1.12.2
從上面的結(jié)果發(fā)現(xiàn),已經(jīng)平滑的回滾的上一個(gè)版本,接下來(lái)測(cè)試是否能正常訪問(wèn):

一樣可以正常訪問(wèn),所以,這個(gè)回滾的操作對(duì)用戶來(lái)說(shuō)也是不可感知的。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Nginx實(shí)現(xiàn)if多重判斷配置方法示例
這篇文章主要介紹了Nginx實(shí)現(xiàn)if多重判斷配置方法示例,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-05-05
Nginx 502 Bad Gateway錯(cuò)誤原因及解決方案
這篇文章主要介紹了Nginx 502 Bad Gateway錯(cuò)誤原因及解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
Nginx禁止部分UserAgent訪問(wèn)的問(wèn)題解決
本文主要介紹了使用Nginx禁止特定UserAgent訪問(wèn)域名,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-02-02
nginx使用sticky基于cookie的會(huì)話保持方式
這篇文章主要介紹了nginx使用sticky基于cookie的會(huì)話保持方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
Nginx中使用gzip_http_version解決CDN只支持http 1.0問(wèn)題
這篇文章主要介紹了Nginx中使用gzip_http_version解決CDN只支持http 1.0問(wèn)題,問(wèn)題原因是在Header信息中看到Transfer-Encoding: chunked,使用本文方法就可以解決這個(gè)問(wèn)題,需要的朋友可以參考下2014-09-09
Nginx隱藏版本號(hào)與網(wǎng)頁(yè)緩存時(shí)間的方法
這篇文章主要介紹了Nginx優(yōu)化之隱藏版本號(hào)與網(wǎng)頁(yè)緩存時(shí)間的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11
Windows下用Nginx配置https服務(wù)器及反向代理的問(wèn)題
這篇文章主要介紹了Windows下用Nginx配置https服務(wù)器及反向代理的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09
三步配置輕量級(jí)服務(wù)器nginx小結(jié)
Nginx是一個(gè)安裝非常的簡(jiǎn)單 , 配置文件非常簡(jiǎn)潔,本文就來(lái)介紹一下三步配置輕量級(jí)服務(wù)器nginx,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08
nginx高可用集群的實(shí)現(xiàn)過(guò)程
這篇文章主要介紹了nginx高可用集群的實(shí)現(xiàn)過(guò)程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10

