http模块

一、源码分析 #

1. 几个结构的生命周期和关系 #

1.1. ngx_http_request_t 代表一次http请求 #

  • 和其他结构的代码关系
 1// ngx_http.h
 2typedef struct ngx_http_request_s     ngx_http_request_t;
 3// ngx_http_request.h
 4struct ngx_http_request_s {
 5    ...
 6    ngx_connection_t                 *connection;
 7    ...
 8    ngx_http_upstream_t              *upstream;
 9    ...
10    ngx_pool_t                       *pool;
11    ...
12    ngx_http_connection_t            *http_connection;
13    ...
14};
  • ngx_http_request_t代表一次http请求
  • 一次ngx_connection_t可以给多个ngx_http_request_t使用
  • 每个ngx_http_request_t必须依赖于一个ngx_connection_t,并且持有r->connection
  • 每个ngx_http_request_t拥有自己的内存池r->pool,当一次请求结束后会进行释放

(1) 创造 #

  • ngx_http_wait_request_handler中进行构造,存放到ngx_connection_tdata
  • 每个http请求接收的回调中创建
 1// ngx_http_request.c
 2static void
 3ngx_http_wait_request_handler(ngx_event_t *rev)
 4{
 5    ...
 6    c->log->action = "reading client request line";
 7
 8    ngx_reusable_connection(c, 0);
 9
10    c->data = ngx_http_create_request(c);
11    ...
12}

2. nginx网络模型 #

2.1. 网络模型总述 #

  • 使用master进程监听端口创建套接字,然后fork子进程,每个子进程就可以复用监听套接字