how to redirect user from handler

Anatoli Marinov toli at webforge.bg
Thu Sep 20 09:31:12 UTC 2012


I tried to send my buffer with a message  and the code looks like this:

static ngx_int_t ngx_http_my_handler(ngx_http_request_t *r)
{

     header = ngx_list_push(&r->headers_out.headers);
     if (header == NULL) {
         return NGX_HTTP_INTERNAL_SERVER_ERROR;
     }


     header->value.len = strlen("http://new_location.com/1.dat");
     header->value.data = ngx_pcalloc(r->pool, header->value.len);

     ngx_snprintf(header->value.data, header->value.len, "%s", 
"http://new_location.com/1.dat");
     r->headers_out.status = NGX_HTTP_MOVED_TEMPORARILY;
     r->chunked = 0;

     ngx_http_send_header(r);

     b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));

     if (b == NULL) {
         return NGX_HTTP_INTERNAL_SERVER_ERROR;
     }

     b->pos = "Go to new address";
     b->last = "Go to new address" + sizeof("Go to new address");
     b->memory = 1;
     b->last_buf = 1;

     out.buf = b;
     out.next = NULL;

     return ngx_http_output_filter(r, &out);
}

And this seems to work but 302 answer has a body and it is a chunked 
response?
Is it possible to send it without body. I also do not want to be chunked 
if it is possible.



On 09/20/2012 12:12 PM, Anatoli Marinov wrote:
> Hello Colleagues,
> Which is the right way to redirect user request from handler module. 
> Now I have something like this in my handler:
>
> My handler function(r) {
>     header = ngx_list_push(&r->headers_out.headers);
>     header->key.len = sizeof("Location") - 1;
>     header->key.data = (u_char *) "Location";
>
>     header->value.len = strlen("http://new_location.com/1.dat");
>     header->value.data = ngx_pcalloc(r->pool, header->value.len);
>
>     ngx_snprintf(header->value.data, header->value.len, "%s", 
> "http://new_location.com/1.dat");
>     r->headers_out.status = NGX_HTTP_MOVED_TEMPORARILY;
>     r->chunked = 0;
>
>     ngx_http_send_header(r);
>
>     return NGX_DECLINED;
> }
>
> I think this approach is wrong. It sends 302 to the client but there 
> is a body with 404 return code which is not correct.
>
>
> So please advise me how to redirect from hander.
>
> Thanks in advance
> Anatoli Marinov
>
> _______________________________________________
> nginx-devel mailing list
> nginx-devel at nginx.org
> http://mailman.nginx.org/mailman/listinfo/nginx-devel



More information about the nginx-devel mailing list