您的位置:首页 > 理论基础 > 计算机网络

Nginx学习笔记(七):HTTP配置模型

2014-01-14 21:36 316 查看

前言

这两天回家日期越来越近,长夜漫漫,归家心切,无心学习,就差问晶晶姑娘了。。算了,还是学习吧。通过书中本章前两节的学习,已经基本上了解了HTTP模块怎么获取感兴趣的配置项。这篇主要就是学习HTTP配置模型的实现。

HTTP配置模型

ngx_http_conf_ctx_t结构体

typedef struct {
    /* 三个指针数组,数组中每个元素分别指向由create_main/srv/loc_conf方法产生的配置结构体 */
    void        **main_conf;
    void        **srv_conf;
    void        **loc_conf;
} ngx_http_conf_ctx_t;

static char *
ngx_http_core_server(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)
{
    ...
    /* HTTP模块创建ngx_http_conf_ctx_t结构体 */
    ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
    if (ctx == NULL) {
        return NGX_CONF_ERROR;
    }

    http_ctx = cf->ctx;
    ctx->main_conf = http_ctx->main_conf;

    /* the server{}'s srv_conf */
    /* 生成一个srv指针数组存储所有srv配置 */
    ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
    if (ctx->srv_conf == NULL) {
        return NGX_CONF_ERROR;
    }

    /* the server{}'s loc_conf */
    /* 生成一个loc指针数组存储所有srv里的loc配置 */
    ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
    if (ctx->loc_conf == NULL) {
        return NGX_CONF_ERROR;
    }

    /* 遍历所有HTTP模块 */
    for (i = 0; ngx_modules[i]; i++) {
        if (ngx_modules[i]->type != NGX_HTTP_MODULE) {
            continue;
        }

        module = ngx_modules[i]->ctx;

        /* 如果遍历的该模块实现了srv/loc方法,则调用,并将返回的地址存储到srv/loc指针数组中 */
        if (module->create_srv_conf) {
            mconf = module->create_srv_conf(cf);
            if (mconf == NULL) {
                return NGX_CONF_ERROR;
            }

            ctx->srv_conf[ngx_modules[i]->ctx_index] = mconf;
        }

        if (module->create_loc_conf) {
            mconf = module->create_loc_conf(cf);
            if (mconf == NULL) {
                return NGX_CONF_ERROR;
            }

            ctx->loc_conf[ngx_modules[i]->ctx_index] = mconf;
        }
    }
    ...
}


解析HTTP配置流程

画下书中流程,梳理梳理。



HTTP配置模型的内存布局

还是直接画上书中的解释图,一看就明白的东西。这种内存布局设计对生成的ngx_http_XXX_conf_t配置结构体的存储以及对配置文件的解析做了相当好的解释。





如何合并配置项

还是直接画下解析完http配置后的配置合并流程图。



总结

几个图说的清清楚楚。另外,本章还有error日志以及请求上下文两节。error日志就是打印信息函数的实现以及error的级别的介绍。所谓的请求上下文也是再熟悉不过了。之前所在实习公司的全新系统架构中,用的就是全异步的请求机制,对请求上下文这东西,是相当的感性了。然后,现在专注书中设计及业务流程上的学习,之后再此基础上开始做源码的分析。

主要参考

《深入理解Nginx》
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: