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
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.
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.).
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 ”.
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).
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With