body capture and redirect

Dk Jack dnj0496 at
Tue Sep 11 14:52:54 UTC 2018

Hi Maxim,
Thanks for answering. Yes, my filter is linked in the chain. I am taking the action after reading the entire body, otherwise, I won’t be able to perform my check. 

My server seems to be waiting for some data from nginx. It works as expected if I don’t do internal redirect. I suspect it has something to do with internal redirect and body capture. Thanks 

> On Sep 11, 2018, at 5:55 AM, Maxim Dounin <mdounin at> wrote:
> Hello!
>> On Mon, Sep 10, 2018 at 10:58:29PM -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. My code is something like this...
>> 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;
>>    }
>>    ...
>> }
>> 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. The request seems to get
>> forwarded to my server like I expected. However, my connection seems to
>> hang. Looks like the server seems to be waiting to read more data from
>> nginx. When I interrupt my server (ctrl-c; its a simple python server), it
>> sort breaks out of the read loop and a response is returned. Sending the
>> same request to my server without sending it through my module in nginx,
>> behaves correctly.
>> Could someone more experienced in nginx, point out what I am doing wrong?
>> Is redirect allowed from a body filter handler? Thanks for your help in
>> advance.
> Assuming the "nginx_inspect_body_filter" function is installed 
> into the request body filter chain via the 
> ngx_http_top_request_body_filter, what you are trying to do is 
> wrong.
> You cannot stop reading the request body at arbitrary time and 
> switch to different processing.  This will leave the body 
> half-read, in an inconsistent state, and this in turn will cause 
> various problem during further processing.  That is, connection 
> hang you see is an expected result.
> In the request body filter you have to either return a fatal 
> error, or raise some internal flag for your processing, and then 
> act on this flag after the body is fully read.
> -- 
> Maxim Dounin
> _______________________________________________
> nginx-devel mailing list
> nginx-devel at

More information about the nginx-devel mailing list