手机版
你好,游客 登录 注册
背景:
阅读新闻

Nginx配置文件处理过程

[日期:2013-05-19] 来源:Linux社区  作者:wangst4321 [字体:大 中 小]

ngx_http.c文件

这个文件定义了ngx_http_module模块,是属于NGX_CORE_MODULE类型的,包含“http"指令,这个指令的type是NGX_MAIN_CONF | NGX_CONF_BLOCK,对应的set是ngx_http_block。接下来,看看ngx_http_block做些什么。

1.计算NGX_HTTP_MODULE模块的索引值,以及ngx_http_max_module。

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++;
    }

2.创建三个数组main_conf, srv_conf, loc_conf用于存放所有HTTP模块将产生的配置结构体指针。调用每个模块的create_main_conf, create_srv_conf, create_loc_conf,并将返回的配置结构体指针放在对应的数组当中。

    ctx->main_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);

    ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);

    ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);

    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;

        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;
            }
        }
    }

3. 调用每个模块的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;
            }
        }
    }

4. 进入http配置指令的内部指令解析过程,包括server配置指令结构,location配置指令结构等。

pcf = *cf;  /* pcf保存原配置信息 */ 
sp;  cf->ctx = ctx; 

    cf->module_type = NGX_HTTP_MODULE; 
    cf->cmd_type = NGX_HTTP_MAIN_CONF;  /* 改变配置文件解析位置信息 */
    rv = ngx_conf_parse(cf, NULL);

*cf = pcf; /* 最后在返回前,恢复 */ 

5. init http{} main_conf's, merge the server{}s' srv_conf's and its location{}s' loc_conf's 和create location trees。这部分还没看懂。

6.init_phases

7.调用HTTP模块的postconfiguration函数,做后期处理。例如,默认值处理可以放在这里执行。

    for (m = 0; ngx_modules[m]; m++) {
        if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
            continue;
        }

        module = ngx_modules[m]->ctx;

        if (module->postconfiguration) {
            if (module->postconfiguration(cf) != NGX_OK) {
                return NGX_CONF_ERROR;
            }
        }
    }

8. ngx_http_variables_init_vars

9.ngx_http_init_phase_handlers

 

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款