[PATCH] Set Content-Length to 0 when proxying requests with discarded body

Roman Arutyunyan arut at nginx.com
Fri Aug 7 20:25:20 UTC 2015


Hello Daniil,

> On 29 Jul 2015, at 16:28, Bondarev, Daniil <bondarev at amazon.com> wrote:
> 
> Hello,
> 
> We have a use case where we need to discard request body before proxying
> request to the upstream. To do this we call ngx_http_discard_request_body,
> but it uses r->headers_in.content_length_n to store the amount of data nginx
> wants to receive next time, so it won't be 0 until nginx read all bytes from the
> client. So if proxy_request_buffering is set to off, nginx ends up sending
> non-0 Content-Length header to the upstream without a body.

If you have no "proxy_pass_request_body off” or “proxy_set_body ‘’", it’s assumed
that you want your request body to be passed to the upstream.  Missing body
is an error and probably it’s not a good idea to silently ignore it setting
zero Content-Length.

> The following patch fixes this behavior.
> 
> # HG changeset patch
> # User Daniil Bondarev <bondarev at amazon.com>
> # Date 1438119116 25200
> # Node ID ddefee93b698b9261a147a08f42a07810efa2dab
> # Parent  341e4303d25be159d4773b819d0ec055ba711afb
> Set Content-Length to 0 when proxying requests with discarded body
> 
> diff -r 341e4303d25b -r ddefee93b698 src/http/modules/ngx_http_proxy_module.c
> --- a/src/http/modules/ngx_http_proxy_module.c  Thu Jul 16 14:20:48 2015 +0300
> +++ b/src/http/modules/ngx_http_proxy_module.c  Tue Jul 28 14:31:56 2015 -0700
> @@ -1221,6 +1221,9 @@
>         ctx->internal_body_length = body_len;
>         len += body_len;
> 
> +    } else if (r->discard_body) {
> +        ctx->internal_body_length = 0;
> +
>     } else if (r->headers_in.chunked && r->reading_body) {
>         ctx->internal_body_length = -1;
>         ctx->internal_chunked = 1;
> 
> _______________________________________________
> nginx-devel mailing list
> nginx-devel at nginx.org
> http://mailman.nginx.org/mailman/listinfo/nginx-devel
> 

--
Roman Arutyunyan





More information about the nginx-devel mailing list