# Nginx with proxy\_cache\_use\_stale not returning from cache when connection refused

**URL:** <https://www.ruby-forum.com/t/nginx-with-proxy-cache-use-stale-not-returning-from-cache-when-connection-refused/233401>\
**Category:** NGINX\
**Created:** [December 10, 2014, 5:45pm UTC](https://www.ruby-forum.com/t/nginx-with-proxy-cache-use-stale-not-returning-from-cache-when-connection-refused/233401 "2014-12-10T17:45:45Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [December 10, 2014, 5:45pm UTC](https://www.ruby-forum.com/t/nginx-with-proxy-cache-use-stale-not-returning-from-cache-when-connection-refused/233401/1 "2014-12-10T17:45:45Z")

</div>

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](http://stackoverflow.com/questions/27371285/nginx-proxy-cache-use-stale-not-working-when-connection-refused)

Posted at Nginx Forum:

> **[nginx with proxy\_cache\_use\_stale not returning from cache when connection...](https://forum.nginx.org/read.php?2,255408,255408#msg-255408)**
>
> 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...

---

<div class="post-metadata">

**Author:** ![new299](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e99b99/32.png) [@new299](https://www.ruby-forum.com/u/new299)\
**Post date:** [December 10, 2014, 6:20pm UTC](https://www.ruby-forum.com/t/nginx-with-proxy-cache-use-stale-not-returning-from-cache-when-connection-refused/233401/2 "2014-12-10T18:20:42Z")

</div>

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.

> **[nginx](http://nginx.org/)**

---

<div class="post-metadata">

**Author:** ![new299](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e99b99/32.png) [@new299](https://www.ruby-forum.com/u/new299)\
**Post date:** [December 11, 2014, 4:00am UTC](https://www.ruby-forum.com/t/nginx-with-proxy-cache-use-stale-not-returning-from-cache-when-connection-refused/233401/3 "2014-12-11T04:00:48Z")

</div>

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:

> **[Re: nginx with proxy\_cache\_use\_stale not returning from cache when connection...](https://forum.nginx.org/read.php?2,255408,255414#msg-255414)**
>
> 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...

---

<div class="post-metadata">

**Author:** ![new299](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e99b99/32.png) [@new299](https://www.ruby-forum.com/u/new299)\
**Post date:** [December 11, 2014, 3:42pm UTC](https://www.ruby-forum.com/t/nginx-with-proxy-cache-use-stale-not-returning-from-cache-when-connection-refused/233401/4 "2014-12-11T15:42:34Z")

</div>

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](http://nginx.org/r/$upstream_cache_status).

–  
Maxim D.

> **[nginx](http://nginx.org/)**

---

<div class="post-metadata">

**Author:** ![new299](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e99b99/32.png) [@new299](https://www.ruby-forum.com/u/new299)\
**Post date:** [December 11, 2014, 9:14am UTC](https://www.ruby-forum.com/t/nginx-with-proxy-cache-use-stale-not-returning-from-cache-when-connection-refused/233401/5 "2014-12-11T09:14:06Z")

</div>

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](http://nginx.org/r/proxy_cache_valid)  
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.

---

<div class="post-metadata">

**Author:** ![new299](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e99b99/32.png) [@new299](https://www.ruby-forum.com/u/new299)\
**Post date:** [December 11, 2014, 4:26pm UTC](https://www.ruby-forum.com/t/nginx-with-proxy-cache-use-stale-not-returning-from-cache-when-connection-refused/233401/6 "2014-12-11T16:26:07Z")

</div>

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:

> **[Re: nginx with proxy\_cache\_use\_stale not returning from cache when connection...](https://forum.nginx.org/read.php?2,255408,255441#msg-255441)**
>
> 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...

---

<div class="post-metadata">

**Author:** ![new299](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e99b99/32.png) [@new299](https://www.ruby-forum.com/u/new299)\
**Post date:** [December 15, 2014, 6:29pm UTC](https://www.ruby-forum.com/t/nginx-with-proxy-cache-use-stale-not-returning-from-cache-when-connection-refused/233401/7 "2014-12-15T18:29:24Z")

</div>

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

Posted at Nginx Forum:

> **[Re: nginx with proxy\_cache\_use\_stale not returning from cache when connection...](https://forum.nginx.org/read.php?2,255408,255502#msg-255502)**
>
> 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...

---

<div class="post-metadata">

**Author:** ![new299](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e99b99/32.png) [@new299](https://www.ruby-forum.com/u/new299)\
**Post date:** [December 11, 2014, 4:15pm UTC](https://www.ruby-forum.com/t/nginx-with-proxy-cache-use-stale-not-returning-from-cache-when-connection-refused/233401/8 "2014-12-11T16:15:05Z")

</div>

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:

> **[Re: nginx with proxy\_cache\_use\_stale not returning from cache when connection...](https://forum.nginx.org/read.php?2,255408,255440#msg-255440)**
>
> 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...

---

<div class="post-metadata">

**Author:** ![new299](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e99b99/32.png) [@new299](https://www.ruby-forum.com/u/new299)\
**Post date:** [December 15, 2014, 7:12pm UTC](https://www.ruby-forum.com/t/nginx-with-proxy-cache-use-stale-not-returning-from-cache-when-connection-refused/233401/9 "2014-12-15T19:12:25Z")

</div>

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.

> **[nginx](http://nginx.org/)**
