Fixing ngx_http_slice_module use with named locations

Mindaugas Rasiukevicius rmind at
Wed May 4 13:20:30 UTC 2016


There is a problem with ngx_http_slice_module when it is used together
with an internal redirection to a named location.  It results in Nginx
workers caching incorrect content and spinning in an infinite loop.
Consider the following fragment:

    location /foo {
        error_page 550 = @bar;
        return 550;
    location @bar {
        slice ...;
        proxy_pass ...;
        proxy_cache ...;

In ngx_http_slice_body_filter(), the slice context is associated with
the subrequest:

    ngx_http_set_ctx(sr, ctx, ngx_http_slice_filter_module);

However, the location re-match is triggered for the subrequests trying
to fetch each slice.  In the ngx_http_named_location() function:

            /* clear the modules contexts */
            ngx_memzero(r->ctx, sizeof(void *) * ngx_http_max_module);

So, the slice context gets lost.  There are several ways to fix this.
Unless you want an API to preserve the context, one simple fix would be
to get the context via r->parent (if ctx == NULL && r != r->main).

What would be the preferred way to fix this?



More information about the nginx-devel mailing list