[nginx] Upstream: fixed cache revalidation with SSI.

Maxim Dounin mdounin at mdounin.ru
Wed Jun 25 22:40:14 UTC 2014


details:   http://hg.nginx.org/nginx/rev/d0ce06cb9be1
branches:  
changeset: 5732:d0ce06cb9be1
user:      Maxim Dounin <mdounin at mdounin.ru>
date:      Thu Jun 26 02:20:09 2014 +0400
description:
Upstream: fixed cache revalidation with SSI.

Previous code in ngx_http_upstream_send_response() used last modified time
from r->headers_out.last_modified_time after the header filter chain was
already called.  At this point, last_modified_time may be already cleared,
e.g., with SSI, resulting in incorrect last modified time stored in a
cache file.  Fix is to introduce u->headers_in.last_modified_time instead.

diffstat:

 src/http/ngx_http_upstream.c |  34 +++++++++++++++++++++++++++++-----
 src/http/ngx_http_upstream.h |   5 +++--
 2 files changed, 32 insertions(+), 7 deletions(-)

diffs (90 lines):

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
@@ -87,6 +87,8 @@ static ngx_int_t ngx_http_upstream_proce
     ngx_table_elt_t *h, ngx_uint_t offset);
 static ngx_int_t ngx_http_upstream_process_content_length(ngx_http_request_t *r,
     ngx_table_elt_t *h, ngx_uint_t offset);
+static ngx_int_t ngx_http_upstream_process_last_modified(ngx_http_request_t *r,
+    ngx_table_elt_t *h, ngx_uint_t offset);
 static ngx_int_t ngx_http_upstream_process_set_cookie(ngx_http_request_t *r,
     ngx_table_elt_t *h, ngx_uint_t offset);
 static ngx_int_t
@@ -184,8 +186,7 @@ ngx_http_upstream_header_t  ngx_http_ups
                  offsetof(ngx_http_headers_out_t, date), 0 },
 
     { ngx_string("Last-Modified"),
-                 ngx_http_upstream_process_header_line,
-                 offsetof(ngx_http_upstream_headers_in_t, last_modified),
+                 ngx_http_upstream_process_last_modified, 0,
                  ngx_http_upstream_copy_last_modified, 0, 0 },
 
     { ngx_string("ETag"),
@@ -2491,7 +2492,7 @@ ngx_http_upstream_send_response(ngx_http
         }
 
         if (valid) {
-            r->cache->last_modified = r->headers_out.last_modified_time;
+            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);
 
@@ -3728,6 +3729,29 @@ ngx_http_upstream_process_content_length
 
 
 static ngx_int_t
+ngx_http_upstream_process_last_modified(ngx_http_request_t *r,
+    ngx_table_elt_t *h, ngx_uint_t offset)
+{
+    ngx_http_upstream_t  *u;
+
+    u = r->upstream;
+
+    u->headers_in.last_modified = h;
+
+#if (NGX_HTTP_CACHE)
+
+    if (u->cacheable) {
+        u->headers_in.last_modified_time = ngx_http_parse_time(h->value.data,
+                                                               h->value.len);
+    }
+
+#endif
+
+    return NGX_OK;
+}
+
+
+static ngx_int_t
 ngx_http_upstream_process_set_cookie(ngx_http_request_t *r, ngx_table_elt_t *h,
     ngx_uint_t offset)
 {
@@ -4183,8 +4207,8 @@ ngx_http_upstream_copy_last_modified(ngx
 #if (NGX_HTTP_CACHE)
 
     if (r->upstream->cacheable) {
-        r->headers_out.last_modified_time = ngx_http_parse_time(h->value.data,
-                                                                h->value.len);
+        r->headers_out.last_modified_time =
+                                    r->upstream->headers_in.last_modified_time;
     }
 
 #endif
diff --git a/src/http/ngx_http_upstream.h b/src/http/ngx_http_upstream.h
--- a/src/http/ngx_http_upstream.h
+++ b/src/http/ngx_http_upstream.h
@@ -246,11 +246,12 @@ typedef struct {
     ngx_table_elt_t                 *content_encoding;
 #endif
 
-    off_t                            content_length_n;
-
     ngx_array_t                      cache_control;
     ngx_array_t                      cookies;
 
+    off_t                            content_length_n;
+    time_t                           last_modified_time;
+
     unsigned                         connection_close:1;
     unsigned                         chunked:1;
 } ngx_http_upstream_headers_in_t;



More information about the nginx-devel mailing list