I'm trying to set up caching for an upstream server (which I do not manage). Most files can be cached (and don't have Cache-Control
set), those work fine.
However, some locations on the server are directory listings (and have Cache-Control: no-store
). I'd like to cache those only if the server is not reachable. Unfortunately, I either end up in one of the following:
I tried to modify the headers to stale-if-error
, but that didn't seem to help either.
map $http_cache_control $http_updated_cache_control {
no-store stale-if-error;
}
server {
...
location /somewhere {
sendfile on;
sendfile_max_chunk 10m;
tcp_nopush on;
proxy_cache keyzone;
# allow using stale requests in case of errors or when updating a file
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
proxy_cache_revalidate on;
proxy_cache_background_update on;
# add header to indicate if caching works
add_header X-Cache-Status $upstream_cache_status;
proxy_cache_lock on;
proxy_read_timeout 900;
proxy_pass_header Server;
proxy_ignore_headers Set-Cookie;
# allow caching of non-cacheable entries only when the server is erroring
proxy_hide_header Cache-Control;
add_header Cache-Control $http_updated_cache_control;
# don't ignore the cache control header: some items (like directory listings) are marked as "don't cache")
#proxy_ignore_headers Cache-Control;
}
}
How can I cache entries with Cache-Control: no-store
, but only use the cached entries if the upstream server is down?
I see two possibilities:
Expires
despite the Cache-Control: no-store
then after you modify headers for NGINX they become Expires: ... Cache-Control: stale-if-error
and it waits
at least as long as the cache is valid
proxy_cache_valid
probably have the same effectSo you need to
proxy_cache_valid
for location /somewhere
Expires
if it's presentmax-age=0
to Cache-Control
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments