对ngx_http_conf_ctx_t结构的思考

本文探讨了ngx_http_conf_ctx_t结构在处理HTTP配置上下文中的角色,强调了指令作用域的概念,包括http{}、server{}和location{}。每个指令由特定模块处理,并遵循特定的合并规则来确定最终配置值。分析了如何处理在同一作用域内重复出现的指令及其可能的合并策略。

前言

ngx_http_conf_ctx_t结构是nginx配置里面的非常非常重要的一个数据结构,这在我们前面的几篇博客中得到充分的体现。这篇博客中我们会对这个数据结构做一个更深入的思考。对这个结构以及这个结构背后暗含的深意。

分析

前面的一系列文章我们说过,nginx配置解析的时候如果遇到"http{...}"、"server{...}"、"location...{...}"这样的一个配置块的时候都会为该块创建一个ngx_http_conf_ctx_t结构,并通过一定的结构将代表所有块的ngx_http_conf_ctx_t组织起来,关于这点,我们已经在前面的几篇博客中阐述过了。
那接下来,让我们剖析下这个结构,看看这个结构内部到底有什么
typedef struct {
    void        **main_conf;
    void        **srv_conf;
    void        **loc_conf;
} ngx_http_conf_ctx_t;
看起来,这个结构内部只有三个void*数组,那为什么需要弄出这三个数组呢?
说起这个,我们就再来研究下nginx配置文件的形式,我们还是以http{}为例来说明,一个典型的配置如下所示:
http {  
    test_str "hi"  
    ...  
    server {  
        ...  
        test_str "how"  
        location /hello {  
            ...  
        }  
        location /hi {  
            ...  
            test_str "how are u"  
        }  
        ...  
    }  
  
    server {  
        location /x {  
            ...  
        }  
        location /y {  
            ...  
        }  
    }   
}  
在这里我们发现,nginx的配置有以下一些特点:
  1. 每个指令可出现在http{}内、server{}内、location{}内,每个指令必须由某个模块来处理;
  2. 每个指令既可以出现在http{}块内、server{}块内、location{}块内,同时出现的指令必须由处理该指令的模
static ngx_int_t ngx_http_flv_live_init(ngx_conf_t *cf) { ngx_str_t *value; ngx_array_t a; ngx_http_handler_pt *h; ngx_http_flv_live_fmt_t *fmt; ngx_http_flv_live_main_conf_t *lmcf; ngx_http_core_main_conf_t *cmcf; lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_flv_live_module); if (lmcf->combined_used) { if (ngx_array_init(&a, cf->pool, 1, sizeof(ngx_str_t)) != NGX_OK) { return NGX_ERROR; } value = ngx_array_push(&a); if (value == NULL) { return NGX_ERROR; } *value = ngx_http_combined_fmt; fmt = lmcf->formats.elts; if (ngx_http_flv_live_compile_format(cf, NULL, fmt->ops, &a, 0) != NGX_CONF_OK) { return NGX_ERROR; } } cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); h = ngx_array_push(&cmcf->phases[NGX_HTTP_LOG_PHASE].handlers); if (h == NULL) { return NGX_ERROR; } *h = ngx_http_flv_live_handler; ngx_http_next_body_filter = ngx_http_top_body_filter; ngx_http_top_body_filter = ngx_http_flv_parser_body_filter; return NGX_OK; } static ngx_http_module_t ngx_http_flv_live_module_ctx = { NULL, /* preconfiguration */ ngx_http_flv_live_init, /* postconfiguration */ ngx_http_flv_live_create_main_conf, /* create main configuration */ NULL, /* init main configuration */ NULL, /* create server configuration */ NULL, /* merge server configuration */ ngx_http_flv_live_create_loc_conf, /* create location configuration */ ngx_http_flv_live_merge_loc_conf /* merge location configuration */ }; ngx_module_t ngx_http_flv_live_module = { NGX_MODULE_V1, &ngx_http_flv_live_module_ctx, /* module context */ ngx_http_flv_live_commands, /* module directives */ NGX_HTTP_MODULE, /* module type */ NULL, /* init master */ NULL, /* init module */ NULL, /* init process */ NULL, /* init thread */ NULL, /* exit thread */ NULL, /* exit process */ NULL, /* exit master */ NGX_MODULE_V1_PADDING };
最新发布
08-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值