1. 程式人生 > 實用技巧 >nginx不停業務升級/增加編譯模組

nginx不停業務升級/增加編譯模組

1.前提【編譯安裝nginx】

######安裝nginx
yum install -y pcre-devel openssl-devel gcc >/dev/null
groupadd -g 30001 wx
useradd -u 30001  -g 30001  wx
tar xf /scripts/shell/nginx-1.12.2.tar.gz -C /scripts/shell/
cd /scripts/shell/nginx-1.12.2
./configure --prefix=/home/wx/nginx --user=wx --group=wx --with-http_stub_status_module  --with-http_ssl_module
make
make install

######檢視安裝目錄
[root@centos7 ~]# tree -L 2 /home/wx/ /home/wx/ └── nginx ├── conf ├── html ├── logs └── sbin 4 directories, 0 file ######啟動nginx [root@centos7 ~]# /home/wx/nginx/sbin/nginx ######檢視nginx版本以及編譯資訊 [root@centos7 ~]# /home/wx/nginx/sbin/nginx -V nginx version: nginx/1.12.2 built by gcc 4.8.5 20150623 (Red Hat 4.8
.5-39) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --prefix=/home/wx/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gunzip_module

2.新增模組with-http_gunzip_module

#注意:必須按照安裝nginx的方法進行安裝,只需要到make,千萬不要make install
tar xf 
/scripts/shell/nginx-1.12.2.tar.gz -C /scripts/shell/ cd /scripts/shell/nginx-1.12.2 ./configure --prefix=/home/wx/nginx --user=wx --group=wx --with-http_stub_status_module --with-http_ssl_module --with-http_gunzip_module
#備份舊版本的nginx可執行檔案(期間nginx不會停止服務) [root@centos7
~]# mv /home/wx/nginx/sbin/nginx /home/wx/nginx/sbin/nginx_bak
#複製新的nginx二進位制檔案,進入新的nginx原始碼包 cp
/home/wx/nginx-1.12.2/objs/nginx /home/wx/nginx/sbin/

#測試新版本的nginx是否正常 [root@centos7 nginx-1.12.2]# /home/wx/nginx/sbin/nginx -t nginx: the configuration file /home/wx/nginx/conf/nginx.conf syntax is ok nginx: configuration file /home/wx/nginx/conf/nginx.conf test is successful

3.關閉源nginx程序載入配置

#給nginx傳送平滑遷移訊號(若不清楚pid路徑,請檢視nginx配置檔案)
kill -USR2 `cat /var/run/nginx.pid`
 
#檢視nginx pid,會出現一個nginx.pid.oldbin
 [root@centos7 ~]# find / -name "nginx.pid.oldbin"
/home/wx/nginx/logs/nginx.pid.oldbin

#從容關閉舊的Nginx程序
kill -WINCH `cat /home/wx/nginx/logs/nginx.pid.oldbin`
 
#此時不過載配置啟動舊的工作程序
kill -HUP     `cat /home/wx/nginx/logs/nginx.pid.oldbin`
 
#結束工作程序,完成此次升級
kill -QUIT   `cat /home/wx/nginx/logs/nginx.pid.oldbin`
 
#驗證Nginx是否升級成功
[root@centos7 ~]# /home/wx/nginx/sbin/nginx -V
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/home/wx/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gunzip_module

4.註釋詳解

Nginx訊號處理與平滑升級
Nginx程序分為master程序和worker程序,我們可以通過訊號來控制master程序。預設情況下,Nginx會把它的master程序id寫到/home/wx/nginx/logs/nginx.pid中。你可以在編譯的時候通
升級過./configure來指定,或者在配置檔案中用pid來配置。 Master程序能夠接收並處理如下的訊號: ERM, INT(快速退出,當前的請求不執行完成就退出) QUIT (優雅退出,執行完當前的請求後退出) HUP (重新載入配置檔案,用新的配置檔案啟動新worker程序,並優雅的關閉舊的worker程序) USR1 (重新開啟日誌檔案) USR2 (平滑的升級nginx二進位制檔案) WINCH(優雅的關閉worker程序) Worker程序也可以接收並處理一些訊號: TERM, INT (快速退出) QUIT (優雅退出) USR1 (重新開啟日誌檔案) 用HUP訊號使Nginx載入新的配置檔案 當Nginx接收到HUP訊號的時候,它會嘗試著去解析並應用這個配置檔案,如果沒有問題,那麼它會建立新的worker程序,併發送訊號給舊的 worker程序,讓其優雅的退出。接收到訊號的舊的worker程序會關
閉監聽socket,但是還會處理當前的請求,處理完請求之後,舊的 worker程序退出。如果Nginx不能夠應用新的配置檔案,那麼仍將用舊的配置檔案來提供服務。 線上升級Nginx二進位制檔案 當你想升級Nginx到一個新的版本,增加或減少module的時候,你需要替換Nginx的二進位制檔案,你可以平滑的實現它,沒有請求會丟失。 #首先,用新的二進位制檔案替換掉舊的,然後傳送USR2訊號給master程序。master程序會把自己的.pid檔案重新命名為.oldbin(例 如,
/usr/local/nginx/logs/nginx.pid.oldbin),然後執行新的 二進位制檔案,從而啟動一個新的master程序和新的worker程序: PID PPID USER %CPU VSZ WCHAN COMMAND 33126 1 root 0.0 1164 pause nginx: master process /usr/local/nginx/sbin/nginx 33134 33126 wx 0.0 1368 kqread nginx: worker process (nginx) 33135 33126 wx 0.0 1380 kqread nginx: worker process (nginx) 33136 33126 wx 0.0 1368 kqread nginx: worker process (nginx) 36264 33126 root 0.0 1148 pause nginx: master process /usr/local/nginx/sbin/nginx 36265 36264 wx 0.0 1364 kqread nginx: worker process (nginx) 36266 36264 wx 0.0 1364 kqread nginx: worker process (nginx) 36267 36264 wx 0.0 1364 kqread nginx: worker process (nginx) #在這個時候,有兩個Nginx例項在執行,一起處理進來的請求。為了讓舊的例項退出,你需要傳送WINCH訊號給舊的master程序,這樣舊master程序的worker程序就會優雅的退出: PID PPID USER %CPU VSZ WCHAN COMMAND 33126 1 root 0.0 1164 pause nginx: master process /usr/local/nginx/sbin/nginx 33135 33126 wx 0.0 1380 kqread nginx: worker process is shutting down (nginx) 36264 33126 root 0.0 1148 pause nginx: master process /usr/local/nginx/sbin/nginx 36265 36264 wx 0.0 1364 kqread nginx: worker process (nginx) 36266 36264 wx 0.0 1364 kqread nginx: worker process (nginx) 36267 36264 wx 0.0 1364 kqread nginx: worker process (nginx) #一段時間後,舊的worker程序都已經退出了,只有新的worker程序處理進來的請求: PID PPID USER %CPU VSZ WCHAN COMMAND 33126 1 root 0.0 1164 pause nginx: master process /usr/local/nginx/sbin/nginx 36264 33126 root 0.0 1148 pause nginx: master process /usr/local/nginx/sbin/nginx 36265 36264 wx 0.0 1364 kqread nginx: worker process (nginx) 36266 36264 wx 0.0 1364 kqread nginx: worker process (nginx) 36267 36264 wx 0.0 1364 kqread nginx: worker process (nginx) #這個時候你仍然可以通過以下幾個步驟回滾到舊的服務,因為舊master程序並沒有關閉其監聽的socket: 傳送HUP訊號給舊的master程序,它會啟動worker程序並且不需要重新載入配置檔案 傳送QUIT訊號 給新的master程序,讓它優雅的終止其worker程序傳送TERM訊號給新的master程序,強制其退出 如果一些原因,新的worker程序沒有退出,傳送KILL訊號給它們 當新的master程序退出之後,舊的master 程序會刪除其pid檔名中的字尾.oldbin,這樣一切就又變成升級之前的樣子。 如果一個升級已經成功,然後你想只保留新的server,那麼傳送QUIT訊號給舊的master程序讓新的server來提供服務: PID PPID USER %CPU VSZ WCHAN COMMAND 36264 1 root 0.0 1148 pause nginx: master process /usr/local/nginx/sbin/nginx 36265 36264 wx 0.0 1364 kqread nginx: worker process (nginx) 36266 36264 wx 0.0 1364 kqread nginx: worker process (nginx) 36267 36264 wx 0.0 1364 kqread nginx: worker process (nginx)

######