We want to set up an NGINX server that will proxy all requests it receives to an upstream server, but when the upstream server fails/becomes unavailable it should fall back to a local cache of the most recently received files. What's the best way to set this up?
The best way I thought of doing it would be setting the 5xx error documents to something like
error_document 500 502 503 504 =200 /cache/;
location /cache/ {
#Send cached files
}
But I'm not sure how to effectively* get NGINX to cache all files while still proxying to the upstream and then how to pull the files back from the cache via a location.
*Without A) filling up the disk very quickly with multiple versions of the same cached file and B) not slowing down requests too much
proxy_cache
directive, used withproxy_cache_use_stale
(which will let you use the cache when the upstream server(s) are down). (As for the all part:proxy_ignore_headers
(to ignore headers that would otherwise prevent caching).