首頁  >  文章  >  運維  >  Nginx熱部署如何實現

Nginx熱部署如何實現

王林
王林轉載
2023-05-24 16:55:391217瀏覽

關閉防火牆,讓本機可以透過瀏覽器存取Nginx服務。

[root@localhost ~]# systemctl stop firewalld

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種信號量,以下是幾種常用的信號量:

  • SIGINTSIGTERM:快速關閉。

  • SIGQUIT:從容關閉(優雅的關閉進程,即等待請求結束後再關閉)。

  • SIGHUP:平滑重啟,重新載入設定檔 (平滑重啟,修改設定檔之後不用重啟伺服器)。

  • SIGUSR1 :重新讀取日誌文件,切割日誌檔案時用途較大。

  • SIGUSR2:平滑升級執行程式 ,nginx升級時候用。

  • SIGWINCH :從容關閉工作進程。

Nginx熱部署

Nginx是一個多進程的高效能反向代理伺服器,包含一個master進程和多個worker進程(worker進程的數量可以透過nginx.conf設定檔中的worker_processes參數進行設置,預設1個),這樣可以充分利用多核心處理器。

Nginx熱部署如何實現

預設1worker流程。

Nginx熱部署如何實現

並且master行程和worker程式是父子程式關係。

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的升級(先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進行熱部署,現在訪問http://192.168.1.199/還是原來的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熱部署如何實現

而造訪http://192.168.1.199/nacos,會造訪到Nacos服務。

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进程,并且进行版本升级的masterworker进程没有被关闭。

[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熱部署如何實現

还需要对版本回滚(即博主这里的配置文件回滚,不然下次重启就会出问题)。

[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信号,所有新进程都可以正常关闭。

如果不存在新进程的情况下(只有一组masterworker进程),修改配置文件,再向master进程发送SIGHUP信号,看是否会重新加载配置文件。

Nginx熱部署如何實現

[root@localhost conf]# kill -s SIGHUP 15084

很显然配置文件被重新加载了,由于博主还没有看源码,只能猜测Nginx的实现(如果说错了,请大家评论补充),Nginx应该是根据当前是否在进行热部署(存在新master进程),来决定SIGHUP信号是否需要重新加载配置文件。

Nginx熱部署如何實現

以上是Nginx熱部署如何實現的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除