]> git.kaiwu.me - nginx.git/commitdiff
Cache: send conditional requests only for cached 200/206 responses.
authorPiotr Sikora <piotr@cloudflare.com>
Thu, 27 Nov 2014 02:35:37 +0000 (18:35 -0800)
committerPiotr Sikora <piotr@cloudflare.com>
Thu, 27 Nov 2014 02:35:37 +0000 (18:35 -0800)
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@cloudflare.com>
src/http/ngx_http_file_cache.c
src/http/ngx_http_upstream.c

index c3f4f05d2d41a467958dcd5ab334cd27b6610650..ee4bfc06dc78a005a8009392098ba2400ff3d2a1 100644 (file)
@@ -177,6 +177,8 @@ ngx_http_file_cache_new(ngx_http_request_t *r)
     c->file.log = r->connection->log;
     c->file.fd = NGX_INVALID_FILE;
 
+    c->last_modified = -1;
+
     return NGX_OK;
 }
 
index 4c768b1bcab0be411bd4c20a6d0765684cc1b492..337ec3eb8dc3021dac379d2fc501243fe62b1ff7 100644 (file)
@@ -2565,12 +2565,17 @@ ngx_http_upstream_send_response(ngx_http_request_t *r, ngx_http_upstream_t *u)
         }
 
         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);