# Issue with upstream

**URL:** <https://www.ruby-forum.com/t/issue-with-upstream/199189>\
**Category:** NGINX\
**Created:** [November 16, 2010, 2:17pm UTC](https://www.ruby-forum.com/t/issue-with-upstream/199189 "2010-11-16T14:17:32Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![okkezSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/85f322/32.png) [@okkezSS](https://www.ruby-forum.com/u/okkezSS)\
**Post date:** [November 16, 2010, 2:17pm UTC](https://www.ruby-forum.com/t/issue-with-upstream/199189/1 "2010-11-16T14:17:32Z")

</div>

I’m having some problems configuring upstream servers with localhost  
backup  
(error page)

upstream backend1 {  
server 1.2.3.4;  
server 2.3.4.5;  
server localhost backup;  
}

upstream backend2 {  
server 3.4.5.6;  
server 4.5.6.7;  
}

Works, but:

upstream backend1 {  
server 1.2.3.4;  
server 2.3.4.5;  
server localhost backup;  
}

upstream backend2 {  
server 3.4.5.6;  
server 4.5.6.7;  
server localhost backup;  
}

Causes:

Testing nginx configuration: [emerg]: invalid parameter “backup”

Is this a bug?

Cheers  
Phil.

---

<div class="post-metadata">

**Author:** ![phoyce](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bc8723/32.png) [@phoyce](https://www.ruby-forum.com/u/phoyce)\
**Post date:** [November 16, 2010, 3:29pm UTC](https://www.ruby-forum.com/t/issue-with-upstream/199189/2 "2010-11-16T15:29:07Z")

</div>

Problem solved, strangely enough the order in which my servers are  
loaded  
seams to trigger this error, it’s actually nothing to do with the  
upstream  
settings but forcing one of my server configs to be loaded last resolved  
the  
issue.

On 16 November 2010 13:16, Phil B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> server 3.4.5.6;
> 
> ```
> 
> Is this a bug?
> 
> Cheers  
> Phil.

–  
Phil B.  
Developer  
Techlightenment

3.08 Tea Building | 56 Shoreditch High St | London | E1 6JJ  
t: +44 (0) 8445814444  
m: +44 (0) 7711117117  
e: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[www.techlightenment.com](http://www.techlightenment.com)

_Social Media Developer positions available. See [http://bit.ly/afmRt4](http://bit.ly/afmRt4)  
for  
info_

---

<div class="post-metadata">

**Author:** ![phoyce](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bc8723/32.png) [@phoyce](https://www.ruby-forum.com/u/phoyce)\
**Post date:** [November 16, 2010, 3:50pm UTC](https://www.ruby-forum.com/t/issue-with-upstream/199189/3 "2010-11-16T15:50:27Z")

</div>

Hello!

On Tue, Nov 16, 2010 at 01:16:25PM +0000, Phil B. wrote:

> ```
> server 3.4.5.6;
> 
> ```
> 
> Is this a bug?  
> Most likely you used backend2 somewhere in proxy\_pass before  
> defining upstream backend2. This is not generally supported and  
> and shouldn’t be allowed, but happens to “work” (though with  
> several unexpected side effects) for now.

Attached patch makes sure such configuration like

server {  
location / {  
proxy\_pass backend;  
…  
}  
…  
}

upstream backend {  
…  
}

will generate “upstream “backend” defined too late” error during  
configuration parsing.

Maxim D.

---

<div class="post-metadata">

**Author:** ![phoyce](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bc8723/32.png) [@phoyce](https://www.ruby-forum.com/u/phoyce)\
**Post date:** [November 16, 2010, 7:25pm UTC](https://www.ruby-forum.com/t/issue-with-upstream/199189/4 "2010-11-16T19:25:53Z")

</div>

Hello!

On Tue, Nov 16, 2010 at 03:02:00PM +0000, Phil B. wrote:

> I had actually prefixed upstream and localhost 000 and everything else 100  
> to force them to load in the correct order.  
> On 1 particular domain it would throw that error unless it was loaded last,  
> hence just giving it a prefix 101 solved the issue.  
> Bit strange!

Addition prefixes isn’t going to do anything good as nginx doesn’t sort  
included files (I assume you used something like “include  
/path/\*.conf”) and they are loaded in unspecified order (as  
filesystem returns them).

You have to manually preserve correct order, i.e. use something  
like

include /path/to/generic/includes/_.conf;  
include /path/to/virtual/hosts/_.conf;

Maxim D.

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

---

<div class="post-metadata">

**Author:** ![phoyce](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bc8723/32.png) [@phoyce](https://www.ruby-forum.com/u/phoyce)\
**Post date:** [November 16, 2010, 4:03pm UTC](https://www.ruby-forum.com/t/issue-with-upstream/199189/5 "2010-11-16T16:03:00Z")

</div>

I had actually prefixed upstream and localhost 000 and everything else  
100  
to force them to load in the correct order.  
On 1 particular domain it would throw that error unless it was loaded  
last,  
hence just giving it a prefix 101 solved the issue.  
Bit strange!

On 16 November 2010 14:49, Maxim D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > server 2.3.4.5;
> > 
> > ```
> > 
> > upstream backend1 {
> 
> upstream backend {  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

–  
Phil B.  
Developer  
Techlightenment

3.08 Tea Building | 56 Shoreditch High St | London | E1 6JJ  
t: +44 (0) 8445814444  
m: +44 (0) 7711117117  
e: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[www.techlightenment.com](http://www.techlightenment.com)

_Social Media Developer positions available. See [http://bit.ly/afmRt4](http://bit.ly/afmRt4)  
for  
info_

---

<div class="post-metadata">

**Author:** ![phoyce](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bc8723/32.png) [@phoyce](https://www.ruby-forum.com/u/phoyce)\
**Post date:** [November 16, 2010, 7:39pm UTC](https://www.ruby-forum.com/t/issue-with-upstream/199189/6 "2010-11-16T19:39:03Z")

</div>

On 16 Nov 2010 18h24 WET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Addition prefixes isn’t going to do anything good as nginx doesn’t  
> sort included files (I assume you used something like “include  
> /path/\*.conf”) and they are loaded in unspecified order (as  
> filesystem returns them).
> 
> You have to manually preserve correct order, i.e. use something  
> like
> 
> include /path/to/generic/includes/_.conf;  
> include /path/to/virtual/hosts/_.conf;

Well that’s interesting. I was under the (wrong) impression that in  
fact it operated like in Apache. Where there’s a 000-default vhost  
config file in Debian that is a symlink created when enabling the  
site.

I have a 000-default file that just returns a 444 when the Host header  
doesn’t match any of the server\_name directives. I guess that I should  
be loading that file explicitly before all other.

include /etc/nginx/sites-enabled/000-default  
include /etc/nginx/sites-enabled/[a-z]\*

Right?

Thanks,  
— appa

---

<div class="post-metadata">

**Author:** ![phoyce](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bc8723/32.png) [@phoyce](https://www.ruby-forum.com/u/phoyce)\
**Post date:** [November 16, 2010, 8:02pm UTC](https://www.ruby-forum.com/t/issue-with-upstream/199189/7 "2010-11-16T20:02:52Z")

</div>

Hello!

On Tue, Nov 16, 2010 at 06:32:19PM +0000, António P. P. Almeida wrote:

> > > strange!  
> > > include /path/to/virtual/hosts/_.conf;  
> > > include /etc/nginx/sites-enabled/000-default  
> > > include /etc/nginx/sites-enabled/[a-z]_
> 
> Right?

Alternative solution is to use “listen … default;” (or “listen  
… default\_server;” which is the same) in a server which should  
be default on a given socket.

> **[How nginx processes a request](http://nginx.org/en/docs/http/request_processing.html#how_to_prevent_undefined_server_names)**
>
> nginx first decides which server should process the request. Let’s start with a simple configuration where all three virtual servers listen on port \*:80:

Maxim D.

---

<div class="post-metadata">

**Author:** ![phoyce](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bc8723/32.png) [@phoyce](https://www.ruby-forum.com/u/phoyce)\
**Post date:** [November 16, 2010, 9:15pm UTC](https://www.ruby-forum.com/t/issue-with-upstream/199189/8 "2010-11-16T21:15:45Z")

</div>

If you order them 000, 001, 002 that is the same order as the filesystem  
returns:

phil@proxy:~$ ls -l /etc/nginx/sites-enabled/  
total 0  
lrwxrwxrwx 1 root root 28 2010-11-12 22:56 000-l\*  
lrwxrwxrwx 1 root root 27 2010-11-13 00:19 000-u\*  
lrwxrwxrwx 1 root root 26 2010-11-13 02:06 100-p\*  
lrwxrwxrwx 1 root root 29 2010-11-13 00:20 101-d\*  
lrwxrwxrwx 1 root root 33 2010-11-15 20:30 200-p\*  
lrwxrwxrwx 1 root root 46 2010-11-15 20:30 201-s\*  
lrwxrwxrwx 1 root root 50 2010-11-16 14:11 202-p\*

Maybe not the case on all OS? This solution worked perfectly for me,  
Ubuntu  
10.10 server 🙂

---

<div class="post-metadata">

**Author:** ![phoyce](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bc8723/32.png) [@phoyce](https://www.ruby-forum.com/u/phoyce)\
**Post date:** [November 16, 2010, 9:34pm UTC](https://www.ruby-forum.com/t/issue-with-upstream/199189/9 "2010-11-16T21:34:05Z")

</div>

Hello!

On Tue, Nov 16, 2010 at 08:14:34PM +0000, Phil B. wrote:

> lrwxrwxrwx 1 root root 46 2010-11-15 20:30 201-s\*  
> lrwxrwxrwx 1 root root 50 2010-11-16 14:11 202-p\*

ls returns sorted results, see here:

> **[ls](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/ls.html)**

> Maybe not the case on all OS? This solution worked perfectly for me, Ubuntu  
> 10.10 server 🙂

Some filesystems may return sorted results, but it’s unlikely to  
be so in your case.

Maxim D.

---

<div class="post-metadata">

**Author:** ![phoyce](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bc8723/32.png) [@phoyce](https://www.ruby-forum.com/u/phoyce)\
**Post date:** [January 6, 2011, 9:01am UTC](https://www.ruby-forum.com/t/issue-with-upstream/199189/10 "2011-01-06T09:01:22Z")

</div>

Hello!

On Tue, Nov 16, 2010 at 05:49:16PM +0300, Maxim D. wrote:

[…]

> Most likely you used backend2 somewhere in proxy\_pass before  
> defining upstream backend2. This is not generally supported and  
> and shouldn’t be allowed, but happens to “work” (though with  
> several unexpected side effects) for now.

Disregard this (and patch). Actually, the only thing which won’t  
work as expected is an attempt to redefine unix socket with  
upstream{} block.

New patch attached.

Maxim D.

---

<div class="post-metadata">

**Author:** ![phoyce](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bc8723/32.png) [@phoyce](https://www.ruby-forum.com/u/phoyce)\
**Post date:** [November 17, 2010, 1:38pm UTC](https://www.ruby-forum.com/t/issue-with-upstream/199189/11 "2010-11-17T13:38:09Z")

</div>

You might be right, find returns different order.

I guess to be safe, I’ll move the upstream declarations into a new  
folder  
‘sites-enabled-upstream’ and stick that first in nginx.conf
