nginx 处理http模块的流程
1 | NGX_HTTP_POST_READ_PHASE ngx_http_post_read_phase |
接收到完整的http头部后处理的阶段,它位于uri重写之前,实际上很少有模块会注册在该阶段,默认的情况下,该阶段被跳过 |
---|---|---|
2 | NGX_HTTP_SERVER_REWRITE_PHASE ngx_http_server_rewrite_phase |
uri与location匹配前,修改uri的阶段,用于重定向,也就是该阶段执行处于server块内,location块外的重写指令,在读取请求头的过程中nginx会根据host及端口找到对应的虚拟主机配置 |
3 | NGX_HTTP_FIND_CONFIG_PHASE ngx_http_find_config_phase |
根据uri寻找匹配的location块配置项阶段,该阶段使用重写之后的uri来查找对应的location,值得注意的是该阶段可能会被执行多次,因为也可能有location级别的重写指令 |
4 | NGX_HTTP_REWRITE_PHASE ngx_http_rewrite_phase |
上一阶段找到location块后再修改uri,location级别的uri重写阶段,该阶段执行location基本的重写指令,也可能会被执行多次 |
5 | NGX_HTTP_POST_REWRITE_PHASE ngx_http_post_rewrite_phase |
防止重写url后导致的死循环,location级别重写的后一阶段,用来检查上阶段是否有uri重写,并根据结果跳转到合适的阶段。(限制跳转次数) |
6 | NGX_HTTP_PREACCESS_PHASE ngx_http_preaccess_phase |
下一阶段之前的准备,访问权限控制的前一阶段,该阶段在权限控制阶段之前,一般也用于访问控制,比如限制访问频率,链接数等 |
7 | NGX_HTTP_ACCESS_PHASE ngx_http_access_phase |
让http模块判断是否允许这个请求进入nginx服务器,访问权限控制阶段,比如基于ip黑白名单的权限控制,基于用户名密码的权限控制等 |
8 | NGX_HTTP_POST_ACCESS_PHASE ngx_http_post_access_phase |
访问权限控制的后一阶段,该阶段根据权限控制阶段的执行结果进行相应处理,向用户发送拒绝服务的错误码,用来响应上一阶段的拒绝 |
9 | NGX_HTTP_TRY_FILES_PHASE ngx_http_try_files_phase |
为访问静态文件资源而设置,try_files指令的处理阶段,如果没有配置try_files指令,则该阶段被跳过 |
10 | NGX_HTTP_CONTENT_PHASE ngx_http_content_phase |
处理http请求内容的阶段,大部分http模块介入这个阶段,内容生成阶段,该阶段产生响应,并发送到客户端 |
11 | NGX_HTTP_LOG_PHASE ngx_http_log_phase |
处理完请求后的日志记录阶段,该阶段记录访问日志 |
location /index {
try_file index.html index.php hello.html 404.html
}
nginx 处理tcp模块的流程
1 | Post-accept ngx_stream_realip_module |
accepting a client connection |
---|---|---|
2 | Pre-access ngx_stream_limit_conn_module ngx_stream_set_module |
check for access. |
3 | Access ngx_stream_access_module |
Client access limitation before actual data processing. |
4 | SSL ngx_stream_ssl_module |
TLS/SSL termination |
5 | Preread ngx_stream_ssl_preread_module |
Reading initial bytes of data into the preread buffer to allow modules such as ngx_stream_ssl_preread_module analyze the data before its processing. |
6 | Content | Mandatory phase where data is actually processed, usually proxied to upstream servers, or a specified value is returned to a client. |
7 | Log ngx_stream_log_module |
where the result of a client session processing is recorded. |
http反向代理流程
参考资料:
https://blog.csdn.net/chen213wb/article/details/84728615
https://blog.csdn.net/xuezhiwu001/article/details/100038073
http://nginx.org/en/docs/stream/stream_processing.html