天天看點

Nginx:Nginx熱部署

Nginx部署

  • ​​分布式篇 - Nginx安裝與運作​​

跟着上面這篇部落格進行操作即可。關閉防火牆,讓本地可以通過浏覽器通路​

​Nginx​

​服務。

[root@localhost ~]# systemctl stop firewalld      
Nginx:Nginx熱部署

信号量

檢視信号量:

[root@localhost ~]# kill -l
 1) SIGHUP   2) SIGINT   3) SIGQUIT  4) SIGILL   5) SIGTRAP
 6) SIGABRT  7) SIGBUS   8) SIGFPE   9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG  24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH  29) SIGIO 30) SIGPWR
31) SIGSYS  34) SIGRTMIN  35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3
38) SIGRTMIN+4  39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7
58) SIGRTMAX-6  59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1  64) SIGRTMAX      

有​

​64​

​種信号量,以下是幾種常用的信号量:

  • ​SIGINT​

    ​​ 、​

    ​SIGTERM​

    ​:快速關閉。
  • ​SIGQUIT​

    ​:從容關閉(優雅的關閉程序,即等請求結束後再關閉)。
  • ​SIGHUP​

    ​:平滑重新開機,重新加載配置檔案 (平滑重新開機,修改配置檔案之後不用重新開機伺服器)。
  • ​SIGUSR1​

    ​ :重新讀取日志檔案,在切割日志檔案時用途較大。
  • ​SIGUSR2​

    ​​:平滑更新可執行程式 ,​

    ​nginx​

    ​更新時候用。
  • ​SIGWINCH​

    ​ :從容關閉工作程序。

Nginx熱部署

​Nginx​

​​是一個多程序的高性能反向代理伺服器,包含一個​

​master​

​​程序和多個​

​worker​

​​程序(​

​worker​

​​程序的數量可以通過​

​nginx.conf​

​​配置檔案中的​

​worker_processes​

​​參數進行設定,預設​

​1​

​個),這樣可以充分利用多核處理器。

Nginx:Nginx熱部署

預設​

​1​

​​個​

​worker​

​程序。

Nginx:Nginx熱部署

并且​

​master​

​​程序和​

​worker​

​程序是父子程序關系。

Nginx:Nginx熱部署

​Nginx​

​​工作模式為多程序,​

​Nginx​

​​在啟動之後會有一個​

​master​

​​程序和多個​

​worker​

​​程序(預設​

​1​

​​個),多個​

​worker​

​​子程序将監聽​

​master​

​​父程序監聽的端口(參考父子程序的關系),并行處理請求。​

​master​

​​父程序主要用來管理​

​worker​

​​子程序(管理真正提供服務的​

​worker​

​​程序,向​

​worker​

​​程序發送信号,監控​

​worker​

​​程序的運作狀态,當​

​worker​

​​程序異常退出後,會重新啟動新的​

​worker​

​​程序),讀取并驗證配置資訊,​

​master​

​​程序不會對使用者請求提供服務,而使用者請求是由​

​worker​

​​程序進行處理。​

​Nginx​

​​是通過信号量來控制,比如停止和重新開機​

​Nginx​

​​。信号量是程序間通信的一種機制,​

​master​

​​主程序控制多個​

​worker​

​子程序,也是通過信号量。

Nginx:Nginx熱部署

現在來示範​

​Nginx​

​​是怎麼實作熱部署的,部落客通過修改​

​Nginx​

​​的配置檔案來模拟​

​Nginx​

​​的更新(先​

​copy​

​一份副本)。

[root@localhost ~]# cd /usr/local/nginx/conf/
[root@localhost conf]# ll
總用量 68
-rw-r--r--. 1 root root 1077 12月 20 20:24 fastcgi.conf
-rw-r--r--. 1 root root 1077 12月 20 20:24 fastcgi.conf.default
-rw-r--r--. 1 root root 1007 12月 20 20:24 fastcgi_params
-rw-r--r--. 1 root root 1007 12月 20 20:24 fastcgi_params.default
-rw-r--r--. 1 root root 2837 12月 20 20:24 koi-utf
-rw-r--r--. 1 root root 2223 12月 20 20:24 koi-win
-rw-r--r--. 1 root root 5231 12月 20 20:24 mime.types
-rw-r--r--. 1 root root 5231 12月 20 20:24 mime.types.default
-rw-r--r--. 1 root root 2656 12月 20 21:26 nginx.conf
-rw-r--r--. 1 root root 2656 12月 20 20:24 nginx.conf.default
-rw-r--r--. 1 root root  636 12月 20 20:24 scgi_params
-rw-r--r--. 1 root root  636 12月 20 20:24 scgi_params.default
-rw-r--r--. 1 root root  664 12月 20 20:24 uwsgi_params
-rw-r--r--. 1 root root  664 12月 20 20:24 uwsgi_params.default
-rw-r--r--. 1 root root 3610 12月 20 20:24 win-utf
[root@localhost conf]# cp nginx.conf nginx_old.conf
[root@localhost conf]# vim nginx.conf      
Nginx:Nginx熱部署

由于還沒有給​

​Nginx​

​​進行熱部署,現在通路​

​http://192.168.1.199/​

​​還是原來的​

​Nginx​

​頁面。

Nginx:Nginx熱部署

檢視​

​Nginx​

​的程序:

[root@localhost conf]# ps -ef | grep nginx
root     14964     1  0 22:25 ?        00:00:00 nginx: master process ./nginx
nobody   14965 14964  0 22:25 ?        00:00:00 nginx: worker process
root     15016  1521  0 23:07 pts/0    00:00:00 grep --color=auto nginx      

給​

​master​

​​程序發送​

​SIGUSR2​

​​信号,讓​

​Nginx​

​​平滑更新可執行程式。可以看到​

​Nginx​

​​重新啟動了一組​

​master​

​​程序和​

​worker​

​​程序,而新​

​master​

​​程序是舊​

​master​

​​程序的子程序(通過父子程序的繼承關系,新​

​master​

​​程序可以很友善地繼承舊​

​master​

​程序的相關資源)。

[root@localhost conf]# kill -s SIGUSR2 14964
[root@localhost conf]# ps -ef | grep nginx
root     14964     1  0 22:25 ?        00:00:00 nginx: master process ./nginx
nobody   14965 14964  0 22:25 ?        00:00:00 nginx: worker process
root     15019 14964  0 23:18 ?        00:00:00 nginx: master process ./nginx
nobody   15020 15019  0 23:18 ?        00:00:00 nginx: worker process
root     15022  1521  0 23:19 pts/0    00:00:00 grep --color=auto nginx      

并且​

​Nginx​

​​在日志目錄中存儲了新舊​

​pid​

​​檔案(儲存了新舊​

​master​

​​程序的​

​ID​

​)。

[root@localhost conf]# ll ../logs
總用量 16
-rw-r--r--. 1 root root 2729 12月 20 23:20 access.log
-rw-r--r--. 1 root root  708 12月 20 23:18 error.log
-rw-r--r--. 1 root root    6 12月 20 23:18 nginx.pid
-rw-r--r--. 1 root root    6 12月 20 22:25 nginx.pid.oldbin
[root@localhost conf]# cat ../logs/nginx.pid
15019
[root@localhost conf]# cat ../logs/nginx.pid.oldbin 
14964      

給舊​

​master​

​​程序發送​

​SIGWINCH​

​​信号,讓舊​

​master​

​​程序關閉舊​

​worker​

​程序。

[root@localhost conf]# kill -s SIGWINCH 14964
[root@localhost conf]# ps -ef | grep nginx
root     14964     1  0 22:25 ?        00:00:00 nginx: master process ./nginx
root     15019 14964  0 23:18 ?        00:00:00 nginx: master process ./nginx
nobody   15020 15019  0 23:18 ?        00:00:00 nginx: worker process
root     15030  1521  0 23:27 pts/0    00:00:00 grep --color=auto nginx      

現在通路​

​http://192.168.1.199/​

​​,會響應​

​404​

​。

Nginx:Nginx熱部署

而通路​

​http://192.168.1.199/nacos​

​​,會通路到​

​Nacos​

​服務。

Nginx:Nginx熱部署

如果更新版本沒有問題,就可以給舊​

​master​

​​程序發送​

​SIGQUIT​

​​信号,讓舊​

​master​

​​程序關閉,這樣就隻剩下新​

​master​

​​程序和新​

​worker​

​​程序,實作了​

​Nginx​

​的熱部署。

[root@localhost conf]# kill -s SIGQUIT 14964
[root@localhost conf]# ps -ef | grep nginx
root     15019     1  0 23:18 ?        00:00:00 nginx: master process ./nginx
nobody   15020 15019  0 23:18 ?        00:00:00 nginx: worker process
root     15034  1521  0 23:31 pts/0    00:00:00 grep --color=auto nginx      

如果更新版本有問題,需要復原到之前的版本,就可以給舊​

​master​

​​程序發送​

​SIGHUP​

​​信号,因為部落客重新進行了測試,是以程序号都變了,但很顯然舊​

​master​

​​程序重新建立了舊​

​worker​

​​程序,并且進行版本更新的​

​master​

​​和​

​worker​

​程序沒有被關閉。

[root@localhost conf]# kill -s SIGHUP 15084
[root@localhost conf]# ps -ef | grep nginx
root     15084     1  0 12月20 ?      00:00:00 nginx: master process ./nginx
root     15106 15084  0 12月20 ?      00:00:00 nginx: master process ./nginx
nobody   15107 15106  0 12月20 ?      00:00:00 nginx: worker process
nobody   15131 15084  0 00:02 ?        00:00:00 nginx: worker process
root     15141  1521  0 00:09 pts/0    00:00:00 grep --color=auto nginx      

給新​

​master​

​​程序發送​

​SIGQUIT​

​​信号,讓新​

​master​

​​程序關閉,這樣就隻剩下舊​

​master​

​​程序和新建立的舊​

​worker​

​程序,實作了復原。

[root@localhost conf]# kill -s SIGQUIT 15106
[root@localhost conf]# ps -ef | grep nginx
root     15084     1  0 12月20 ?      00:00:00 nginx: master process ./nginx
nobody   15131 15084  0 00:02 ?        00:00:00 nginx: worker process
root     15159  1521  0 00:25 pts/0    00:00:00 grep --color=auto nginx      

復原成功。

Nginx:Nginx熱部署

還需要對版本復原(即部落客這裡的配置檔案復原,不然下次重新開機就會出問題)。

[root@localhost conf]# cp -f nginx_old.conf nginx.conf
cp:是否覆寫"nginx.conf"? y      

為什麼給舊​

​master​

​​程序發送​

​SIGHUP​

​​信号,舊​

​master​

​​程序重新建立的​

​worker​

​程序沒有重新讀取配置檔案?下面是官方的說明:

Send the HUP signal to the old master process. The old master process will start new worker processes without re-reading the configuration. After that, all new processes can be shut down gracefully, by sending the QUIT signal to the new master process.

向舊​

​master​

​​程序發送​

​SIGHUP​

​​信号。舊​

​master​

​​程序将啟動新​

​worker​

​程序,而無需重新讀取配置。之後,通過向新​

​master​

​​程序發送​

​SIGQUIT​

​信号,所有新程序都可以正常關閉。

如果不存在新程序的情況下(隻有一組​

​master​

​​、​

​worker​

​​程序),修改配置檔案,再向​

​master​

​​程序發送​

​SIGHUP​

​信号,看是否會重新加載配置檔案。

Nginx:Nginx熱部署
[root@localhost conf]# kill -s SIGHUP 15084      

很顯然配置檔案被重新加載了,由于部落客還沒有看源碼,隻能猜測​

​Nginx​

​​的實作(如果說錯了,請大家評論補充),​

​Nginx​

​​應該是根據目前是否在進行熱部署(存在新​

​master​

​​程序),來決定​

​SIGHUP​

​信号是否需要重新加載配置檔案。

Nginx:Nginx熱部署