Filter insertion and ordering
Maxim Dounin
mdounin at mdounin.ru
Tue Jan 8 13:52:05 UTC 2013
Hello!
On Tue, Jan 08, 2013 at 12:34:43PM +0000, Nick Kew wrote:
> 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?
Filter ordering is determinded during configure - config script of
your module should place it into appropriate list, usually
HTTP_AUX_FILTER_MODULES. Your symptoms suggests you've added your
filter module into HTTP_MODULES list instead.
You may want to refer here for basic instructions:
http://www.evanmiller.org/nginx-modules-guide.html#compiling
And to auto/modules code for all the details.
--
Maxim Dounin
http://nginx.com/support.html
More information about the nginx-devel
mailing list