nginx use proxy cache if backend is down

Solution 1:

Seems a duplicate of this:

https://stackoverflow.com/questions/16756271/how-to-configure-nginx-to-serve-cached-content-only-when-backend-is-down-5xx-re

In short, use proxy_cache_use_stale

As an update, i tested this and it works fine. I did the test in my workstation where i have (for completeness):

Fedora 23 nginx 1.8.1 configured as ssl terminator + cache + reverse proxy Apache 2.4.18 configured to listen at port 80

With apache acting as upstream, serving just a static file i did this test:

  1. Apache up, nginx up, pointing the browser to the reverse proxied URL served by nginx i see the proxied content from Apache. At this point nginx keeps this on cache.
  2. Stopped apache
  3. connecting to nginx i see the cached file as served before by Apache.

The nginx config i used is (only the interesting parts):

nginx.conf :

http {
[...]
location
    proxy_cache_path        /var/lib/nginx/tmp/proxy/ levels=1:2 keys_zone=STATIC:10m inactive=24h max_size=1g;
    include /etc/nginx/conf.d/*.conf;
}

/etc/nginx/conf.d/local.conf :

upstream localhost {
    server 127.0.0.1:80;
[...]
}


server {
    listen       127.0.0.1:443 ssl;

[...]

    location /be/ {
        proxy_pass              http://localhost;
        proxy_cache             STATIC;
        proxy_cache_valid       200 1d;
        proxy_cache_use_stale   error;
}