[PATCH] Cache: send conditional requests only for cached 200/206 responses

Piotr Sikora piotr at cloudflare.com
Thu Nov 27 02:38:31 UTC 2014


# HG changeset patch
# User Piotr Sikora <piotr at cloudflare.com>
# Date 1417055737 28800
#      Wed Nov 26 18:35:37 2014 -0800
# Node ID ec4837c14647c6745b41f0a8c55fcc5fcb6f336b
# Parent  2c10db908b8c4a9c0532c58830275d5ad84ae686
Cache: send conditional requests only for cached 200/206 responses.

RFC7232 says:

   The 304 (Not Modified) status code indicates that a conditional GET
   or HEAD request has been received and would have resulted in a 200
   (OK) response if it were not for the fact that the condition
   evaluated to false.

which means that there is no reason to send requests with "If-None-Match"
and/or "If-Modified-Since" headers for responses cached with other status
codes.

Also, sending conditional requests for responses cached with other status
codes could result in a strange behavior, e.g. upstream server returning
304 Not Modified for cached 404 Not Found responses, etc.

Signed-off-by: Piotr Sikora <piotr at cloudflare.com>

diff -r 2c10db908b8c -r ec4837c14647 src/http/ngx_http_file_cache.c
--- a/src/http/ngx_http_file_cache.c	Fri Nov 21 22:51:49 2014 +0300
+++ b/src/http/ngx_http_file_cache.c	Wed Nov 26 18:35:37 2014 -0800
@@ -175,6 +175,8 @@ ngx_http_file_cache_new(ngx_http_request
     c->file.log = r->connection->log;
     c->file.fd = NGX_INVALID_FILE;
 
+    c->last_modified = -1;
+
     return NGX_OK;
 }
 
diff -r 2c10db908b8c -r ec4837c14647 src/http/ngx_http_upstream.c
--- a/src/http/ngx_http_upstream.c	Fri Nov 21 22:51:49 2014 +0300
+++ b/src/http/ngx_http_upstream.c	Wed Nov 26 18:35:37 2014 -0800
@@ -2560,12 +2560,17 @@ ngx_http_upstream_send_response(ngx_http
         }
 
         if (valid) {
-            r->cache->last_modified = u->headers_in.last_modified_time;
             r->cache->date = now;
             r->cache->body_start = (u_short) (u->buffer.pos - u->buffer.start);
 
-            if (u->headers_in.etag) {
-                r->cache->etag = u->headers_in.etag->value;
+            if (u->headers_in.status_n == NGX_HTTP_OK
+                || u->headers_in.status_n == NGX_HTTP_PARTIAL_CONTENT)
+            {
+                r->cache->last_modified = u->headers_in.last_modified_time;
+
+                if (u->headers_in.etag) {
+                    r->cache->etag = u->headers_in.etag->value;
+                }
             }
 
             ngx_http_file_cache_set_header(r, u->buffer.start);



More information about the nginx-devel mailing list