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的进程结构实例演示

[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、退出进程

posted @ 2020-03-05 13:28  活的潇洒80  阅读(1863)  评论(0编辑  收藏  举报