Files
Knowledge/0 编程/nginx/Nginx系列(一).md
T

98 lines
3.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
**accept 阶段(ngx_http_init_connection):**
1. 分配ngx_http_connection_t结构体;
2. 根据 fd 的端口和地址,为hc->addr_conf赋值,以供后续server块查询;
3. 新的 fd 加入epoll,设置读超时,超时时间为 post_accept_timeout,设置连接可重用。
**首次读阶段(ngx_http_wait_request_handler):**
1. 分配c->buffer(cscf->client_header_buffer_size大小)用于存储客户端包头;
2. recv调用接收包头;
3. 创建ngx_http_request_t。
**请求行阶段(ngx_http_process_request_line):**
1. 循环recv,放入r->header_in(c->buffer);
2. 解析uri,包括r->uri、r->args、r->exten;
3. 请求行处理结束。
**请求头阶段(ngx_http_process_request_headers):**
1. 循环recv,放入r->header_in(c->buffer);
2. 解析请求头,保存在r->headers_in.headers;
3. 解析请求头结束后,调用ngx_http_process_request,进行11阶段和子请求处理。
**读触发**
1. 在r->content_handler存在情况下,调用content_handler。
**content_handler阶段:(ngx_http_proxy_handler)**
1. 分配ngx_http_upstream_t结构体,并根据上游的协议类型,为upstream分配不同的回调函数,例如:
create_request:创建上游请求
process_header:处理上游响应
finalize_request:结束上游请求
input_filter_init:input_filter初始化
input_filter:上游响应的处理函数
2. 无请求体情况,直接进入上游初始化(ngx_http_upstream_init);
3. 调用u->create_request将全部请求放入u->request_bufs(ngx_chain_t);
4. 建立上游连接(ngx_http_upstream_connect)。
**request_body_no_buffering=0的情况(缓存请求体):**
1.不调用ngx_http_upstream_init建立上游;
2.读事件触发请求体接收,直至请求体读取完成;
3.回调rb->post_handler,即建立上游。
**request_body_no_buffering=1的情况(不缓存请求体):**
1.第一次读取结束后,设置r->reading_body = 1;
2.立即建立上游;
3.上游写事件触发时,将判断r->reading_body位,并不断读取请求体并发送至上游,直至请求体读取完成。
**上游请求发送阶段:(ngx_http_upstream_send_request)**
1. 对于首次请求发送(!u->request_sent),调用ngx_output_chain,发送u->request_bufs和r->request_body->bufs中的请求,直到请求发送完毕;
2. 非首次发送,只需要发送r->request_body->bufs。
3. 如果request_body_no_buffering = 0,发送一次就结束。否则,将循环读取下游包体,并循环发送。
**响应头阶段:(ngx_http_upstream_process_header)**
1.循环recv,放入u->buffer;
2.u->process_header解析响应行和响应头,解析结果保存在u->headers_in;
3.将响应头放入r->headers_out;
4.ngx_http_send_header发送响应头。
**响应体阶段:**
即ngx_http_upstream_process_non_buffered_request调用
1.循环recv,放入u->buffer;
2.调用ngx_http_output_filter过滤及发送。