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

link|improve this question

A good starting point may be the nginx proxy_cache directive, used with proxy_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). – cyberx86 Feb 7 at 15:16
A much more robust solution would be to put a purpose-built caching proxy like Varnish ahead of nginx and the let the caching happen transparently. – tmehlinger Mar 6 at 4:37
feedback

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
or
required, but never shown

Browse other questions tagged or ask your own question.