在nginx源码分析之启动流程—主框架中,我们知道会调用ngx_conf_parse来解析配置文件,该函数中会通过一个无限的for循环,调用ngx_conf_read_token来一行行的解析。解析完成之后又会调用ngx_conf_handler来进一步处理该配置项,而在ngx_conf_handler中,会遍历ngx_modules[]数组,找到对该函数感兴趣的模块,并且会调用commands数组中设置的set函数来处理。
那么,当nginx遇到"http"配置指令时,它会找到ngx_http_module模块,并且调用对应的set方法:ngx_http_block,在这里,http模块才正式嵌入到nginx的启动流程中,这里也是http模块框架启动的第一步。
首先很简单,就是创建一个类型为ngx_http_conf_ctx_t的结构体,该结构体原型如下:
typedef struct {
void **main_conf;
void **srv_conf;
void **loc_conf;
} ngx_http_conf_ctx_t;
其中main_conf,srv_conf,loc_conf分别指向了存储main、server、location段配置项的数组。
接下来是为每一个数组申请ngx_http_max_module个大小的空间。
*(ngx_http_conf_ctx_t **) conf = ctx;
/* count the number of the http modules and set up their indices */
ngx_http_max_module = ngx_count_modules(cf->cycle, NGX_HTTP_MODULE);
/* 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;
}
/*
* 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;
}
/*
* 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;
}
这里开始遍历所有的http模块,依次调用他们的create_main_conf,create_srv_conf,create_loc_conf方法,这些方法无非就是创建各自的ngx_http_xxx_conf_t结构体,并初始化成员变量。然后将这些结构体依次放在ngx_http_conf_ctx_t三个数组各自的位置。
/*
* create the main_conf's, the null srv_conf's, and the null loc_conf's
* of the all http modules
*/
for (m = 0; cf->cycle->modules[m]; m++) {
if (cf->cycle->modules[m]->type != NGX_HTTP_MODULE) {
continue;
}
module = cf->cycle->modules[m]->ctx;
mi = cf->cycle->modules[m]->ctx_index;
if (module->create_main_conf) {
ctx->main_conf[mi] = module->create_main_conf(cf);
if (ctx->main_conf[mi] == NULL) {
return NGX_CONF_ERROR;
}
}
if (module->create_srv_conf) {
ctx->srv_conf[mi] = module->create_srv_conf(cf);
if (ctx->srv_conf[mi] == NULL) {
return NGX_CONF_ERROR;
}
}
if (module->create_loc_conf) {
ctx->loc_conf[mi] = module->create_loc_conf(cf);
if (ctx->loc_conf[mi] == NULL) {
return NGX_CONF_ERROR;
}
}
}
pcf = *cf;
cf->ctx = ctx;
顾名思义,preconfiguration就是每个http模块在解析配置文件之前调用的方法。
for (m = 0; cf->cycle->modules[m]; m++) {
if (cf->cycle->modules[m]->type != NGX_HTTP_MODULE) {
continue;
}
module = cf->cycle->modules[m]->ctx;
if (module->preconfiguration) {
if (module->preconfiguration(cf) != NGX_OK) {
return NGX_CONF_ERROR;
}
}
}
同样,第一个执行的是ngx_http_core_module的preconfiguration方法,该方法又会去调用ngx_http_variables_add_core_vars,如下:
static ngx_int_t
ngx_http_core_preconfiguration(ngx_conf_t *cf)
{
return ngx_http_variables_add_core_vars(cf);
}
而ngx_http_variables_add_core_vars干的事情就是将http模块的内置变量依次加入到cmcf的variables_keys数组里,
ngx_int_t
ngx_http_variables_add_core_vars(ngx_conf_t *cf)
{
ngx_http_variable_t *cv, *v;
ngx_http_core_main_conf_t *cmcf;
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
cmcf->variables_keys = ngx_pcalloc(cf->temp_pool,
sizeof(ngx_hash_keys_arrays_t));
if (cmcf->variables_keys == NULL) {
return NGX_ERROR;
}
... ...
for (cv = ngx_http_core_variables; cv->name.len; cv++) {
v = ngx_http_add_variable(cf, &cv->name, cv->flags);
if (v == NULL) {
return NGX_ERROR;
}
*v = *cv;
}
return NGX_OK;
}
可以看到,在这里申请了cmcf->variables_keys 的内存空间,并且通过循环的调用ngx_http_add_variable函数将ngx_http_core_variables[]数组里的变量加入到variables_keys 列表中。ngx_http_add_variable的原型如下:
ngx_http_variable_t *
ngx_http_add_variable(ngx_conf_t *cf, ngx_str_t *name, ngx_uint_t flags)
{
ngx_int_t rc;
ngx_uint_t i;
ngx_hash_key_t *key;
ngx_http_variable_t *v;
ngx_http_core_main_conf_t *cmcf;
if (name->len == 0) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid variable name \"$\"");
return NULL;
}
if (flags & NGX_HTTP_VAR_PREFIX) {
return ngx_http_add_prefix_variable(cf, name, flags);
}
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
key = cmcf->variables_keys->keys.elts;
//先检查变量是否已经添加过了
for (i = 0; i < cmcf->variables_keys->keys.nelts; i++) {
if (name->len != key[i].key.len
|| ngx_strncasecmp(name->data, key[i].key.data, name->len) != 0)
{
continue;
}
v = key[i].value;
// 如果已添加,并且是不可变的变量,则提示变量的重复添加
// 其它NGX_HTTP_VAR_CHANGEABLE就是为了让变量的重复添加时不出错,都指向同一变量
if (!(v->flags & NGX_HTTP_VAR_CHANGEABLE)) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"the duplicate \"%V\" variable", name);
return NULL;
}
if (!(flags & NGX_HTTP_VAR_WEAK)) {
v->flags &= ~NGX_HTTP_VAR_WEAK;
}
// 如果变量已添加,并且有NGX_HTTP_VAR_CHANGEABLE标识,则直接返回
return v;
}
//添加变量
v = ngx_palloc(cf->pool, sizeof(ngx_http_variable_t));
if (v == NULL) {
return NULL;
}
v->name.len = name->len;
v->name.data = ngx_pnalloc(cf->pool, name->len);
if (v->name.data == NULL) {
return NULL;
}
//注意,变量是不区分大小写的
ngx_strlow(v->name.data, name->data, name->len);
v->set_handler = NULL;
v->get_handler = NULL;
v->data = 0;
v->flags = flags;
v->index = 0;
rc = ngx_hash_add_key(cmcf->variables_keys, &v->name, v, 0);
if (rc == NGX_ERROR) {
return NULL;
}
if (rc == NGX_BUSY) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"conflicting variable name \"%V\"", name);
return NULL;
}
return v;
}
在往cmcf->variables_keys->keys.elts里添加变量之前,先要判断当前要添加的变量是否之前已经添加过。如果没有则继续添加,当执行完ngx_http_core_module模块的preconfiguration之后,所有的http核心模块的变量就被添加到该数组中了,一共有65个,通过gdb调试打印发现此时的cmcf->variables_keys:
(gdb) p key[0]
$40 = {key = {len = 9, data = 0x7ffff6ca2ba0 "http_host"}, key_hash = 91998378965759, value = 0x7ffff6ca2b68}
(gdb) p key[1]
$41 = {key = {len = 15, data = 0x7ffff6ca2c28 "http_user_agent"}, key_hash = 3611054648123159976, value = 0x7ffff6ca2bf0}
(gdb) p key[2]
$42 = {key = {len = 12, data = 0x7ffff6ca2cb0 "http_referer"}, key_hash = 2740723716345331830, value = 0x7ffff6ca2c78}
(gdb) p key[3]
$43 = {key = {len = 8, data = 0x7ffff6ca2d38 "http_via\b"}, key_hash = 2967689657303, value = 0x7ffff6ca2d00}
(gdb) p key[4]
$44 = {key = {len = 20, data = 0x7ffff6ca2db8 "http_x_forwarded_for"}, key_hash = 2810977803666258672, value = 0x7ffff6ca2d80}
(gdb) p key[5]
$45 = {key = {len = 11, data = 0x7ffff6ca2f48 "http_cookie"}, key_hash = 88410442042824187, value = 0x7ffff6ca2f10}
(gdb) p key[6]
$46 = {key = {len = 14, data = 0x7ffff6ca2fd0 "content_length"}, key_hash = 17349979335510112620, value = 0x7ffff6ca2f98}
(gdb) p key[7]
$47 = {key = {len = 12, data = 0x7ffff6ca3058 "content_type"}, key_hash = 2609428126208685888, value = 0x7ffff6ca3020}
(gdb) p key[8]
$48 = {key = {len = 4, data = 0x7ffff6ca30e0 "host"}, key_hash = 3208616, value = 0x7ffff6ca30a8}
(gdb) p key[9]
$49 = {key = {len = 18, data = 0x7ffff6ca3360 "binary_remote_addr"}, key_hash = 18156848646088231180, value = 0x7ffff6ca3328}
(gdb) p key[59]
$50 = {key = {len = 10, data = 0x7ffff6cc4cf0 "time_local"}, key_hash = 3159641065293689, value = 0x7ffff6cc4cb8}
(gdb) p key[60]
$51 = {key = {len = 11, data = 0x7ffff6cc4d78 "tcpinfo_rtt"}, key_hash = 97792917155386946, value = 0x7ffff6cc4d40}
(gdb) p key[61]
$52 = {key = {len = 14, data = 0x7ffff6cc4e00 "tcpinfo_rttvar"}, key_hash = 17209975403733021093, value = 0x7ffff6cc4dc8}
(gdb) p key[62]
$53 = {key = {len = 16, data = 0x7ffff6cc4e88 "tcpinfo_snd_cwnd\021"}, key_hash = 10503672965383374960, value = 0x7ffff6cc4e50}
(gdb) p key[63]
$54 = {key = {len = 17, data = 0x7ffff6cc4ed0 "tcpinfo_rcv_spacehttp_"}, key_hash = 12019211534282075260, value = 0x7ffff6cc4e98}
(gdb) p key[64]
$55 = {key = {len = 24, data = 0x7ffff6ca47a0 "sent_http_content_lengthhostname"}, key_hash = 27, value = 0x7ffff6cc4858}
(gdb) p key[65]
$56 = {key = {len = 0, data = 0x0}, key_hash = 0, value = 0x0}
这里key[]一共有65个元素,这个有时候会看到一个奇怪的现象,有的地方存储了好几个值。比如key[64]中的data包含了“sent_http_content_length”和“hostname”。
第三个执行的是ngx_http_upstream_module的preconfiguration方法,同样,他会将upstream模块的内置变量也加入到cmcf->variables_keys中,通过调用ngx_http_upstream_add_variables函数完成的。本质上还是调用了ngx_http_add_variable来完成添加操作。
static ngx_int_t
ngx_http_upstream_add_variables(ngx_conf_t *cf)
{
ngx_http_variable_t *var, *v;
for (v = ngx_http_upstream_vars; v->name.len; v++) {
var = ngx_http_add_variable(cf, &v->name, v->flags);
if (var == NULL) {
return NGX_ERROR;
}
var->get_handler = v->get_handler;
var->data = v->data;
}
return NGX_OK;
}
通过gdb调试发现,此时这个数组中已经有64个值了,这些变量都是ngx_http_core_variables[]里的,很明显这是ngx_http_core_module在他前面已经捷足先登了。这时候nginx会把定义在ngx_http_upstream.c的 ngx_http_upstream_vars[]中的变量继续往其中添加,添加完成之后,variables_keys变成了:
-------------------- ngx_http_core_variables[]-------------------------------
(gdb) p key[0]
$25 = {key = {len = 9, data = 0x7ffff6ca2ba0 "http_host"}, key_hash = 91998378965759, value = 0x7ffff6ca2b68}
(gdb) p key[1]
$26 = {key = {len = 15, data = 0x7ffff6ca2c28 "http_user_agent"}, key_hash = 3611054648123159976, value = 0x7ffff6ca2bf0}
(gdb) p key[2]
$27 = {key = {len = 12, data = 0x7ffff6ca2cb0 "http_referer"}, key_hash = 2740723716345331830, value = 0x7ffff6ca2c78}
(gdb) p key[3]
......
$34 = {key = {len = 11, data = 0x7ffff6cc4d78 "tcpinfo_rtt"}, key_hash = 97792917155386946, value = 0x7ffff6cc4d40}
(gdb) p key[63]
$35 = {key = {len = 17, data = 0x7ffff6cc4ed0 "tcpinfo_rcv_spacehttp_sent_http_sent_trailer_cookie_arg_"}, key_hash = 12019211534282075260,
value = 0x7ffff6cc4e98}
(gdb) p key[64]
$36 = {key = {len = 24, data = 0x7ffff6ca47a0 "sent_http_content_lengthhostname"}, key_hash = 27, value = 0x7ffff6cc4858}
------------------------------------ngx_http_upstream_vars[]------------------------------------------
(gdb) p key[64]
$5 = {key = {len = 13, data = 0x7ffff6cc4f40 "upstream_addr"}, key_hash = 2865999093934786517, value = 0x7ffff6cc4f08}
(gdb) p key[65]
$6 = {key = {len = 15, data = 0x7ffff6cc4f98 "upstream_status"}, key_hash = 5660262289136669398, value = 0x7ffff6cc4f60}
(gdb) p key[66]
$7 = {key = {len = 21, data = 0x7ffff6cc4fe0 "upstream_connect_time"}, key_hash = 13496111015847451878, value = 0x7ffff6cc4fa8}
(gdb) p key[67]
$8 = {key = {len = 20, data = 0x7ffff6cc5030 "upstream_header_time"}, key_hash = 18290868616047682779, value = 0x7ffff6cc4ff8}
(gdb) p key[70]
$9 = {key = {len = 23, data = 0x7ffff6cc5160 "upstream_bytes_received"}, key_hash = 3752868925487481369, value = 0x7ffff6cc5128}
(gdb) p key[71]
$10 = {key = {len = 19, data = 0x7ffff6cc51f0 "upstream_bytes_sent"}, key_hash = 6540449568393741264, value = 0x7ffff6cc51b8}
......
之后还会有其他的模块将自己的内置变量依次加入到variables数组里。
我们知道ngx_init_cycle中会调用ngx_conf_parse来解析配置文件,当遇到了http配置时,会调用ngx_http_block来处理,但是ngx_http_block又一次调用了ngx_conf_parse函数,目的是为了解析http块内的配置项。