Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display the age of an nginx cached file in headers

I've set up a caching server for a site through nginx 1.6.3 on CentOS 7, and it's configured to add http headers to served files to show if said files came from the caching server (HIT, MISS, or BYPASS) like so:

add_header X-Cached $upstream_cache_status;

However, i'd like to see if there's a way to add a header to display the age of the cached file, as my solution has proxy_cache_valid 200 60m; set, and i'd like to check that it's respecting that setting.

So what i'm looking for would be something like:

add_header Cache-Age $upstream_cache_age;

I'm unable to find anything of the sort though, can you help?

Thanks

like image 940
Shiver McTimbers Avatar asked Jun 28 '16 09:06

Shiver McTimbers


People also ask

How can I tell how old my cache is?

Chrome: Navigate to chrome://cache . Note that there's a key for each cache entry (requested URL). Associated with the key, you will find the whole response details (status codes, headers and content). With those details, the browser is able to determine the age of a requested resource and whether it's expired or not.

How configure Nginx Cache-Control header?

If you want to enable Cache-Control for all files, add a add_header line without the enclosing location block, as what the location block does is specify specific filetypes you are targeting with your directives (ico,pdf,flv etc.).

How do I see Nginx cache?

1) Adding cache status header You could also check your header by using Developer Tools in your browser (Firefox and Chrome can open dev tools with F12 ). The cache status could be one of the following: “ MISS ”, “ BYPASS ”, “ EXPIRED ”, “ STALE ”, “ UPDATING ”, “ REVALIDATED ”, or “ HIT ”.

How long will Nginx cache CUR files?

The default value is 10 minutes ( 10m ). Inactive content differs from expired content. NGINX does not automatically delete content that has expired as defined by a cache control header ( Cache-Control:max-age=120 for example).


1 Answers

The nginx documentation is quite exhaustive — there's no variable with the direct relative age of the cached file.

The best way would be to use the $upstream_http_ variable class to get the absolute age of the resource by picking up its Date header through $upsteam_http_date.

add_header X-Cache-Date $upstream_http_date;

For the semantic meaning of the Date header field in HTTP/1.1, refer to rfc7231#section-7.1.1.2, which describes it as the time of the HTTP response generation, so, basically, this should accomplish exactly what you want (especially if the backend runs with the same timecounter).

like image 144
cnst Avatar answered Oct 20 '22 01:10

cnst