代码改变世界

nginx http子模块conf的初始化

2011-09-05 22:34  Sun Yongyue  阅读(1396)  评论(0编辑  收藏  举报

nginx.conf文件中有http这个模块,里边可以加入各种子模块ngx_http_module_t ,这些子模块自定义conf的初始化过程如下??

 

一、几个结构

 

ngx_conf_t                  // 路人甲

ngx_http_XXX_(main|srv|loc)_conf_t     // 自定义的conf结构

ngx_command_t        // 各个Directives

ngx_http_module_t   // http中的子module

ngx_module_t           // 高层module

 

二、一些函数

http中子module初始化的过程:

main -> ngx_init_cycle -> ngx_conf_param -> ngx_conf_param -> ngx_conf_handler -> ngx_http_block -> ngx_http_merge_servers

在最后两个函数中调用了ngx_http_module_t结构中各个create、init和merge函数做自定义conf结构的初始化、合并等。

 

 

 

 

1. Main中调用了ngx_init_cycle,做了以下操作:

// ngx_module_t  ngx_http_module是NGX_CORE_MODULE

 

rv = module->create_conf(cycle);

cycle->conf_ctx[ngx_modules[i]->index] = rv;

 

conf.ctx = cycle->conf_ctx;. //此外的conf是一个ngx_conf_t

 

 

2. Ngx_init_cycle调用了ngx_conf_param,把上边conf传过来

ngx_conf_param(&conf)

 

 

3. Nginx_conf_param调用ngx_conf_parse

 rv = ngx_conf_parse(cf, NULL);,又接收了上方

 

 

4. ngx_conf_parse调用ngx_conf_handler

rc = ngx_conf_handler(cf, rc);,又接收上方

 

 

5. Ngx_conf_handler中把conf取出并传给module中各命令的set函数。

conf = NULL;

if (cmd->type & NGX_DIRECT_CONF) {

    conf = ((void **) cf->ctx)[ngx_modules[i]->index];

} else if (cmd->type & NGX_MAIN_CONF) {

    conf = &(((void **) cf->ctx)[ngx_modules[i]->index]);

} else if (cf->ctx) {

    confp = *(void **) ((char *) cf->ctx + cmd->conf);

    if (confp) {

        conf = confp[ngx_modules[i]->ctx_index];

    }

}

rv = cmd->set(cf, cmd, conf); 

 

 

6. Ngx_http_module_t中的http命令的set函数为ngx_http_block,其中做了一系列的各模块自定义的conf的创建、初始化等操作。最后调用的ngx_http_merge_servers又调用了各个merge函数。

for (m = 0; ngx_modules[m]; m++) {

 

        ctx->main_conf[mi] = module->create_main_conf(cf);

        ctx->srv_conf[mi] = module->create_srv_conf(cf);

        ctx->loc_conf[mi] = module->create_loc_conf(cf);

}    

 

for (m = 0; ngx_modules[m]; m++) {

 

       module->preconfiguration(cf)

}

for (m = 0; ngx_modules[m]; m++) {

 

        rv = module->init_main_conf(cf, ctx->main_conf[mi]);

    rv = ngx_http_merge_servers(cf, cmcf, module, mi); // 此处调用了各merge

}

for (m = 0; ngx_modules[m]; m++) {

    module->postconfiguration(cf)

}

 

 

 

7. ngx_http_merge_servers又调用了各个merge函数。

for (s = 0; s < cmcf->servers.nelts; s++) {

        rv = module->merge_srv_conf(cf, saved.srv_conf[ctx_index],

                                    cscfp[s]->ctx->srv_conf[ctx_index]);

        rv = module->merge_loc_conf(cf, saved.loc_conf[ctx_index],

                                    cscfp[s]->ctx->loc_conf[ctx_index]);

}