Nginx with proxy_cache_use_stale not returning from cache when connection refused

Hi,

I’m using nginx as a reverse proxy, but I can’t get nginx to serve
requests
from its cache when the upstream server is refusing connections. I
understood that “proxy_cache_use_stale error” should allow me to do
this,
but it doesn’t seem to work for me. Have I perhaps misunderstood
something?

My complete nginx.conf looks like this:

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
worker_connections 768;
# multi_accept on;
}

http {
upstream localsvr {
server localhost:8080;
}

proxy_cache_path  /data/nginx/cache levels=1:2 keys_zone=one:8m

max_size=5000m inactive=300m;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
server {
keepalive_timeout 65;
types_hash_max_size 2048;
proxy_buffering on;

        default_type application/octet-stream;

        gzip on;
        gzip_disable "msie6";
        listen 80;
        proxy_cache one;
        proxy_cache_min_uses 100;
        proxy_set_header Host $host;
        location / {
                proxy_pass http://localsvr;
                proxy_cache_use_stale error;
                proxy_next_upstream error;
                proxy_redirect off;
        }
}

}


I’ve previously also posted this on stackoverflow, but didn’t get any
feedback there:

http://stackoverflow.com/questions/27371285/nginx-proxy-cache-use-stale-not-working-when-connection-refused

Posted at Nginx Forum:

Hello!

On Wed, Dec 10, 2014 at 11:45:26AM -0500, new299 wrote:

I’m using nginx as a reverse proxy, but I can’t get nginx to serve requests
from its cache when the upstream server is refusing connections. I
understood that “proxy_cache_use_stale error” should allow me to do this,
but it doesn’t seem to work for me. Have I perhaps misunderstood something?

You may want to clariy what “doesn’t seem to work for me” means.
What makes you think that it doesn’t work?


Maxim D.
http://nginx.org/

When the upstream goes away nginx gives the error “502 Bad Gateway
nginx/1.4.6 (Ubuntu)”. The log contains:

" [error] 2624#0: *48941 connect() failed (111: Connection refused)
while
connecting to upstream,"

Rather than serving it from cache as I would expect. It should be cached
as
the page was previous returned successfully.

Posted at Nginx Forum:

Hello!

On Wed, Dec 10, 2014 at 10:00:25PM -0500, new299 wrote:

When the upstream goes away nginx gives the error “502 Bad Gateway
nginx/1.4.6 (Ubuntu)”. The log contains:

" [error] 2624#0: *48941 connect() failed (111: Connection refused) while
connecting to upstream,"

Rather than serving it from cache as I would expect. It should be cached as
the page was previous returned successfully.

The problem is in the “it should be cached” statement.

There are lots of cases when pages will not be cached even if
returned - for example, because caching is explicitly disabled by
response headers, or just not enabled and/or disabled with
proxy_cache_min_uses, see Ruslan’s answer.

So in your case the response is likely not cached, and that’s
why “proxy_cache_use_stale” doesn’t work for you. To be sure
you can follow Ruslan’s suggestion to look into the cache
directory, or use the $upstream_cache_status variable, see
Module ngx_http_upstream_module.


Maxim D.
http://nginx.org/

On Wed, Dec 10, 2014 at 11:45:26AM -0500, new299 wrote:

worker_processes 4;
}
default_type application/octet-stream;
proxy_next_upstream error;
proxy_redirect off;
}
}
}

Your config doesn’t have any Module ngx_http_proxy_module
directives. If this is intentional, then your responses should
carry caching information themselves (X-Accel-Expires, Expires,
Cache-Control, Set-Cookie, Vary, see the link above for details)
and otherwise qualify to be cached.

Also, “proxy_cache_min_uses 100” in your config instructs to cache
a response only after it was requested 100 times.

Please first make sure your responses actually get cached by
looking into /data/nginx/cache.

After adding the following:

proxy_cache_valid 200 302 301 10m;

It appears to be working. It’s unclear to me why:

proxy_cache_valid any 10m;

Wasn’t working, for me.

Posted at Nginx Forum:

i have the same problem . how to fix this please help

Posted at Nginx Forum:

Thanks, I’ve tried this. My amended configuration is below. However, I’m
still getting the same error when the upstream goes away. The cache
directory is now being populated correctly however. Any ideas?


user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
worker_connections 768;
# multi_accept on;
}

http {
upstream localsvr {
server localhost:8080;
}

    proxy_cache_path  /data/nginx/cache levels=1:2 keys_zone=one:8m

max_size=5000m inactive=300m;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
server {
keepalive_timeout 65;
types_hash_max_size 2048;
proxy_buffering on;

            default_type application/octet-stream;

            gzip on;
            gzip_disable "msie6";
            listen 80;
            proxy_cache one;
            proxy_cache_min_uses 1;
            proxy_cache_valid 200 302 10m;
            proxy_cache_valid any      10m;
            proxy_set_header Host $host;
            location / {
                    proxy_pass http://localsvr;
                    proxy_cache_use_stale error;
                    proxy_next_upstream error;
                    proxy_redirect off;
            }
    }

}

Posted at Nginx Forum:

Hello!

On Mon, Dec 15, 2014 at 12:28:39PM -0500, jurerickporras wrote:

i have the same problem . how to fix this please help

As already explained in this thread, the “fix” is to make sure the
response is in cache. If it’s there, nginx will return it once
configured to do so.


Maxim D.
http://nginx.org/