Nginx 配置文件源码分析

main
|
|__>ngx_init_cycle
        |
        |__>ngx_conf_parse


ngx_cycle_t *
ngx_init_cycle(ngx_cycle_t *old_cycle){
    …………
    ngx_conf_t           conf;//保存解析后的每项配置
    …………
    for (i = 0; cycle->modules[i]; i++) {
        if (cycle->modules[i]->type != NGX_CORE_MODULE) {
            continue;
        }
        //ngx_module_s结构初始化时将一个ngx_core_module_t的全局变量指针赋值给ctx
        //ngx_core_module_t包含回调函数create_conf
        module = cycle->modules[i]->ctx;

        if (module->create_conf) {
            //create_conf回调函数会返回一个palloc分配的结构指针
            //该结构就是模块的配置结构如ngx_core_conf_t
            rv = module->create_conf(cycle);
            if (rv == NULL) {
                ngx_destroy_pool(pool);
                return NULL;
            }
            //将模块的配置结构复制到全局的cyccle中
            cycle->conf_ctx[cycle->modules[i]->index] = rv;
        }
    }
    …………
    //将module结构赋值给conf,建立module与conf的关联
    conf.ctx = cycle->conf_ctx;
    conf.cycle = cycle;
    conf.pool = pool;
    conf.log = log;
    conf.module_type = NGX_CORE_MODULE;
    conf.cmd_type = NGX_MAIN_CONF;
    …………
    //读取配置
    if (ngx_conf_parse(&conf, &cycle->conf_file) != NGX_CONF_OK) {
        environ = senv;
        ngx_destroy_cycle_pools(&conf);
        return NULL;
    }
}
ngx_conf_s
    |
    |__>ctx
    |
    |__>ngx_cycle_s
    |       |
    |       |__conf_ctx[]<___________________
    |       |                                |
    |       |__ngx_module_t[]                |
    |           |                            |
    |           |__ctx                       |
    |               |                        |
                    |__>create_conf()________|





  
            
           

配置解析

ngx_conf_parse
|
|__>ngx_conf_read_token//解析关键字
|
|__>ngx_conf_handler//通过set回调设置cf->handler
|
|__>cf->handler//解析命令


char *
ngx_conf_parse(ngx_conf_t *cf, ngx_str_t *filename){
    …………
    //读取配置文件
    fd = ngx_open_file(filename->data, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0);
    …………
    //循环解析配置
    //解析配置文件
    for ( ;; ) {
        rc = ngx_conf_read_token(cf);
        …………
        //cf->handler 回调函数
        //用于解析具体的命令,该命令就是ngx_command_s中的name
        //通过ngx_command_s中的set设置该回调函数
        if (cf->handler) {
            …………
            //解析ngx_command_s项配置
            rv = (*cf->handler)(cf, NULL, cf->handler_conf);
            …………
        }
        …………
        //在该函数中调用ngx_command_s中的set设置该回调函数
        rc = ngx_conf_handler(cf, rc);
        …………
    }
}


static ngx_int_t
ngx_conf_handler(ngx_conf_t *cf, ngx_int_t last){
    …………
    //ngx_command_t结构中的set回调函数 
    //http模块的set就是ngx_http_block
    rv = cmd->set(cf, cmd, conf);
    …………
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值