[PATCH] Use the upstream Age header to adjust our cache duration
Florent Le Coz
florent.lecoz at smartjog.com
Fri Nov 15 16:41:46 UTC 2013
Hello,
Here's the second part of a revised patch, trying to address the issue
of the Age headers (in upstream’s responses) being ignored by nginx.
It relies on the sec_valid_prio value (see previous patch) to determine
if the cache duration as been set by a Cache-Control header, and if
that’s the case it subtracts this from valid_sec.
If the result would be an age in the past, the response is
non-cacheable. If the result is "now", the response is also
non-cacheable, as it is already done in the case of a max-age=0 in
Cache-Control.
If age as an invalid value (negative), it is ignored.
Note: I use
+ { ngx_string("Age"),
+ ngx_http_upstream_process_header_line,
+ offsetof(ngx_http_upstream_headers_in_t, age),
+ ngx_http_upstream_copy_header_line, 0, 0 },
+
to copy this header in a special pointer, to access it easily in
process_headers(), where the valid_sec modification based on age is done.
Is this the correct way to do? I could of course remove this
ngx_http_upstream_headers_in_t.age variable and look for a Age header in
the headers list when I need it, but I think this would be suboptimal.
Please correct me if I’m wrong.
Regards,
--
Florent Le Coz
Smartjog
-------------- next part --------------
# HG changeset patch
# User Florent Le Coz <florent.lecoz at smartjog.com>
# Date 1384532950 0
# Node ID 721fec3e72d3b273146df60dcb5c45fdc2dbf97a
# Parent c26efc188ac84a8b8f2bb4478efb795635a3498d
Use the upstream Age header when calculating our caching duration
diff -r c26efc188ac8 -r 721fec3e72d3 src/http/ngx_http_upstream.c
--- a/src/http/ngx_http_upstream.c Fri Nov 15 15:05:55 2013 +0000
+++ b/src/http/ngx_http_upstream.c Fri Nov 15 16:29:10 2013 +0000
@@ -229,6 +229,11 @@ ngx_http_upstream_header_t ngx_http_ups
ngx_http_upstream_copy_header_line,
offsetof(ngx_http_headers_out_t, expires), 1 },
+ { ngx_string("Age"),
+ ngx_http_upstream_process_header_line,
+ offsetof(ngx_http_upstream_headers_in_t, age),
+ ngx_http_upstream_copy_header_line, 0, 0 },
+
{ ngx_string("Accept-Ranges"),
ngx_http_upstream_process_header_line,
offsetof(ngx_http_upstream_headers_in_t, accept_ranges),
@@ -1939,6 +1944,8 @@ ngx_http_upstream_process_headers(ngx_ht
{
ngx_str_t *uri, args;
ngx_uint_t i, flags;
+ time_t now;
+ ngx_int_t age;
ngx_list_part_t *part;
ngx_table_elt_t *h;
ngx_http_upstream_header_t *hh;
@@ -1946,6 +1953,20 @@ ngx_http_upstream_process_headers(ngx_ht
umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module);
+ if (u->cacheable && u->headers_in.age &&
+ u->headers_in.valid_sec_prio == NGX_HTTP_UPSTREAM_CACHE_CONTROL_H_P)
+ {
+ age = ngx_atoi(u->headers_in.age->value.data,
+ u->headers_in.age->value.len);
+ if (age >= 0 && age != NGX_ERROR)
+ {
+ now = ngx_time();
+ if (r->cache->valid_sec - age > now)
+ r->cache->valid_sec -= age;
+ else
+ u->cacheable = 0;
+ }
+ }
if (u->headers_in.x_accel_redirect
&& !(u->conf->ignore_headers & NGX_HTTP_UPSTREAM_IGN_XA_REDIRECT))
{
diff -r c26efc188ac8 -r 721fec3e72d3 src/http/ngx_http_upstream.h
--- a/src/http/ngx_http_upstream.h Fri Nov 15 15:05:55 2013 +0000
+++ b/src/http/ngx_http_upstream.h Fri Nov 15 16:29:10 2013 +0000
@@ -225,6 +225,7 @@ typedef struct {
ngx_table_elt_t *connection;
ngx_table_elt_t *expires;
+ ngx_table_elt_t *age;
ngx_table_elt_t *etag;
ngx_table_elt_t *x_accel_expires;
ngx_table_elt_t *x_accel_redirect;
More information about the nginx-devel
mailing list