[nginx] Upstream: avoid closing client connection in edge case.

Maxim Dounin mdounin at mdounin.ru
Thu Mar 10 20:39:24 UTC 2016

details:   http://hg.nginx.org/nginx/rev/545b5e4d83b2
changeset: 6428:545b5e4d83b2
user:      Justin Li <jli.justinli at gmail.com>
date:      Tue Mar 08 22:31:55 2016 -0500
Upstream: avoid closing client connection in edge case.

If proxy_cache is enabled, and proxy_no_cache tests true, it was previously
possible for the client connection to be closed after a 304. The fix is to
recheck r->header_only after the final cacheability is determined, and end the
request if no longer cacheable.

Example configuration:

proxy_cache foo;
proxy_cache_bypass 1;
proxy_no_cache 1;

If a client sends If-None-Match, and the upstream server returns 200 with a
matching ETag, no body should be returned to the client. At the start of
ngx_http_upstream_send_response proxy_no_cache is not yet tested, thus cacheable
is still 1 and downstream_error is set.

However, by the time the downstream_error check is done in process_request,
proxy_no_cache has been tested and cacheable is set to 0. The client connection
is then closed, regardless of keepalive.


 src/http/ngx_http_upstream.c |  5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)

diffs (15 lines):

diff --git a/src/http/ngx_http_upstream.c b/src/http/ngx_http_upstream.c
--- a/src/http/ngx_http_upstream.c
+++ b/src/http/ngx_http_upstream.c
@@ -2861,6 +2861,11 @@ ngx_http_upstream_send_response(ngx_http
         ngx_http_file_cache_free(r->cache, u->pipe->temp_file);
+    if (r->header_only && !u->cacheable && !u->store) {
+        ngx_http_upstream_finalize_request(r, u, 0);
+        return;
+    }
     p = u->pipe;

More information about the nginx-devel mailing list