[PATCH 18 of 25] Cache: fix for sending of stale responses

Maxim Dounin mdounin at mdounin.ru
Tue Sep 6 15:58:15 UTC 2011

# HG changeset patch
# User Maxim Dounin <mdounin at mdounin.ru>
# Date 1315324516 -14400
# Node ID a5f19d575163e79c38bdf7e67dc22a72ebb3a7dd
# Parent  2a8a9625e90d91f3a35a55472ad0770f8e02d96b
Cache: fix for 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
@@ -3027,7 +3027,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