Is there a wait to get at a module's main conf inside the loc conf's init?

Maxim Dounin mdounin at mdounin.ru
Thu Aug 17 12:39:30 UTC 2017


Hello!

On Thu, Aug 17, 2017 at 02:34:20AM -0400, ptcell wrote:

> Maxim Dounin Wrote:
> -------------------------------------------------------
> 
> > 
> > Try ngx_http_conf_get_module_main_conf().
> > 
> > (Doing so in the create_loc_conf callback is probably pointless 
> > though, as main conf contents can be changed later.)
> 
> On a related note, is there a way to get the my module's main/srv/loc conf_t
> instances from the cycle instance passed to the worker process init callback
> in my module?     I see my conf objects created and put into the ctx object
> before the worker process init is called, so I think there there.

This is something generally better to avoid (because getting a 
module configuration from a cycle implies that there can be only 
one http{} block), but it is certainly possible to obtain main 
configuration via ngx_http_cycle_get_module_main_conf().  For an 
example, see ngx_http_perl_init_worker() in the perl module:

http://hg.nginx.org/nginx/file/tip/src/http/modules/perl/ngx_http_perl_module.c#l1041

> From reading the code, I tried something like:
> 
> #define ngx_http_cycle_get_module_main_conf(cf, module)                     
>   \
>     ((ngx_http_conf_ctx_t *)
> (cycle)->conf_ctx)->main_conf[(module).ctx_index]

It is not clear where you get this macro.  Defined it yourself?  
You certainly did it wrong.  Correct macro is defined in 
src/http/ngx_http_config.h as follows:

#define ngx_http_cycle_get_module_main_conf(cycle, module)                    \
    (cycle->conf_ctx[ngx_http_module.index] ?                                 \
        ((ngx_http_conf_ctx_t *) cycle->conf_ctx[ngx_http_module.index])      \
            ->main_conf[module.ctx_index]:                                    \
        NULL)

You don't need to redefine it, just use it as is.

-- 
Maxim Dounin
http://nginx.org/


More information about the nginx mailing list