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

Maxim Dounin mdounin at mdounin.ru
Tue Feb 15 16:33:41 MSK 2011

# HG changeset patch
# User Maxim Dounin <mdounin at mdounin.ru>
# Date 1297776703 -10800
# Node ID 11baba299a5b7a90c70f37fbfd28ad8a8f5eab58
# Parent  c0eb70bfa1d7e6cfe3c246a508dba9cdfe2708a2
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