[PATCH 25 of 31] Cache: fix sending of stale responses

Maxim Dounin mdounin at mdounin.ru
Mon Jun 27 21:06:55 MSD 2011

# HG changeset patch
# User Maxim Dounin <mdounin at mdounin.ru>
# Date 1309187446 -14400
# Node ID 3210e8744bf18fc2470d47157df754f068451d48
# Parent  a4ddc3c062f0eb2895888fe2cec43a82e5f9c273
Cache: fix sending of stale responses.

For normal cached responses ngx_http_cache_send() sends last buffer and then
request finalized via ngx_http_finalize_request() call, i.e. everything is

But for stale responses (i.e. when upstream died, but we have something in
cache) the same ngx_http_cache_send() sends last buffer, but then in
ngx_http_upstream_finalize_request() another last buffer is send.  This
causes duplicate final chunk to appear if chunked encoding is used (and
resulting problems with keepalive connections and so on).

Fix this by not sending in ngx_http_upstream_finalize_request()
another last buffer if we know response was from cache.

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
@@ -3016,7 +3016,12 @@ ngx_http_upstream_finalize_request(ngx_h
     r->connection->log->action = "sending to client";
-    if (rc == 0) {
+    if (rc == 0
+        && !r->cached
+       )
+    {
         rc = ngx_http_send_special(r, NGX_HTTP_LAST);

More information about the nginx-devel mailing list