Welcome! Log In Create A New Profile

Advanced

Re: how to redirect user from handler

Maxim Dounin
September 20, 2012 09:18AM
Hello!

On Thu, Sep 20, 2012 at 12:31:12PM +0300, Anatoli Marinov wrote:

[...]

> And this seems to work but 302 answer has a body and it is a chunked
> response?

This is because you said it to.

> Is it possible to send it without body.

No, it's not allowed by a protocol (unless request was HEAD).
Response body must be present. It may be empty though.

> I also do not want to be chunked if it is possible.

It is. You should set content length, then chunked encoding won't
be used. You may take a look at ngx_http_send_response() for a
complete code (and the function is actually a usefull helper for
simple cases when you need to return some predefined response).

But actually to return a 302 redirect from a content handler it's
enough to set Location header and return NGX_HTTP_MOVED_TEMPORARILY,
i.e. to do something like this:

ngx_http_clear_location(r);

r->headers_out.location = ngx_palloc(r->pool, sizeof(ngx_table_elt_t));
if (r->headers_out.location == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}

/*
* we do not need to set the r->headers_out.location->hash and
* r->headers_out.location->key fields
*/

ngx_str_set(&r->headers_out.location->value, "http://example.com/");

return NGX_HTTP_MOVED_TEMPORARILY;

(The code was mostly borrowed from static module, with minor
modification.)

Maxim Dounin


>
>
>
> 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@nginx.org
> >http://mailman.nginx.org/mailman/listinfo/nginx-devel
>
> _______________________________________________
> nginx-devel mailing list
> nginx-devel@nginx.org
> http://mailman.nginx.org/mailman/listinfo/nginx-devel

_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

how to redirect user from handler

toli 1192 September 20, 2012 05:14AM

Re: how to redirect user from handler

toli 725 September 20, 2012 05:32AM

Re: how to redirect user from handler

Maxim Dounin 634 September 20, 2012 09:18AM

Re: how to redirect user from handler

toli 1088 September 20, 2012 09:50AM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 237
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready