Welcome! Log In Create A New Profile

Advanced

[PATCH] Use the upstream Age header to adjust our cache duration

Florent Le Coz
November 15, 2013 11:44AM
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
# HG changeset patch
# User Florent Le Coz <florent.lecoz@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;

_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

[PATCH] Use the upstream Age header to adjust our cache duration

Florent Le Coz 742 November 15, 2013 11:44AM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 110
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready