# Primary and fallback to backup -- try\_files, proxy\_pass, upstream?

**URL:** <https://www.ruby-forum.com/t/primary-and-fallback-to-backup-try-files-proxy-pass-upstream/183446>\
**Category:** NGINX\
**Created:** [February 14, 2010, 1:44pm UTC](https://www.ruby-forum.com/t/primary-and-fallback-to-backup-try-files-proxy-pass-upstream/183446 "2010-02-14T13:44:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Denis\_Arh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/82dd89/32.png) [@Denis\_Arh](https://www.ruby-forum.com/u/Denis_Arh)\
**Post date:** [February 14, 2010, 1:44pm UTC](https://www.ruby-forum.com/t/primary-and-fallback-to-backup-try-files-proxy-pass-upstream/183446/1 "2010-02-14T13:44:30Z")

</div>

What I would like to have is nginx first to try primary service and then  
to  
fallback to the backup if primary is not available.

What I’ve tried:

1. try\_files:

This results in nginx only invoking @backup.  
If I switch try\_files params (try\_files @backup @primary) then @primary  
is  
invoked but in case it’s not running I get 502 error (error log: 9228#0:  
\*1850 no live upstreams while connecting to upstream).

1. upstream

upstream backend {  
server localhost:81;  
server unix:/tmp/php-fpm.sock;  
}

location /api {  
proxy\_pass [http://backend](http://backend);  
}

The first part of this solution works – requests are first passed to  
localhost:81 and handled there without any problems and if this one is  
down  
requests go to the second server in the upstream.  
What happens there is another 502 error:  
error-log: \*1840 recv() failed (104: Connection reset by peer) while  
reading  
response header from upstream, client: …, server: …, request:  
“GET  
/api HTTP/1.1”, upstream: “[http://unix](http://unix):/tmp/php-fpm.sock:/api”

How to configure fastcgi backend propery to handle this properly?

Is there any way to make this work?  
If both are possible, which one is better?

---

<div class="post-metadata">

**Author:** ![Denis\_Arh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/82dd89/32.png) [@Denis\_Arh](https://www.ruby-forum.com/u/Denis_Arh)\
**Post date:** [February 14, 2010, 1:56pm UTC](https://www.ruby-forum.com/t/primary-and-fallback-to-backup-try-files-proxy-pass-upstream/183446/2 "2010-02-14T13:56:12Z")

</div>

Hello!

On Sun, Feb 14, 2010 at 01:43:55PM +0100, Denis Arh wrote:

> What I would like to have is nginx first to try primary service and then to  
> fallback to the backup if primary is not available.
> 
> What I’ve tried:
> 
> 1. try\_files:

This won’t work as try\_files tris _files_ (surprise!) and uses  
fallback uri if no one was found. See syntax details here:

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

[…]

> The first part of this solution works – requests are first passed to  
> localhost:81 and handled there without any problems and if this one is down  
> requests go to the second server in the upstream.

Not really. Requests are randomly balanced between two servers  
you specified. Once one of them down it uses another one to  
process requests.

> What happens there is another 502 error:  
> error-log: \*1840 recv() failed (104: Connection reset by peer) while reading  
> response header from upstream, client: …, server: …, request: “GET  
> /api HTTP/1.1”, upstream: “[http://unix](http://unix):/tmp/php-fpm.sock:/api”
> 
> How to configure fastcgi backend propery to handle this properly?

You use proxy\_pass, and all backends should be http. Using  
fastcgi backend in one pool with http ones isn’t an option.

> Is there any way to make this work?  
> If both are possible, which one is better?

```
location /api {
    error_page 502 504 = @fallback;
    proxy_pass http://primary-backend;
}

location @fallback {
    fastcgi_pass ...
}

```

Maxim D.

---

<div class="post-metadata">

**Author:** ![Denis\_Arh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/82dd89/32.png) [@Denis\_Arh](https://www.ruby-forum.com/u/Denis_Arh)\
**Post date:** [February 14, 2010, 2:12pm UTC](https://www.ruby-forum.com/t/primary-and-fallback-to-backup-try-files-proxy-pass-upstream/183446/3 "2010-02-14T14:12:00Z")

</div>

I will not even try to explain why I’ve tried the “hard” way first 🙂

Your solution It worked perfectly.

I’ve added “error\_log /dev/null crit;” to “location /api {” so it does  
not  
fill the log files with connection refused"

Thank you.
