[PATCH] Upstream: Cache stale responses if they may be revalidated.

Thorvaldur Thorvaldsson thorvaldur.thorvaldsson at gmail.com
Sat Dec 19 01:41:25 UTC 2015


# HG changeset patch
# User Thorvaldur Thorvaldsson <thorvaldur.thorvaldsson at gmail.com>
# Date 1450486750 -3600
#      Sat Dec 19 01:59:10 2015 +0100
# Node ID d5f8a24ee96d47f056949f3a103fd53a9dd56282
# Parent  def9c9c9ae05cfa7467b0ec96e76afa180c23dfb
Upstream: Cache response if max-age=0 and it may be revalidated.

Previously, the proxy cache would never store responses with "max-age=0"
in the Cache-Control header. Now it will, but only if
"proxy_cache_revalidate" is "on" and the response includes an ETag or a
valid Last-Modified header.

This opens up the possibilty to make nginx cache responses that must
always be revalidated, e.g., when authorization is required (and cheap).

diff -r def9c9c9ae05 -r d5f8a24ee96d src/http/ngx_http_upstream.c
--- a/src/http/ngx_http_upstream.c Sat Dec 12 10:32:58 2015 +0300
+++ b/src/http/ngx_http_upstream.c Sat Dec 19 01:59:10 2015 +0100
@@ -2815,11 +2815,16 @@
             valid = ngx_http_file_cache_valid(u->conf->cache_valid,
                                               u->headers_in.status_n);
             if (valid) {
-                r->cache->valid_sec = now + valid;
+                valid = now + valid;
+                r->cache->valid_sec = valid;
             }
         }

-        if (valid) {
+        if (valid > now
+            || (valid && r->upstream->conf->cache_revalidate
+                && (u->headers_in.etag
+                    || u->headers_in.last_modified_time != -1)))
+        {
             r->cache->date = now;
             r->cache->body_start = (u_short) (u->buffer.pos - u->buffer.start);

@@ -4272,12 +4277,7 @@
         return NGX_OK;
     }

-    if (n == 0) {
-        u->cacheable = 0;
-        return NGX_OK;
-    }
-
-    r->cache->valid_sec = ngx_time() + n;
+    r->cache->valid_sec = ngx_time() + (n ? n : -1);
     }
 #endif



More information about the nginx-devel mailing list