概述
在前面的文章《 Nginx 配置解析》简单讲解了通用模块的配置项解析,并且大概讲解了HTTP 模块的配置项解析过程,本文更具体的分析HTTP 模块的初始化过程。HTTP 模块初始化过程主要有:上下文结构初始化、配置项解析、配置项合并、server 相关端口设置。
HTTP 模块接口
ngx_http_module_t 结构体
在 Nginx 中,结构体 ngx_module_t 是 Nginx 模块最基本的接口。对于每一种不同类型的模块,都有一个具体的结构体来描述这一类模块的通用接口。在Nginx 中定义了HTTP 模块的通用接口ngx_http_module_t 结构体,该结构体定义在文件src/http/ngx_http_config.h:我们把直属于http{}、server{}、location{} 块的配置项分别称为main、srv、loc 级别配置项。
/* 所有HTTP模块的通用接口结构ngx_http_module_t */
typedef struct {
/* 在解析http{}块内的配置项前回调 */
ngx_int_t (*preconfiguration)(ngx_conf_t *cf);
/* 在解析http{}块内的配置项后回调 */
ngx_int_t (*postconfiguration)(ngx_conf_t *cf);
/*
* 创建用于存储HTTP全局配置项的结构体;
* 该结构体中的成员将保存直属于http{}块的配置项参数;
* 该方法在解析main配置项前调用;
*/
void *(*create_main_conf)(ngx_conf_t *cf);
/* 解析完main配置项后回调 */
char *(*init_main_conf)(ngx_conf_t *cf, void *conf);
/*
* 创建用于存储可同时出现在main、srv级别配置项的结构体;
* 该结构体中的成员与server配置是相关联的;
*/
void *(*create_srv_conf)(ngx_conf_t *cf);
/*
* 由create_srv_conf产生的结构体所要解析的配置项,
* 可能同时出现在main、srv级别中,
* merge_srv_conf 方法可以将出现在main级别中的配置项值合并到srv级别的配置项中;
*/
char *(*merge_srv_conf)(ngx_conf_t *cf, void *prev, void *conf);
/*
* 创建用于存储同时出现在main、srv、loc级别配置项的结构体,
* 该结构体成员与location配置相关联;
*/
void *(*create_loc_conf)(ngx_conf_t *cf);
/*
* 由create_loc_conf 产生的结构体所要解析的配置项,
* 可能同时出现在main、srv、loc级别的配置项中,
* merge_loc_conf 方法将出现在main、srv级别的配置项值合并到loc级别的配置项中;
*/
char *(*merge_loc_conf)(ngx_conf_t *cf, void *prev, void *conf);
} ngx_http_module_t;
ngx_http_conf_ctx_t 结构体
在 HTTP 模块中,管理 HTTP 模块配置项的结构由 ngx_http_conf_ctx_t 实现,该结构有三个成员,分别指向三个指针数组,指针数组是由相应地 HTTP 模块create_main_conf、create_srv_conf、create_loc_conf 方法创建的结构体指针组成的数组。ngx_http_conf_ctx_t 结构体定义在文件src/http/ngx_http_config.h 中:
/* HTTP框架的上下文结构体ngx_http_conf_ctx_t */
typedef struct {
/*
* 指向一个指针数组;
* 数组中的每个成员都是由所有HTTP模块create_main_conf方法创建的存放全局配置项的结构体,
* 它们存放着解析直属于http{}块内main级别的配置项参数;
*/
void **main_conf;
/*
* 指向一个指针数组;
* 数组中的每个成员都是由所有HTTP模块create_srv_conf方法创建的与server相关的配置项结构体,
* 它们存放着main级别,或srv级别的配置项参数;
* 这与当前的ngx_http_conf_ctx_t是在解析http{}或server{}块时创建有关;
*/
void **srv_conf;
/*
* 指向一个指针数组;
* 数组中的每个成员都是由所有HTTP模块create_loc_conf方法创建的与location有关的配置项结构体,
* 它们存放着main级别、srv级别、loc级别的配置项参数;
* 这样当前ngx_http_conf_ctx_t是在解析http{}、server{}或location{}块时创建有关;
*/
void **loc_conf;
} ngx_http_conf_ctx_t;
ngx_http_module 核心模块
ngx_http_module 核心模块定义
ngx_http_module 是 HTTP 模块的核心模块,该模块的功能是:定义新的 HTTP 模块类型,并为每个HTTP 模块定义通用接口ngx_http_module_t 结构体,管理HTTP 模块生成的配置项结构体,并解析HTTP 类配置项。该模块定义在文件src/http/ngx_http.c 中:
/* 定义http核心模块 */
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
};
ngx_http_module 作为核心模块,必须定义核心模块的通用接口上下文结构,其通用接口上下文结构定义在文件src/http/ngx_http.c 中:在 ngx_http_module 核心模块中只定义了 http 模块的名称。
/* 定义核心模块的通用接口上下文结构 */
static ngx_core_module_t ngx_http_module_ctx = {
ngx_string("http"),
NULL,
NULL
};
在 ngx_http_module 模块中定义了http{} 块感兴趣的配置项数组,配置项数组定义在文件src/http/ngx_http.c 中:
/* 定义http模块感兴趣的配置项,即配置项指令数组 */
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
};
从 ngx_http_module 模块的定义中可以知道,该模块只有一个初始化处理方法ngx_http_block,该处理方法是HTTP 模块的初始化作用。
ngx_http_module 核心模块初始化
在上面 ngx_http_module 模块的定义中已经知道,HTTP 模块的初始化过程由函数ngx_http_block 实现,首先先给出该函数的整体分析,接着对该函数进行具体的分析。该函数定义在文件src/http/ngx_http.c 中:
/* HTTP框架初始化 */
static char *
ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
char *rv;
ngx_uint_t mi, m, s;
ngx_conf_t pcf;
ngx_http_module_t *module;
ngx_http_conf_ctx_t *ctx;
ngx_http_core_loc_conf_t *clcf;
ngx_http_core_srv_conf_t **cscfp;
ngx_http_core_main_conf_t *cmcf;
/* the main http context */
/* 分配HTTP框架的上下文结构ngx_http_conf_ctx_t 空间 */
ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
if (ctx == NULL) {
return NGX_CONF_ERROR;
}
/*
* conf 是结构体ngx_cycle_t 成员conf_ctx数组中的元素,
* 该元素conf指向ngx_http_module模块所对应的配置项结构信息;
*/
*(ngx_http_conf_ctx_t **) conf = ctx;
/* count the number of the http modules and set up their indices */
/* 初始化所有HTTP模块的ctx_index序号 */
ngx_http_max_module = 0;
for (m = 0; ngx_modules[m]; m++) {
if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
continue;
}
ngx_modules[m]->ctx_index = ngx_http_max_module++;
}
/*
* 分配存储HTTP模块main级别下的main_conf配置项的空间;
*/
/* the http main_conf context, it is the same in the all http contexts */
ctx->main_conf = ngx_pcalloc(cf->pool,
sizeof(void *) * ngx_http_max_module);
if (ctx->main_conf == NULL) {
return NGX_CONF_ERROR;
}
/*
* 分配存储HTTP模块main级别下的srv_conf配置项的空间;
*/
/*
* the http null srv_conf context, it is used to merge
* the server{}s' srv_conf's
*/
ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
if (ctx->srv_conf == NULL) {
return NGX_CONF_ERROR;
}
/*
* 分配存储HTTP模块main级别下的loc_conf配置项的空间;
*/
/*
* the http null loc_conf context, it is used to merge
* the server{}s' loc_conf's
*/
ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
if (ctx->loc_conf == NULL) {
return NGX_CONF_ERROR;
}
/*
* create the main_conf's, the null srv_conf's, and the null loc_conf's
* of the all http modules
*/
/*
* 遍历所有HTTP模块,为每个HTTP模块创建main级别的配置项结构main_conf、srv_conf、loc_conf;
*/
for (m = 0; ngx_modules[m]; m++) {
if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
continue;
}
module = ngx_modules[m]->ctx;
mi = ngx_modules[m]->ctx_index;
/*
* 调用create_main_conf创建main级别的配置项结构main_conf;
*/
if (module->create_main_conf) {
ctx->main_conf[mi] = module->create_main_conf(cf);
if (ctx->main_conf[mi] == NULL) {
return NGX_CONF_ERROR;
}
}
/*
* 调用create_srv_conf创建main级别的配置项结构srv_conf;
*/
if (module->create_srv_conf) {
ctx->srv_conf[mi] = module->create_srv_conf(cf);
if (ctx->srv_conf[mi] == NULL) {
return NGX_CONF_ERROR;
}
}
/*
* 调用create_loc_conf创建main级别的配置项结构loc_conf;
*/
if (module->create_loc_conf) {
ctx->loc_conf[mi] = module->create_loc_conf(cf);
if (ctx->loc_conf[mi] == NULL) {
return NGX_CONF_ERROR;
}
}
}
/*
* 保存待解析配置项结构cf的副本为pcf,待解析完毕后恢复cf;
* 这里备份是由于配置指令解析函数ngx_conf_parse递归调用,因此为了不影响外层的调用环境;
*/
pcf = *cf;
/*
* 把HTTP模块解析指令的上下文参数保存到配置项结构ngx_http_conf_ctx_t ctx中;
*/
cf->ctx = ctx;/* 值-结果 模式 */
/* 遍历所有HTTP模块,并调用每个模块的preconfiguration回调函数 */
for (m = 0; ngx_modules[m]; m++) {
if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
continue;
}
module = ngx_modules[m]->ctx;
if (module->preconfiguration) {
if (module->preconfiguration(cf) != NGX_OK) {
return NGX_CONF_ERROR;
}
}
}
/*
* 调用模块通用配置项解析函数ngx_conf_parse解析http{}块内的指令;
*/
/* parse inside the http{} block */
cf->module_type = NGX_HTTP_MODULE;/* 模块类型为HTTP模块 */
cf->cmd_type = NGX_HTTP_MAIN_CONF;/* 指令类型为HTTP模块的main级别指令 */
/*
* 开始解析http{}块内的指令;
* 这里必须注意的是:http{}块内可能会包含server{}块,
* 而server{}可能会包含location{}块,location{}块会嵌套location{}块;
* 还需注意的是http{}块内可能有多个server{}块,
* location{}块也可能有多个locati