redirect after body capture

Maxim Dounin mdounin at mdounin.ru
Thu May 16 14:12:41 UTC 2019


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/


More information about the nginx-devel mailing list