Handling module ctx reset on internal redirect

Roman Arutyunyan arut at nginx.com
Tue Feb 14 15:41:43 UTC 2017


On Tue, Feb 14, 2017 at 07:25:49AM -0800, Pillai, Saju wrote:
> Hello,
> I am new to nginx and will appreciate some insight on handling the resetting of
> module ctx during internal redirects.
> Specifically, I have a module that runs in the HTTP_POST_READ_PHASE to build
> some objects in memory, which it hangs off the request object via
> ngx_http_set_ctx(r,..). These memory objects back new variables created by this
> module.
> After an internal redirect, when a filter tries to access the variables, my
> handler is unable to find the ctx object via a ngx_http_get_module_ctx(). I see
> that ngx_http_core_module.c:ngx_http_internal_redirect() zeros out all module
> contexts during an internal redirect, and that makes sense. The (appropriate)
> contexts will have to be rebuilt by modules that run for that internal redirect.
> Since HTTP_POST_READ_PHASE handlers dont run on a redirect, how should I setup
> my ctx so I can still access it after a redirect? I could create a r->pool
> cleanup function and hang my ctx in the ngx_pool_cleanup_t->data slot; but that
> seems like a workaround.

Take a look at the standard nginx realip module.
It falls back to pool cleanup when ngx_http_get_module_ctx() returns nothing.
While this solution does look like a workaround, it's ok for this particular

Roman Arutyunyan

More information about the nginx-devel mailing list