遇到conf文件的http模块。http不是在Nginx的mian函数中启动,而是解析conf文件时遇到http才会去解析并启动。
2、请求到来时,调用http server。

二、Nginx 启动http Server流程
2.1、ngx_module_t
Nginx的http是以NGX_CORE_MODULE模块的方式加载到Nginx中,因此它定义成一个ngx_module_t(/src/http/ngx_http.c):
展开
代码语言:C
自动换行
AI代码解释
static ngx_command_t ngx_http_commands[] = { { ngx_string("http"), NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS, ngx_http_block, 0, 0, NULL }, ngx_null_command }; static ngx_core_module_t ngx_http_module_ctx = { ngx_string("http"), NULL, NULL }; ngx_module_t ngx_http_module = { NGX_MODULE_V1, &ngx_http_module_ctx, /* module context */ ngx_http_commands, /* module directives */ NGX_CORE_MODULE, /* module type */ NULL, /* init master */ NULL, /* init module */ NULL, /* init process */ NULL, /* init thread */ NULL, /* exit thread */ NULL, /* exit process */ NULL, /* exit master */ NGX_MODULE_V1_PADDING };
/src/core/ngx_module.h
代码语言:C
自动换行
AI代码解释
#define NGX_MODULE_V1 NGX_MODULE_UNSET_INDEX, NGX_MODULE_UNSET_INDEX, NULL, 0, 0, nginx_version, NGX_MODULE_SIGNATURE #define NGX_MODULE_V1_PADDING 0, 0, 0, 0, 0, 0, 0, 0
2.2、ngx_http_commands
当解析conf文件时,如果遇到“http”就会获取ngx_http_commands,执行ngx_http_block函数。
(/src/http/ngx_http.c)
展开
代码语言:C
自动换行
AI代码解释
static ngx_command_t ngx_http_commands[] = { { ngx_string("http"), NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS, ngx_http_block, 0, 0, NULL }, ngx_null_command };
2.3、ngx_http_block
ngx_http_block相当于http的主函数,里面做了很多的初始化工作。其中在最后会调用一个ngx_http_optimize_servers的函数优化端口、地址和服务器名称列表。

2803

被折叠的 条评论
为什么被折叠?



