1

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

2
  • 2
    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, 2012 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, 2012 at 4:37

1 Answer 1

0

If the files do not change on remote server, proxy_store may work for you. e.g.

    upstream big {
            server  serverfault.com:80;
    }

    root /somewhere;
    location / {
            try_files $uri @big;
    }
    location @big {
            proxy_pass http://big;
            proxy_set_header Host serverfault.com:80;
            proxy_store on;
    }

You must log in to answer this question.