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

Bondarev, Daniil bondarev at amazon.com
Wed Jul 29 23:28:40 UTC 2015


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.

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;



More information about the nginx-devel mailing list