[PATCH] Upstream: Cache "immediately stale" responses if revalidate is on.

Thorvaldur Thorvaldsson thorvaldur.thorvaldsson at gmail.com
Mon Dec 14 17:17:21 UTC 2015


# HG changeset patch
# User Thorvaldur Thorvaldsson <thorvaldur.thorvaldsson at gmail.com>
# Date 1450109082 -3600
#      Mon Dec 14 17:04:42 2015 +0100
# Node ID e017db1282d4b8c4d541416e42fe7df8abc73302
# Parent  def9c9c9ae05cfa7467b0ec96e76afa180c23dfb
Upstream: Cache "immediately stale" responses if revalidate is on.

Previously, the proxy cache would never store responses if max-age=0,
even when "proxy_cache_revalidate" was "on" and the response included an
ETag. This came as a surprise.

Now, a header like "Cache-Control: max-age=0, must-revalidate" can be
used to make nginx cache responses that always require revalidation,
like, when authorization is required (and cheap).

diff -r def9c9c9ae05 -r e017db1282d4 src/http/ngx_http_file_cache.c
--- a/src/http/ngx_http_file_cache.c Sat Dec 12 10:32:58 2015 +0300
+++ b/src/http/ngx_http_file_cache.c Mon Dec 14 17:04:42 2015 +0100
@@ -628,7 +628,7 @@

     now = ngx_time();

-    if (c->valid_sec < now) {
+    if (c->valid_sec <= now) {

         ngx_shmtx_lock(&cache->shpool->mutex);

@@ -831,7 +831,7 @@

         if (fcn->error) {

-            if (fcn->valid_sec < ngx_time()) {
+            if (fcn->valid_sec <= ngx_time()) {
                 goto renew;
             }

diff -r def9c9c9ae05 -r e017db1282d4 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 Mon Dec 14 17:04:42 2015 +0100
@@ -2819,7 +2819,7 @@
             }
         }

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

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

-    if (n == 0) {
+    if (n == 0 && !r->upstream->conf->cache_revalidate) {
         u->cacheable = 0;
         return NGX_OK;
     }
@@ -4312,7 +4312,9 @@

     expires = ngx_parse_http_time(h->value.data, h->value.len);

-    if (expires == NGX_ERROR || expires < ngx_time()) {
+    if (expires == NGX_ERROR
+        || (expires < ngx_time() && !r->upstream->conf->cache_revalidate))
+    {
         u->cacheable = 0;
         return NGX_OK;
     }
@@ -4356,7 +4358,9 @@

         switch (n) {
         case 0:
-            u->cacheable = 0;
+            if (!r->upstream->conf->cache_revalidate) {
+                u->cacheable = 0;
+            }
             /* fall through */

         case NGX_ERROR:
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx-devel/attachments/20151214/7e10684e/attachment.html>


More information about the nginx-devel mailing list