nginx平滑升級(jí)與回滾

1.安裝配置1.6版本的 nginx

[root@localhost ~]# yum install -y gcc gcc-c++ pcre-devel openssl-devel zlib-devel 

[root@localhost ~]# tar xzf nginx-1.6.3.tar.gz -C /usr/local/ 

[root@localhost ~]# cd /usr/local/nginx-1.6.3

[root@localhost nginx-1.6.3]# ./configure --prefix=/usr/local/nginx --user=nginx -group=nginx --with-http_stub_status_module 

[root@localhost nginx-1.6.3]# make && make install 

[root@localhost nginx-1.6.3]# useradd -M -s /sbin/nologin nginx 

[root@localhost nginx-1.6.3]# /usr/local/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 

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

2.查看版本和模塊

[root@localhost nginx-1.6.3]# /usr/local/nginx/sbin/nginx -V 
nginx version: nginx/1.6.3 built 
by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --withhttp_stub_status_module 

3.訪問(wèn)驗(yàn)證

[root@localhost nginx-1.6.3]# echo "nginx1.6" > /usr/local/nginx/html/index.html 

[root@localhost nginx-1.6.3]# yum install -y elinks

[root@localhost nginx-1.6.3]# elinks 10.0.105.189

4.升級(jí)nginx

將 nginx 版本進(jìn)行升級(jí) 并在不影響業(yè)務(wù)的情況下添加 SSL 和 pcre 模塊

[root@localhost ~]# tar xzf nginx-1.12.2.tar.gz -C /usr/local/ 
[root@localhost ~]# cd /usr/local/nginx-1.12.2/
[root@localhost nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx -group=ngiinx --with-http_stub_status_module --with-http_ssl_module --with-pcre 
[root@localhost nginx-1.12.2]# make 
[root@localhost nginx-1.12.2]# cd
[root@localhost ~]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_lod
[root@localhost ~]# cp /usr/local/nginx-1.12.2/objs/nginx /usr/local/nginx/sbin/ 
[root@localhost ~]# 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@localhost ~]# kill -USR2 6324  #此時(shí)查看進(jìn)程 給master進(jìn)程發(fā)送一個(gè)熱升級(jí)進(jìn)程信號(hào)
[root@localhost ~]#  ls /usr/local/nginx/logs/  #主進(jìn)程將重命名它的進(jìn)程文件 .pid 文件為 .oldbin
access.log  error.log  nginx.pid  nginx.oldbin
[root@localhost ~]# cat  /usr/local/nginx/logs/nginx.pid 
6340
[root@localhost ~]# 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í),兩個(gè) nginx 實(shí)例會(huì)同時(shí)運(yùn)行,一起處理輸入的請(qǐng)求。
要逐步停止舊的實(shí)例,你必須發(fā)送 WINCH 信號(hào)給舊的主進(jìn)程,然后,它的工作進(jìn)程就將開(kāi)始從容關(guān)閉

[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í)間后,舊的工作進(jìn)程處理了所有已連接的請(qǐng)求后退出,就僅由新的工作進(jìn)程來(lái)處理輸入的請(qǐng)求了
老的worker已經(jīng)沒(méi)了,但是老的master還有,是準(zhǔn)備回滾使用的

查看當(dāng)前nginx狀態(tài)

[root@localhost ~]# /usr/local/nginx/sbin/nginx -V 
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre

如果在版本升級(jí)完成后,沒(méi)有任何問(wèn)題,需要關(guān)閉老的master進(jìn)程.

5.回滾

老的master進(jìn)程是一直存在,在沒(méi)有手工關(guān)閉前,它是不會(huì)自已關(guān)閉的,這種設(shè)計(jì)是有好處的,好處就是為了升級(jí)新版本后,如果出現(xiàn)問(wèn)題能及時(shí)快速的回滾到上一個(gè)穩(wěn)定版本。

[root@localhost ~]# ps aux | grep nginx
root      12264  0.0  0.0  24860   956 ?        Ss   16:31   0:00 nginx: master process /usr/local/nginx/sbin/nginx
root      20083  0.0  0.1  45936  3260 ?        S    16:38   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     20084  0.0  0.1  46384  2128 ?        S    16:38   0:00 nginx: worker process
root      27488  0.0  0.0 112708   984 pts/0    R+   16:47   0:00 grep --color=auto nginx
[root@localhost ~]# kill -HUP 12264    #發(fā)送 HUP 信號(hào)給舊的主進(jìn)程 - 它將在不重載配置文件的情況下啟動(dòng)它的工作進(jìn)程
[root@localhost ~]# ps aux | grep nginx
root      12264  0.0  0.0  24860   956 ?        Ss   16:31   0:00 nginx: master process /usr/local/nginx/sbin/nginx
root      20083  0.0  0.1  45936  3260 ?        S    16:38   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     20084  0.0  0.1  46384  2128 ?        S    16:38   0:00 nginx: worker process
nginx     28108  0.0  0.0  25284  1460 ?        S    16:47   0:00 nginx: worker process
root      28154  0.0  0.0 112708   980 pts/0    R+   16:47   0:00 grep --color=auto nginx
[root@localhost ~]# kill -QUIT 20083  #發(fā)送 QUIT 信號(hào)給新的主進(jìn)程,要求其從容關(guān)閉其工作進(jìn)程
[root@localhost ~]# ps aux | grep nginx
root      12264  0.0  0.0  24860   956 ?        Ss   16:31   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     28108  0.0  0.0  25284  1460 ?        S    16:47   0:00 nginx: worker process
root      28933  0.0  0.0 112708   984 pts/0    R+   16:48   0:00 grep --color=auto nginx

如果新主進(jìn)程還沒(méi)退出,發(fā)送 TERM 信號(hào)給新的主進(jìn)程,迫使其退出

kill -TERM 20083

如果因?yàn)槟承┰蛐碌墓ぷ鬟M(jìn)程不能退出,向其發(fā)送 KILL 信號(hào)

kill -9 20083

新的主進(jìn)程退出后,舊的主進(jìn)程會(huì)由移除 .oldbin 后綴,恢復(fù)為它的 .pid 文件,這樣,一切就都恢復(fù)到升級(jí)之前了。

[root@localhost ~]#  ls /usr/local/nginx/logs/ 
access.log  error.log  nginx.pid
[root@localhost ~]# cat  /usr/local/nginx/logs/nginx.pid 
12264

如果嘗試升級(jí)成功,而你也希望保留新的服務(wù)器時(shí),發(fā)送 QUIT 信號(hào)給舊的主進(jìn)程使其退出而只留下新的服務(wù)器運(yùn)行:

kill -QUIT 12264

參考:
https://blog.csdn.net/mingongge/article/details/88097055
https://blog.csdn.net/u011085172/article/details/71515745

Nginx重新編譯添加新的模塊

http://www.itdecent.cn/p/0309cb5e7e76
http://www.itdecent.cn/p/695af00ee857

</article>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容