tags:

views:

23

answers:

1

I'm writing an nginx module. From looking at other examples I'm registering my header filter in my modules postconfiguration hook:

static ngx_int_t
mod_py_postconfig(ngx_conf_t *cf)
{
    ngx_http_next_header_filter = ngx_http_top_header_filter;
    ngx_http_top_header_filter = mod_py_headers_filter;
    return NGX_OK;
}

But the handler is never called. I've set a breakpoint in gdb on ngx_http_top_header_filter change and it seems my module's postconfig is called first, but then runs postconfig of the ngx_http_write_filter_module which overrides ngx_http_top_header_filter w/o storing the old value:

static ngx_int_t
ngx_http_write_filter_init(ngx_conf_t *cf)
{
    ngx_http_top_body_filter = ngx_http_write_filter;

    return NGX_OK;
}

it seems like it is designed to be the very last on called, so how come my module's postconfig is called first?

From what I can see the order of modules is set in objs/ngx_modules.c

I was able to fix the problem by manually reordering the modules there so that my module comes after ngx_http_header_filter_module, but this feels like an ugly hack, and also makes it hard to automate build process as ./configure overwrites this file each time.

A: 

OK, so I figured it out myself. Documenting it here in case anyone else will need it.

I was adding my module to the wrong list. The nginx module is configured through a 'config' file insed module's directory. My had the following line in it:

HTTP_MODULES="$HTTP_MODULES ngx_http_my_module_name"

I searched for HTTP_MODULES usage and found nginx/auto/modules script which actually builds ngx_modules.c file. It turns out there are several possible module lists used by nginx/auto/modules. I needed to add my module to the HTTP_AUX_FILTER_MODULES list like so:

HTTP_AUX_FILTER_MODULES="$HTTP_AUX_FILTER_MODULES ngx_http_my_module_name"

This placed my module at the right place just after HTTP_HEADERS_FILTER_MODULE and fixed the problem.

Vitaly Kushner