[Q] How to iterate over configured locations on process init in custom module?

Andrey Kulikov amdeich at gmail.com
Thu Oct 6 09:40:21 UTC 2016


Hello,

I wrote a custom nginx module, which require specific initialization in
worker process (it can't be done in master process). Initialization should
be performed if server OR location meet certain criteria.
With servers I came out to following solution:

static ngx_int_t
ngx_http_mega_init_process(ngx_cycle_t *cycle)
{
    ngx_uint_t                   s;

    ngx_http_core_srv_conf_t   **cscfp;
    ngx_http_core_main_conf_t   *cmcf;
    ngx_http_mega_srv_conf_t    *sscf; /* My module configuration */

    cmcf  = ngx_http_cycle_get_module_main_conf(cycle,
ngx_http_core_module);
    cscfp = cmcf->servers.elts;

    for (s = 0; s < cmcf->servers.nelts; s++) {

        sscf = cscfp[s]->ctx->srv_conf[ngx_http_mega_module.ctx_index];

        if (!sscf || !sscf->enable) {
            continue;
        }

        <specific initialization functions>
    }
}


But how to iterate over locations in the server?
Is there are any other module, what have similar logic?
Or any other place, where I can find inspiration?

I've tried following, but with no luck:

    ngx_http_core_srv_conf_t   **cscfp;
    ngx_http_core_loc_conf_t   **clcfp;
    ngx_http_core_srv_conf_t    *cscf;

    cmcf  = ngx_http_cycle_get_module_main_conf(cycle,
ngx_http_core_module);
    cscfp = cmcf->servers.elts;

    for (s = 0; s < cmcf->servers.nelts; s++) {

        cscf = cscfp[s];
- OR -
        cscf = cscfp[s]->ctx->srv_conf[ngx_http_core_module.ctx_index];

        if (cscf->named_locations) {

            for (clcfp = cscf->named_locations; *clcfp; clcfp++) {

                ngx_log_debug1(NGX_LOG_DEBUG_HTTP, cycle->log, 0,
                               "MEGA test location: \"%V\"",
&(*clcfp)->name);
            }
        }

here cscf->named_locations is always NULL in both cases, despite I have a
lot of locations in my servers configured in nginx.conf.


--
Best wishes,
Andrey Kulikov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx-devel/attachments/20161006/125775d8/attachment.html>


More information about the nginx-devel mailing list