Filter insertion and ordering

Nick Kew niq at
Tue Jan 8 12:34:43 UTC 2013

OK, so my module needs to insert filters.
I put the usual in a post_config function:

   /* Insert headers_out filter */
 ngx_http_next_header_filter = ngx_http_top_header_filter;
 ngx_http_top_header_filter = my_header_filter;

 /* Insert body_out filter */
 ngx_http_next_body_filter = ngx_http_top_body_filter;
 ngx_http_top_body_filter = my_body_filter;

Only they never get called!

With the aid of gdb I put a breakpoint in my post_config,
and I see ngx_http_top_[header|body]_filters are NULL
at the point where the post_config is being called.
Must mean my module is getting called first, right?
Guess that figures if it was linked in last and works
in Apache 1.x-style order.

Setting a breakpoint in another module's filter, I get to:

613	    return ngx_http_write_filter(r, &out);
(gdb) bt
#0  ngx_http_header_filter (r=0x7f7f728b6000) at ngx_http_header_filter_module.c:613
#1  0x0000000109020834 in ngx_http_chunked_header_filter (r=0x7f7f728b6000) at ngx_http_chunked_filter_module.c:68
… [more header filters] ...
#9  0x0000000108fe2945 in ngx_http_send_header (r=0x7f7f728b6000) at ngx_http_core_module.c:1941
#10 0x0000000109013def in ngx_http_upstream_send_response (r=0x7f7f728b6000, u=0x7f7f72838450) at ngx_http_upstream.c:2063

I infer ngx_http_header_filter must've inserted itself after my module,
and is not calling a "next" filter.  Clearly not useful!

Apart from the obvious WTF, this raises another issue: how does my
module determine its order in the chain?  Its body filter needs to come
before anything that might encode it (like compression or chunking),
but the header filter should ideally come after those operations to
see exactly what will be sent to the Client!

How do I take control?

Nick Kew

More information about the nginx-devel mailing list