Welcome! Log In Create A New Profile

Advanced

Re: redirect after body capture

Maxim Dounin
May 16, 2019 10:14AM
Hello!

On Wed, May 15, 2019 at 05:35:54PM -0700, Dk Jack wrote:

> Hi,
> In my module, I am trying to forward the request to my server based on the
> content of the request body. To acheive this, I've added a body capture
> filter to capture the body. Here are the relevant parts of my code...
>
> static ngx_int_t
> nginx_inspect_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
> {
>
> ... // extract body
> if (if_content_of_interest_in_body(body, body_length)) {
> ngx_str_t uri = ngx_string("/my_location");
> ngx_http_internal_redirect(r, &url, NULL);
> ngx_http_finalize_request(r, NGX_DONE);
> // return NGX_DONE; // causes connection abort.
> }
> ...
> return ngx_http_next_body_filter(r, in);
> }
>
> I have the following conf for '/my_location':
>
> server {
> ...
> location / {
> ...
> }
> location /my_location {
> proxy_pass http://myserver;
> }
> }
>
> However, I am running into an issue with my code. If return NGX_DONE after
> internal redirect, I get a connection abort. If I call, next_body_filter,
> the connection seems to hang.
>
> Doing a redirect, using similar code in PREACCESS_PHASE works without any
> issues. The issue seems to happen only when I wait to read the entire body
> and perform the redirect based on the content of the body. With no body
> capture, it works fine.
>
> Do I need to do anything additional to redirect in the body filter? Thanks
> for your help.

You are not allowed to do any redirects in a request body filter.
Request body filters are to parse / analyze / change the request
body. If there is a problem detected, you can abort the request,
but that's more or less all you can do.

Moreover, when a request body filter is called it may be already
too late to do any redirects - for example, with request buffering
switched off ("proxy_pass_request_buffering off;") reading the
request body happens after the request is already passed to a
backend.

If you really want to do a redirect based on what a request body
filter detected, consider reading the body in your own module, and
once reading the request body complete - redirect based on the
request body filtering results.

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

redirect after body capture

dnj0496 134 May 15, 2019 08:36PM

Re: redirect after body capture

Maxim Dounin 55 May 16, 2019 10:14AM

Re: redirect after body capture

Ranier Vf 86 May 17, 2019 09:06AM



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

Online Users

Guests: 98
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready