平穩切換nginx版本,切換nginx

來源:互聯網
上載者:User

平穩切換nginx版本,切換nginx

在說明如何穩定安全地升級、降級已經在運行中的nginx之前,需要先瞭解nginx支援的幾種訊號。以下幾種是主進程可以接收的訊號,注意worker進程也可以接收一些訊號,但和主進程的訊號處理機制有些不一樣,且主進程支援的訊號worker進程不一定支援。具體可見man nginx。

SIGINT, SIGTERM  立即殺掉nginx主(即所有進程)SIGQUIT          graceful stop主進程SIGWINCH         graceful stop所有的worker進程SIGHUP           reload設定檔,並使老的worker進程graceful stopSIGUSR1          重新開啟記錄檔(Reopen log files)SIGUSR2          線上切換nginx可執行程式(Upgrade the nginx executable on the fly)

graceful stop的行為是:(1)進程不再監聽、接受新的請求;(2)進程繼續處理正在處理的請求,但處理完成後銷毀。

1. 升級

如果想對一個已啟動並執行nginx執行個體進行版本升級,或者因為重新編譯了一個版本而替換舊版本,可以考慮按照以下一系列過程來平穩、安全地升級。當然,如果直接停止服務不會產生多大影響,直接停掉再啟動新版本nginx執行個體更方便簡單。

  • 1.將新版本的nginx命令路徑替換掉舊的nginx命令。

通常,對於編譯安裝的nginx來說,採用軟連結的方式比較便捷。例如舊版本的安裝路徑為/usr/local/nginx-1.12.0,為其建立一個軟連結/usr/local/nginx,如果有新版本/usr/local/nginx-1.12.1,只需修改軟連結/usr/local/nginx的指向目標為/usr/local/nginx-1.12.1即可。這樣/usr/local/nginx/sbin/nginx就會隨著軟連結的指向改變而指向新nginx程式。

  • 2.對舊nginx執行個體的主進程發送USR2訊號。
kill -USR2 `cat /var/run/nginx/nginx.pid`

該訊號提示nginx舊的主進程要升級,並執行新的nginx程式。例如步驟1中,舊的nginx主進程為/usr/local/nginx/sbin/nginx,但其指向的是/usr/local/nginx-1.12.0/sbin/nginx,發送該訊號後仍將執行/usr/local/nginx/sbin/nginx,但此時因為軟連結目標已改變,使得此時啟動的nginx已經是/usr/local/nginx-1.12.1/sbin/nginx程式。

[root@xuexi ~]# ps aux | egrep '(ngin[x]|PI[D])'USER        PID %CPU %MEM    VSZ   RSS TTY  STAT START   TIME COMMANDroot     103753  0.0  0.3 122720  7360 ?    S    17:01   0:00 nginx: master process /usr/sbin/nginxnginx    103754  0.0  0.1 125248  3520 ?    S    17:01   0:00 nginx: worker processnginx    103755  0.0  0.1 125248  3520 ?    S    17:01   0:00 nginx: worker processnginx    103756  0.0  0.1 125248  3520 ?    S    17:01   0:00 nginx: worker processnginx    103757  0.0  0.1 125248  3520 ?    S    17:01   0:00 nginx: worker processroot     103919  0.0  0.3 122720  7364 ?    S    17:44   0:00 nginx: master process /usr/sbin/nginxnginx    103920  0.0  0.1 125248  3524 ?    S    17:44   0:00 nginx: worker processnginx    103921  0.0  0.1 125248  3524 ?    S    17:44   0:00 nginx: worker processnginx    103922  0.0  0.1 125248  3524 ?    S    17:44   0:00 nginx: worker processnginx    103923  0.0  0.1 125248  3524 ?    S    17:44   0:00 nginx: worker process

此外,發送該訊號後將會切換pid檔案,舊的pid檔案被重新命名為nginx.pid.oldbin,記錄的是舊的nginx主進程pid值,新的pid檔案為nginx.pid,記錄的是新啟動的nginx的主進程pid值。

[root@xuexi ~]# ls /var/run/nginx*     /var/run/nginx.pid  /var/run/nginx.pid.oldbin
  • 3.graceful stop舊的主進程號。
kill -QUIT `cat /var/run/nginx/nginx.pid.oldbin`

向舊的主進程號發送QUIT訊號,該訊號將使得主進程以graceful的方式關閉。這將使得舊的主進程、舊的worker進程不再接受任何新請求,但卻會把正在處理過程中的請求處理完畢,然後被銷毀退出。

  • 4.更穩妥的方式是先讓worker進程graceful stop,在新版本的nginx執行個體運行一小段時間後如果正常工作,再graceful stop舊的主進程。
kill -WINCH `cat /var/run/nginx/nginx.pid.oldbin`# a period of time goes, graceful stop old master nginxkill -QUIT `cat /var/run/nginx/nginx.pid.oldbin`

在發送WINCH訊號給舊的主進程後,舊的worker進程將逐漸退出,但舊的主進程卻會保留不退出。

[root@xuexi ~]# ps aux | egrep '(ngin[x]|PI[D])'USER        PID %CPU %MEM    VSZ   RSS TTY   STAT START   TIME COMMANDroot     103753  0.0  0.3 122720  7432 ?     S    17:01   0:00 nginx: master process /usr/sbin/nginxroot     103919  0.0  0.3 122720  7364 ?     S    17:44   0:00 nginx: master process /usr/sbin/nginxnginx    103920  0.0  0.1 125248  3524 ?     S    17:44   0:00 nginx: worker processnginx    103921  0.0  0.1 125248  3524 ?     S    17:44   0:00 nginx: worker processnginx    103922  0.0  0.1 125248  3524 ?     S    17:44   0:00 nginx: worker processnginx    103923  0.0  0.1 125248  3524 ?     S    17:44   0:00 nginx: worker process

如果發現新版本的nginx執行個體不滿意,可以直接向舊主進程號發送HUP訊號,這樣舊的主進程就會重新讀取設定檔並fork新的worker進程,再將新的主進程號殺掉(可以graceful stop),就可以還原為舊版本的nginx執行個體。

2. 降級

上面第4步其實就是最安全的降級方式。即:

kill -HUP `cat /var/run/nginx/nginx.pid.oldbin`kill -QUIT `cat /var/run/nginx/nginx.pid`

但如果舊的主進程號已經被殺掉了,目前只有新版本的nginx執行個體在運行,那麼只需以升級的步驟進行降級即可。即:

kill -USR2 `cat /var/run/nginx/nginx.pid`kill -QUIT `cat /var/run/nginx/nginx.pid.oldbin`
3.一鍵升級指令碼

以下是升級的指令碼。

#!/bin/sh## Legacy action script for "service nginx upgrade"# Source function library.[ -f /etc/rc.d/init.d/functions ] && . /etc/rc.d/init.d/functionsif [ -f /etc/sysconfig/nginx ]; then    . /etc/sysconfig/nginxfiprefix=/usr/local/nginxprog=nginxnginx=$prefix/sbin/nginxconffile=$prefix/conf/nginx.confpidfile=/var/run/nginx.pidSLEEPSEC=${SLEEPSEC:-1}UPGRADEWAITLOOPS=${UPGRADEWAITLOOPS:-5}oldbinpidfile=${pidfile}.oldbin# 設定檔語法檢查${nginx} -t -c ${conffile} -q || return 6echo -n $"Starting new master $prog: "# 發送USR2訊號升級nginx可執行程式killproc -p ${pidfile} ${prog} -USR2echofor i in `/usr/bin/seq $UPGRADEWAITLOOPS`; do    /bin/sleep $SLEEPSEC    if [ -f ${oldbinpidfile} -a -f ${pidfile} ]; then        echo -n $"Graceful shutdown of old $prog: "        # graceful stop舊主nginx主進程        killproc -p ${oldbinpidfile} ${prog} -QUIT        echo        exit 0    fidoneecho $"Upgrade failed!"exit 1
回到Linux系列文章大綱:http://www.cnblogs.com/f-ck-need-u/p/7048359.html回到網站架構系列文章大綱:http://www.cnblogs.com/f-ck-need-u/p/7576137.html回到資料庫系列文章大綱:http://www.cnblogs.com/f-ck-need-u/p/7586194.html轉載請註明出處:http://www.cnblogs.com/f-ck-need-u/p/7658111.html註:若您覺得這篇文章還不錯請點擊右下角推薦,您的支援能激發作者更大的寫作熱情,非常感謝!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.