# Subrequest failover

**URL:** <https://www.ruby-forum.com/t/subrequest-failover/192362>\
**Category:** NGINX\
**Created:** [July 29, 2010, 2:37am UTC](https://www.ruby-forum.com/t/subrequest-failover/192362 "2010-07-29T02:37:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Lyons](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@James\_Lyons](https://www.ruby-forum.com/u/James_Lyons)\
**Post date:** [July 29, 2010, 2:37am UTC](https://www.ruby-forum.com/t/subrequest-failover/192362/1 "2010-07-29T02:37:11Z")

</div>

I am trying to use nginx as reverse proxy to perform failover from 1  
critical service to another.

When processing a request, it sends a request (http GET) to a backend  
(custom http server) using SubRequest methods. If it is for some  
reason down, and there is no response, i’d like it to fail over to a  
second server. Is there a way to do this in conf settings or do I  
have to make code modifications to accomplish this?

Any help appreciated.

-James-

---

<div class="post-metadata">

**Author:** ![James\_Lyons](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@James\_Lyons](https://www.ruby-forum.com/u/James_Lyons)\
**Post date:** [July 29, 2010, 3:41am UTC](https://www.ruby-forum.com/t/subrequest-failover/192362/2 "2010-07-29T03:41:34Z")

</div>

29.07.2010, 04:36, “James Lyons” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I am trying to use nginx as reverse proxy to perform failover from 1  
> critical service to another.
> 
> When processing a request, it sends a request (http GET) to a backend  
> (custom http server) using SubRequest methods. If it is for some  
> reason down, and there is no response, i’d like it to fail over to a  
> second server. Is there a way to do this in conf settings or do I  
> have to make code modifications to accomplish this?
> 
> Any help appreciated.

upstream failover {  
server 1.1.1.1;  
server 2.2.2.2 backup;  
}

## server { […] location / { proxy\_pass [http://failover](http://failover); } […] }

br, Denis F. Latypoff.

---

<div class="post-metadata">

**Author:** ![James\_Lyons](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@James\_Lyons](https://www.ruby-forum.com/u/James_Lyons)\
**Post date:** [July 30, 2010, 9:58pm UTC](https://www.ruby-forum.com/t/subrequest-failover/192362/3 "2010-07-30T21:58:45Z")

</div>

Hello!

On Wed, Jul 28, 2010 at 05:36:11PM -0700, James Lyons wrote:

> I am trying to use nginx as reverse proxy to perform failover from 1  
> critical service to another.
> 
> When processing a request, it sends a request (http GET) to a backend  
> (custom http server) using SubRequest methods. If it is for some  
> reason down, and there is no response, i’d like it to fail over to a  
> second server. Is there a way to do this in conf settings or do I  
> have to make code modifications to accomplish this?

Subrequests in nginx aren’t really different from ordinary  
requests, they are handled in the same way - with location  
matching and so on. Setting failover correctly in config (either  
with proxy\_next\_upstream or via error\_page) would do the trick for  
both normal requests and subrequests.

Maxim D.

---

<div class="post-metadata">

**Author:** ![James\_Lyons](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@James\_Lyons](https://www.ruby-forum.com/u/James_Lyons)\
**Post date:** [September 15, 2010, 3:04am UTC](https://www.ruby-forum.com/t/subrequest-failover/192362/4 "2010-09-15T03:04:21Z")

</div>

so lets say I have a config that looks like:  
upstream somename {  
server www.somename1.net:80;  
server www.somename2.net:80;  
server www.somename3.net:80;  
}

## location /data/ { … proxy\_pass [http://somename/data/](http://somename/data/); }

## These names are processed as round robin I assume, but if a connection to one fails – it won’t retry elsewhere. To get that, i need to specify backup like in the example below. But can I similarly specify multiple backups and have those load balanced as well?

upstream somename {  
server www.somename1.net:80;  
server www.somename2.net:80;  
server www.somename3.net:80;  
server www.backupname1.net:80 backup;  
server www.backupname2.net:80 backup;  
server www.backupname3.net:80 backup;  
}

location /data/ {  
…  
proxy\_pass [http://somename/data/](http://somename/data/);  
…  
}

---

<div class="post-metadata">

**Author:** ![James\_Lyons](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@James\_Lyons](https://www.ruby-forum.com/u/James_Lyons)\
**Post date:** [September 15, 2010, 3:57am UTC](https://www.ruby-forum.com/t/subrequest-failover/192362/5 "2010-09-15T03:57:37Z")

</div>

## Thanks so much for help so far – with what nginx version is “backup” introduced? I have: 378 upstream iks { 379 server 127.0.0.1:5397; 380 server www.testfailover.net:5397 backup; 381 }

on startup I see:  
2010/09/14 18:46:46 [emerg] 17835#0: invalid parameter “backup” in  
/box/etc/nginx/nginx.conf.iksdev:380

I know we are using 0.6.39 – which is old, so i suspect this is the  
problem – but my job has been reluctant to upgrade so far. Is there  
a workaround?

---

<div class="post-metadata">

**Author:** ![James\_Lyons](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@James\_Lyons](https://www.ruby-forum.com/u/James_Lyons)\
**Post date:** [September 15, 2010, 3:21am UTC](https://www.ruby-forum.com/t/subrequest-failover/192362/6 "2010-09-15T03:21:20Z")

</div>

Hello!

On Tue, Sep 14, 2010 at 06:02:20PM -0700, James Lyons wrote:

> ## }
> 
> These names are processed as round robin I assume, but if a connection  
> to one fails – it won’t retry elsewhere.

It will.

> **[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

> To get that, i need to specify backup like in the example below.

Backup servers are only tried if all normal servers were detected  
to be down.

> But can I similarly specify multiple backups and have those load balanced  
> as well?

Yes.

Maxim D.

---

<div class="post-metadata">

**Author:** ![James\_Lyons](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@James\_Lyons](https://www.ruby-forum.com/u/James_Lyons)\
**Post date:** [September 15, 2010, 4:25am UTC](https://www.ruby-forum.com/t/subrequest-failover/192362/7 "2010-09-15T04:25:43Z")

</div>

Hello!

On Tue, Sep 14, 2010 at 06:56:38PM -0700, James Lyons wrote:

> Thanks so much for help so far – with what nginx version is “backup”  
> introduced?

0.6.7+

See here:  
[http://nginx.org/en/CHANGES](http://nginx.org/en/CHANGES)

> I know we are using 0.6.39 – which is old, so i suspect this is the  
> problem – but my job has been reluctant to upgrade so far. Is there  
> a workaround?

Please make sure you define upstream before you try to use it in  
any proxy\_pass.

Maxim D.

p.s. Please do not top-post. Thank you.

---

<div class="post-metadata">

**Author:** ![James\_Lyons](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@James\_Lyons](https://www.ruby-forum.com/u/James_Lyons)\
**Post date:** [September 16, 2010, 11:30am UTC](https://www.ruby-forum.com/t/subrequest-failover/192362/8 "2010-09-16T11:30:02Z")

</div>

I’m also having strange issue with backup statement. Here’s my config

```auto
upstream test {
  server 10.0.0.1;
  server 10.0.0.2 backup;
}

upstream proxy {
  server 94.1.1.1;
# server 95.1.1.1 backup;
}

server {
  listen 80;
  server_name _;

  access_log off;

  location / {
    proxy_pass http://proxy;
# proxy_pass http://test;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    client_max_body_size 100m;
    client_body_buffer_size 128k;
  }
}
```

It shows no errors on nginx -t run but as I uncomment “server 95.1.1.1  
backup;” line it outputs

```auto
[emerg]: invalid parameter "backup" in /etc/nginx/sites-enabled/site:8
configuration file /etc/nginx/nginx.conf test failed
```

No errors in “upstream test” section. Why is that? Tried nginx 0.7.67-3  
and 0.6.32-3 from debian packages.

Posted at Nginx Forum:

> **[Re: subrequest failover](https://forum.nginx.org/read.php?2,114123,130885#msg-130885)**
>
> I am trying to use nginx as reverse proxy to perform failover from 1 critical service to another. When processing a request, it sends a request (http GET) to a backend (custom http server) using SubRequest methods. If it is for some reason down, and...

---

<div class="post-metadata">

**Author:** ![James\_Lyons](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@James\_Lyons](https://www.ruby-forum.com/u/James_Lyons)\
**Post date:** [September 16, 2010, 3:41pm UTC](https://www.ruby-forum.com/t/subrequest-failover/192362/9 "2010-09-16T15:41:11Z")

</div>

Hello!

On Thu, Sep 16, 2010 at 05:29:21AM -0400, reaper wrote:

> }  
> proxy\_set\_header Host $host;
> 
> ```auto
> [emerg]: invalid parameter "backup" in /etc/nginx/sites-enabled/site:8
> configuration file /etc/nginx/nginx.conf test failed
> ```
> 
> No errors in “upstream test” section. Why is that? Tried nginx 0.7.67-3  
> and 0.6.32-3 from debian packages.

Most likely you have upstream “proxy” already defined somewhere  
before your “upstream proxy” block, e.g. implicitly via

```
proxy_pass http://proxy;

```

or something like this.

Maxim D.
