# Cache as failover

**URL:** <https://www.ruby-forum.com/t/cache-as-failover/180461>\
**Category:** NGINX\
**Created:** [December 19, 2009, 3:25pm UTC](https://www.ruby-forum.com/t/cache-as-failover/180461 "2009-12-19T15:25:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mfeldheim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@mfeldheim](https://www.ruby-forum.com/u/mfeldheim)\
**Post date:** [December 19, 2009, 3:25pm UTC](https://www.ruby-forum.com/t/cache-as-failover/180461/1 "2009-12-19T15:25:42Z")

</div>

So far I am using Nginx as loadbalancer only. Since we have a huge  
amounts of ( frequently changing ) pages, and a small hitrate per unique  
page, output caching has never been really interesting.

I have a failover app server in my upstream configuration for the case  
one of the active servers fails.  
Now if all app servers would fail - e.g. the database backend has a  
problem etc. app-server failover would not help at all, in some cases I  
would get 503 Bad Gateway Error… not nice.

To prevent that I’d like to build a static cache with nginx while  
everything works fine, and deliver those cached sites when all  
appservers fail.

Is that possible?

Posted at Nginx Forum:

> **[Cache as failover](https://forum.nginx.org/read.php?2,31268,31268#msg-31268)**
>
> So far I am using Nginx as loadbalancer only. Since we have a huge amounts of ( frequently changing ) pages, and a small hitrate per unique page, output caching has never been really interesting. I have a failover app server in my upstream...

---

<div class="post-metadata">

**Author:** ![mfeldheim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@mfeldheim](https://www.ruby-forum.com/u/mfeldheim)\
**Post date:** [December 21, 2009, 12:26pm UTC](https://www.ruby-forum.com/t/cache-as-failover/180461/2 "2009-12-21T12:26:23Z")

</div>

Any ideas? Could proxy\_store (  
[Module ngx\_http\_proxy\_module](http://wiki.nginx.org/NginxHttpProxyModule#proxy_store) ) help me in that  
case?

Posted at Nginx Forum:

> **[Re: Cache as failover](https://forum.nginx.org/read.php?2,31268,32363#msg-32363)**
>
> So far I am using Nginx as loadbalancer only. Since we have a huge amounts of ( frequently changing ) pages, and a small hitrate per unique page, output caching has never been really interesting. I have a failover app server in my upstream...

---

<div class="post-metadata">

**Author:** ![mfeldheim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@mfeldheim](https://www.ruby-forum.com/u/mfeldheim)\
**Post date:** [December 21, 2009, 12:43pm UTC](https://www.ruby-forum.com/t/cache-as-failover/180461/3 "2009-12-21T12:43:22Z")

</div>

proxy\_ignore\_headers “Cache-Control” “Expires”;  
proxy\_hide\_header ‘Set-Cookie’;  
proxy\_cache\_key  
“$request\_method|$http\_if\_modified\_since|$http\_if\_none\_match|$host|$request\_uri”;  
proxy\_cache\_valid 200 302 301 304 1m;  
proxy\_cache\_valid any 0m;  
proxy\_cache\_use\_stale error timeout invalid\_header http\_500  
http\_502 http\_503 http\_504;  
proxy\_cache\_path cache/ levels=2:2 keys\_zone=cache:256m  
inactive=7d  
max\_size=65536m;  
server {  
server\_name …;  
location / {  
proxy\_pass http://…;  
proxy\_cache cache;  
}  
}

proxy\_cache\_use\_stale: cached content will return instead of proxying in  
case of 502/503/504 errors.

---

<div class="post-metadata">

**Author:** ![mfeldheim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@mfeldheim](https://www.ruby-forum.com/u/mfeldheim)\
**Post date:** [December 21, 2009, 7:17pm UTC](https://www.ruby-forum.com/t/cache-as-failover/180461/4 "2009-12-21T19:17:17Z")

</div>

Thank you, this is working excellent…  
I’ve set the proxy\_cache\_valid to 10m to soften the I/O overhead… still

- would be cool if I could set  
the cache TTL to a way higher value and bypass the caching completely  
while the backend servers return valid codes.

When backends fail and I have to deliver from cache, additionally I’d  
like to send a header “304 Not modified” ( does that make sense, since  
it is true…!? )

For those who might have a similar scenario…

Content backend servers fail, backup servers fail =\> deliver cache built  
on the Nginx proxy  
search backend fails =\> no backup =\> deliver “503 Service Unavailable”  
maintainance site

upstream contentBackend {  
server 192.168.50.101 weight=2 max\_fails=1 fail\_timeout=30s;  
server 192.168.50.102 weight=2 max\_fails=1 fail\_timeout=30s;  
server 192.168.50.103 weight=2 max\_fails=1 fail\_timeout=30s;  
server 192.168.50.104 weight=2 max\_fails=1 fail\_timeout=30s;  
server 192.168.50.105 weight=2 max\_fails=1 fail\_timeout=30s;  
server 192.168.50.106 weight=1 max\_fails=1 fail\_timeout=30s;  
server 192.168.50.107 weight=1 max\_fails=1 fail\_timeout=30s;  
server 192.168.50.108 weight=1 max\_fails=1 fail\_timeout=30s;  
server 192.168.50.109 backup;  
server 192.168.50.110 backup;  
}

upstream searchBackend {  
server 192.168.50.106 weight=1 max\_fails=1 fail\_timeout=30s;  
server 192.168.50.107 weight=1 max\_fails=1 fail\_timeout=30s;  
server 192.168.50.108 weight=1 max\_fails=1 fail\_timeout=30s;  
}

proxy\_ignore\_headers “Cache-Control” “Expires”;  
proxy\_hide\_header “Set-Cookie”;  
proxy\_cache\_key  
“$request\_method|$http\_if\_modified\_since|$http\_if\_none\_match|$host|$request\_uri”;  
proxy\_cache\_valid 200 302 301 304 1s;  
proxy\_cache\_valid any 0m;  
proxy\_cache\_use\_stale error timeout invalid\_header http\_500 http\_502  
http\_503 http\_504;  
proxy\_cache\_path /tmp/nginxCache levels=2:2 keys\_zone=cache:256m  
inactive=7d max\_size=65536m;

server {

```
    server_name _;

    location / {

            proxy_pass http://contentBackend;
            proxy_cache cache;
    }

    # no sense to deliver cached search masks, search wont work 

```

without a backend  
location /search/ {

```
            proxy_pass http://searchBackend;
            error_page 500 502 503 504 =510 /510.html;
    }

    location /510.html {
            root /srv/www/nginx/htdocs;
    }

```

}

Posted at Nginx Forum:

> **[Re: Cache as failover](https://forum.nginx.org/read.php?2,31268,32536#msg-32536)**
>
> So far I am using Nginx as loadbalancer only. Since we have a huge amounts of ( frequently changing ) pages, and a small hitrate per unique page, output caching has never been really interesting. I have a failover app server in my upstream...
