Welcome! Log In Create A New Profile

Advanced

Re: body capture and redirect

September 11, 2018 10:54AM
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@mdounin.ru> 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
> http://mdounin.ru/
> _______________________________________________
> 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

body capture and redirect

dnj0496 869 September 11, 2018 02:00AM

Re: body capture and redirect

Maxim Dounin 365 September 11, 2018 08:56AM

Re: body capture and redirect

dnj0496 586 September 11, 2018 10:54AM

Re: body capture and redirect

dnj0496 607 September 11, 2018 03:30PM

Re: body capture and redirect

Maxim Dounin 382 September 12, 2018 11:00AM



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

Online Users

Guests: 89
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 500 on July 15, 2024
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready