returning a 302 from ACCESS_PHASE handler

Michael Ellery mellery451 at gmail.com
Tue Sep 10 21:33:31 UTC 2013


Hello,

I have an ACCESS_PHASE handler and, in some cases, I want to return a 302 to a different domain.  I'm currently doing
the following:

<code>
    ngx_table_elt_t *set_location = ngx_list_push(&r->headers_out.headers);
    if (set_location == NULL) {
       SXEL2("ERROR: failed to add location header");
       return -1;
    }
       
    r->err_status = NGX_HTTP_MOVED_TEMPORARILY;
       
    set_location->hash = 1;
    ngx_str_set(&set_location->key, "Location");
    set_location->value.len = len;
    set_location->value.data = location;
       
    ngx_http_clear_location(r);
   
    ngx_http_finalize_request(r, NGX_HTTP_MOVED_TEMPORARILY);
   
</code>

and then returning NGX_OK from my handler.

This seems to work in the sense that I do get the expected 302 at my client - HOWEVER, I also get a segfault:

2013/09/10 13:26:22 [alert] 28910#0: worker process 29362 exited on signal 11

...which seems to be happening at ngx_http_proxy_module.c:645

        ngx_http_set_ctx(r, ctx, ngx_http_proxy_module);

because r->ctx is NULL at this point.

I suspect that calling ngx_http_finalize_request from my handler is causing this, although I have not conclusively
proven this.

Does someone know if it's possible to return a redirect from ACCESS handlers and, if so, what is the proper way to
accomplish it?

TIA,
Mike Ellery



More information about the nginx-devel mailing list