美文网首页
读书笔记#2-2020-深入理解nginx#2-之配置模型

读书笔记#2-2020-深入理解nginx#2-之配置模型

作者: areece | 来源:发表于2020-01-13 16:37 被阅读0次

    core-module

    core-module的配置保存在cycle的conf_ctx数组里面,它是在ngx_init_cycle时创建的,只针对core-module调用create-conf,并且保存。我们最熟悉的,估计就是ngx_event_module与ngx_http_module。它俩负责管理其它其它的event模块与http模块。

    cycle的流程是这样的,首先调用各个core-module的create-conf,再进行配置文件的解析ngx_conf_parse(),,再调用init-conf。而ngx_event_module和ngx_http_module都没有选择在create_module中分配存储结构,而是在配置解析的时候做的。

    先是创建

        for (i = 0; cycle->modules[i]; i++) {
            if (cycle->modules[i]->type != NGX_CORE_MODULE) {
                continue;
            }
    
            module = cycle->modules[i]->ctx;
    
            if (module->create_conf) {
                rv = module->create_conf(cycle);
                if (rv == NULL) {
                    ngx_destroy_pool(pool);
                    return NULL;
                }
                cycle->conf_ctx[cycle->modules[i]->index] = rv;
            }
        }
    

    然后是解析

        if (ngx_conf_parse(&conf, &cycle->conf_file) != NGX_CONF_OK) {
            environ = senv;
            ngx_destroy_cycle_pools(&conf);
            return NULL;
        }
    

    然后再init-conf

        for (i = 0; cycle->modules[i]; i++) {
            if (cycle->modules[i]->type != NGX_CORE_MODULE) {
                continue;
            }
    
            module = cycle->modules[i]->ctx;
    
            if (module->init_conf) {
                if (module->init_conf(cycle,
                                      cycle->conf_ctx[cycle->modules[i]->index])
                    == NGX_CONF_ERROR)
                {
                    environ = senv;
                    ngx_destroy_cycle_pools(&conf);
                    return NULL;
                }
            }
        }
    

    event-module之ngx_event_core_module与ngx_event_epoll_module

    一般我们不自己写event模块,那么就用ngx_event_core_module与ngx_event_epoll_module为例子来说说。对于event模块,http_events_block()函数会创建所有event_module的配置数组,

        *ctx = ngx_pcalloc(cf->pool, ngx_event_max_module * sizeof(void *));
        if (*ctx == NULL) {
            return NGX_CONF_ERROR;
        }
    
        *(void **) conf = ctx;
    

    其中最重要的两个event_module就是ngx_event_core_module,它要保存的配置项就是use,也就是使用哪个event实现模块实现事件机制。而ngx_event_epoll_module则是在linux上缺省的事件模块,它有一些自己要处理的参数需要保存。整个架构和core-module是类似的,也是create-conf创建events配置,接着继续解析配置文件,再init-conf操作。

        for (i = 0; cf->cycle->modules[i]; i++) {
            if (cf->cycle->modules[i]->type != NGX_EVENT_MODULE) {
                continue;
            }
    
            m = cf->cycle->modules[i]->ctx;
    
            if (m->create_conf) {
                (*ctx)[cf->cycle->modules[i]->ctx_index] =
                                                         m->create_conf(cf->cycle);
                if ((*ctx)[cf->cycle->modules[i]->ctx_index] == NULL) {
                    return NGX_CONF_ERROR;
                }
            }
        }
    

    core-module之ngx_http_module

    非常类似,http的core-module就是ngx_http_module,它由cycle管理,cycle的ctx里面保存所有的http的conf,这实际上就是ngx_http_conf_ctx。ngx_http_conf_ctx里面有三个指针main, srv, loc。但是它们是在ngx_http_block()函数中创建的:

        ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
        if (ctx == NULL) {
            return NGX_CONF_ERROR;
        }
    
        *(ngx_http_conf_ctx_t **) conf = ctx;
    

    如果是自己写的http模块,就只是需要处理main, srv或者loc的配置项就得了,而一个我们经常会碰到的就是ngx_http_core_module模块的配置,它们分别是ngx_http_core_main_conf,ngx_http_core_srv_conf和ngx_http_core_loc_conf。

    这个配置模型还是有点容易让人糊涂的,主要是ngx_http_conf_ctx会创建很多份,一份是由ngx_http_block创建的,以core-module的身份创建,满足cycle的管理需求,同时也对应到http配置项,而在解析到server和location时也会创建这样一个结构,但是这一次其实是http模块自己的事情了(也就是说,后面的事情没有了,cycles也能够正常运转)。第二层的对应server层的ngx_http_conf_ctx,保存在ngx_http_core_main_conf的severs成员里,第三层及下面的对应到location的配置,第三层直接放在ngx_http_core_srv_conf的named_locations成员,而再往下,就放在父location的ngx_http_core_locl_conf的locations成员下了。

    也就是说,顶层的ngx_http_conf_ctx,也就是http{}对应的配置,能够直接通过cycle找到,而非顶层的,需要借助于ngx_http_core_module的帮助,如果是对应到某个server的配置,需要通过ngx_http_core_main_conf里面的servers成员匹配到,同理location相关的,也要顺着ngx_http_core_srv_conf和ngx_http_core_loc_conf顺着往下走。

    实际上,不是直接找到,是先要找到ngx_http_core_srv_conf或者ngx_http_core_loc_conf之后,再通过它的ctx成员或者loc_conf找到ngx_http_conf_ctx对象。

    其实从代码中看,在server层,还保存了整个server有效的ngx_http_conf_ctx对象,就是在函数ngx_http_core_server()中

        /* the server configuration context */
    
        cscf = ctx->srv_conf[ngx_http_core_module.ctx_index];
        cscf->ctx = ctx;
    

    但是在loc层,只保存了location相关的配置,如下在函数中ngx_http_core_location()中

       ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
    
        clcf = ctx->loc_conf[ngx_http_core_module.ctx_index];
        clcf->loc_conf = ctx->loc_conf;
    

    可见并没有保存整个的

    相关文章

      网友评论

          本文标题:读书笔记#2-2020-深入理解nginx#2-之配置模型

          本文链接:https://www.haomeiwen.com/subject/pvpgactx.html