微信公众号:郑尔多斯
关注可了解更多的Nginx
知识。任何问题或建议,请公众号留言;
关注公众号,有趣有内涵的文章第一时间送达!
请关注我的博客, 戳这里nginx
解析完请求头部以后调用ngx_http_process_request()函数web
// 进入http的11个阶段处理
static void ngx_http_process_request(ngx_http_request_t *r)
{
ngx_connection_t *c;
c = r->connection;
if (r->plain_http) {
ngx_log_error(NGX_LOG_INFO, c->log, 0,
"client sent plain HTTP request to HTTPS port");
ngx_http_finalize_request(r, NGX_HTTP_TO_HTTPS);
return;
}
if (c->read->timer_set) {
ngx_del_timer(c->read);
}
c->read->handler = ngx_http_request_handler;
c->write->handler = ngx_http_request_handler;
r->read_event_handler = ngx_http_block_reading;
ngx_http_handler(r);
ngx_http_run_posted_requests(c);
}
复制代码
这个函数是在解析完http
请求头以后调用的,咱们分析一下他的功能:
一、删除定时器,由于http
的头部已经解析完了,要进入http
处理阶段了,因此能够删除原先的定时器了。
二、将connection
的读写事件回调函数都设置为ngx_http_request_handler
。代码以下:
当epoll_wait()
监控到当前connection
上发生了读写事件,就会调用ngx_http_request_handler
函数,而这个函数根据读写事件的类型分别调用http
链接的read_event_handler
和write_event_handler()
函数,其实不明白这里为何要这么作。 而后调用ngx_http_run_posted_requests()
函数处理子请求。也就是说,每当链接上面有读写事件发生的时候都会调用ngx_http_run_posted_requests
来处理子请求。微信
static void ngx_http_request_handler(ngx_event_t *ev)
{
ngx_connection_t *c;
ngx_http_request_t *r;
ngx_http_log_ctx_t *ctx;
c = ev->data;
r = c->data;
ctx = c->log->data;
ctx->current_request = r;
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
"http run request: \"%V?%V\"", &r->uri, &r->args);
if (ev->write) {
r->write_event_handler(r);
} else {
r->read_event_handler(r);
}
ngx_http_run_posted_requests(c);
}
3、调用ngx_http_handler()函数
void
ngx_http_handler(ngx_http_request_t *r)
{
ngx_http_core_main_conf_t *cmcf;
r->connection->log->action = NULL;
r->connection->unexpected_eof = 0;
if (!r->internal) {
switch (r->headers_in.connection_type) {
case 0:
r->keepalive = (r->http_version > NGX_HTTP_VERSION_10);
break;
case NGX_HTTP_CONNECTION_CLOSE:
r->keepalive = 0;
break;
case NGX_HTTP_CONNECTION_KEEP_ALIVE:
r->keepalive = 1;
break;
}
r->lingering_close = (r->headers_in.content_length_n > 0);
r->phase_handler = 0;
} else {
cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
r->phase_handler = cmcf->phase_engine.server_rewrite_index;
}
r->valid_location = 1;
r->write_event_handler = ngx_http_core_run_phases;
ngx_http_core_run_phases(r);
}
复制代码
这个函数的主要做用就是将http
链接的write_event_handler
设置为ngx_http_core_run_phases
函数,而后调用ngx_http_core_run_phases
函数,这个函数就是进入nginx
的11个处理阶段。app
四、调用ngx_http_run_posted_requests
函数处理子请求。函数
epoll_wait()
监控到
connection
上面有读写事件的时候会调用
ngx_http_core_run_phase()
函数进行11个阶段的处理。
ngx_http_core_run_phases()
的过程当中,若是某一个处理阶段的
checker
函数返回了
NGX_OK
,那么从代码中能够看到,就会推出
ngx_http_core_run_phases()
函数,这个时候
nginx
的控制权就返回给了
epoll
模块,只能等待发生下一个事件的时候继续调用
ngx_http_core_run_phase()
进行处理。
// nginx的核心处理函数
void ngx_http_core_run_phases(ngx_http_request_t *r)
{
ngx_int_t rc;
ngx_http_phase_handler_t *ph;
ngx_http_core_main_conf_t *cmcf;
cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
ph = cmcf->phase_engine.handlers;
while (ph[r->phase_handler].checker) {
rc = ph[r->phase_handler].checker(r, &ph[r->phase_handler]);
if (rc == NGX_OK) {
return;
}
}
}
复制代码
喜欢本文的朋友们,欢迎长按下图关注订阅号郑尔多斯,更多精彩内容第一时间送达
post