Nginx核心知識100講學習筆記(陶輝)Nginx架構基礎(一)

(轉載,非常不錯的文章)

一、Nginx的請求處理流程進程結構

1、Nginx的請求處理流程

 

2、Nginx的進程結構

 

 3、進程作用

1、Master進程

1、是進行work進程的監控管理的
2、看看work進程是否正常工作需不需要進行熱部署、需不需要重新載入配置文件

2、Cache manager 緩存的管理

1、緩存爲反向代理後端發來的動態請求做緩存使用

2、緩存在不光是在work進程間使用、還要被Cache manager和Cache loader使用

3、Cache loader 載入緩存

二、Nginx的進程結構實例演示

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

[root@luoahong vim]# ps -ef|grep nginx

root 24145 1 0 11:08 ? 00:00:00 nginx: master process ../sbin/nginx

nobody 24191 24145 0 11:36 ? 00:00:00 nginx: worker process

nobody 24192 24145 0 11:36 ? 00:00:00 nginx: worker process

root 24196 24153 0 11:38 pts/1 00:00:00 grep --color=auto nginx

[root@luoahong conf]# ../sbin/nginx -s reload

[root@luoahong vim]# ps -ef|grep nginx

root 24145 1 0 11:08 ? 00:00:00 nginx: master process ../sbin/nginx

nobody 24198 24145 0 11:38 ? 00:00:00 nginx: worker process

nobody 24199 24145 0 11:38 ? 00:00:00 nginx: worker process

root 24201 24153 0 11:38 pts/1 00:00:00 grep --color=auto nginx

[root@luoahong vim]#kill -SIGHUP 24145

[root@luoahong vim]# ps -ef|grep nginx

root 24145 1 0 11:08 ? 00:00:00 nginx: master process ../sbin/nginx

nobody 24203 24145 0 11:40 ? 00:00:00 nginx: worker process

nobody 24204 24145 0 11:40 ? 00:00:00 nginx: worker process

root 24206 24153 0 11:40 pts/1 00:00:00 grep --color=auto nginx

kill -SIGHUP 24145和../sbin/nginx -s reload的作用一樣

三、使用信號管理Nginx的父子進程

四、reload和熱升級

1、reload重載配置文件的真相

1、向master進程發送HUP信號(reload命令)

2、master進程校驗配置語法是否正確

3、master進程進程打開新的監聽端口

爲什麼要打開新的監聽端口?因爲有可能會打開原來沒有打開過的端口

4、master進程用新配置啓動新的worker子進程

設置定時器 worker_shutdown_timeout

5、master進程向老worker子進程發送OUIT信號

6、老worker進程關閉監聽句柄,處理完當前連接後結束進程

2、不停機載入新的配置文件

3、熱升級的完整流程(文字)

1、將舊Nginx文件換成新Nginx文件(注意備份)

nginx的Master進程的PID會記錄在logs目錄的nginx.pid文件中、新nginx和老的nginx 所有的目錄必須一致

2、向master進程發送USR2信號

沒有辦法通過nginx命令行發送信號處理,是因爲到目前爲止還沒有這樣的信號

3、master進程修改pid文件名,加後綴.oldbin

nginx的Master進程的PID會記錄在logs目錄的nginx.pid文件中

4、master進程用新的Nginx文件啓動新的master進程
5、向老master進程發送QUIT信號,關閉老master
6、回滾:向老master發送HUP,向新master發送OUIT

4、不停機更新nginx二進制文件

五、優雅地關閉worker進程

worker進程優雅的關閉,主要針對的是HTTP請求。代理websocker協議,TCP,UDP層的時候不能優雅推出

1、設置定時器 worker_shutdown_timeout
2、關閉監聽句柄 :保證要關閉的進程不再處理新的連接
3、關閉空閒連接 :爲了利用最大化,會保存一些空閒的連接,這時會被關閉。
4、在循環中等待全部連接關閉:每發現一個請求處理完畢就會把這個連接關閉。超過設置的worker_shutdown_timeout的時間,會立即退出。
5、退出進程

作者:羅阿紅 出處:http://www.cnblogs.com/luoahong/ 本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章