# How to use nginx as loadbalancer for different webserver?

**URL:** <https://www.ruby-forum.com/t/how-to-use-nginx-as-loadbalancer-for-different-webserver/228409>\
**Category:** NGINX\
**Created:** [April 21, 2013, 8:31am UTC](https://www.ruby-forum.com/t/how-to-use-nginx-as-loadbalancer-for-different-webserver/228409 "2013-04-21T08:31:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SSJSSrg\_KastningSS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@SSJSSrg\_KastningSS\_S](https://www.ruby-forum.com/u/SSJSSrg_KastningSS_S)\
**Post date:** [April 21, 2013, 8:31am UTC](https://www.ruby-forum.com/t/how-to-use-nginx-as-loadbalancer-for-different-webserver/228409/1 "2013-04-21T08:31:01Z")

</div>

Hello all.

I’m new to this maillinglist as to nginx as well. I setup a nginx to run  
as  
a loadbalancer with just adding the following lines to my  
/etc/nginx/nginx\_conf. This config works fine.

http {  
upstream loadbalancer1 {  
server 192.168.0.1:80;  
server 192.168.0.2:80;  
}

server {  
listen 80;  
server\_name [www.example.com](http://www.example.com) [example.com](http://example.com);  
location / {  
proxy\_pass [http://loadbalancer1](http://loadbalancer1);  
}  
}

Please note, that [example.com](http://example.com) isn’t my real domain. So that’s not the  
error. 😉

Now I tried to add a second site which is hosted on two different  
webservers. My configuration in /etc/nginx/nginx\_conf looks like the  
following now.

http {  
upstream loadbalancer1 {  
server 192.168.0.1:80;  
server 192.168.0.2:80;  
}

server {  
listen 80;  
server\_name [www.example.com](http://www.example.com) [example.com](http://example.com);  
location / {  
proxy\_pass [http://loadbalancer1](http://loadbalancer1);  
}  
}

upstream loadbalancer2 {  
server 192.168.0.3:80;  
server 192.168.0.4:80;  
}

server {  
listen 80;  
server\_name [www.anyway.com](http://www.anyway.com) [anyway.com](http://anyway.com);  
location / {  
proxy\_pass [http://loadbalancer2](http://loadbalancer2);  
}  
}}

But if I try to get [www.anyway.com](http://www.anyway.com) it didn’t work and I got a request  
timeout. Could somebody please tell me, what’s wrong with my  
configuration?

Best Regards

Joerg

---

<div class="post-metadata">

**Author:** ![SSJSSrg\_KastningSS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@SSJSSrg\_KastningSS\_S](https://www.ruby-forum.com/u/SSJSSrg_KastningSS_S)\
**Post date:** [April 21, 2013, 8:40am UTC](https://www.ruby-forum.com/t/how-to-use-nginx-as-loadbalancer-for-different-webserver/228409/2 "2013-04-21T08:40:22Z")

</div>

Try telnetting to port 80 on 192.168.0.3 and .4 to check they’re  
listening, and that [www.anyway.com](http://www.anyway.com) resolves correctly…

On Sun, 2013-04-21 at 08:29 +0200, Jörg Kastning wrote:

> ```
> server 192.168.0.2:80;
> 
> ```
> 
> Please note, that [example.com](http://example.com) isn’t my real domain. So that’s not the error. 😉  
> server 192.168.0.2:80;  
> upstream loadbalancer2 {  
> }
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://mailman.nginx.org/mailman/listinfo/nginx)

–  
Steve H. BSc(Hons) MIITP

> **[Linux & Admin Support Services | "Services provided by people who are proud...](https://www.greengecko.co.nz)**
>
> Offers linux support services, systems and database administration, consultancy and the full development lifecycle for open source - based solutions

Skype: sholdowa

---

<div class="post-metadata">

**Author:** ![SSJSSrg\_KastningSS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@SSJSSrg\_KastningSS\_S](https://www.ruby-forum.com/u/SSJSSrg_KastningSS_S)\
**Post date:** [April 21, 2013, 10:22am UTC](https://www.ruby-forum.com/t/how-to-use-nginx-as-loadbalancer-for-different-webserver/228409/3 "2013-04-21T10:22:00Z")

</div>

can you make sure that [anyway.com](http://anyway.com) ist reachable?

sounds like the path to your front-lb is somehow  
not working.

i think i dont need to ask for an nginx-restart after config-changes?

if you upstream-config is messy or your upstream-servers  
are unreachable you should usually see a:  
502 Bad Gateway

regards,

mex

Posted at Nginx Forum:

> **[Re: How to use nginx as loadbalancer for different webserver?](https://forum.nginx.org/read.php?2,238509,238511#msg-238511)**
>
> Hello all. I'm new to this maillinglist as to nginx as well. I setup a nginx to run as a loadbalancer with just adding the following lines to my /etc/nginx/nginx\_conf. This config works fine. http { upstream loadbalancer1 { server 192.168.0.1:8

---

<div class="post-metadata">

**Author:** ![SSJSSrg\_KastningSS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@SSJSSrg\_KastningSS\_S](https://www.ruby-forum.com/u/SSJSSrg_KastningSS_S)\
**Post date:** [April 22, 2013, 8:39am UTC](https://www.ruby-forum.com/t/how-to-use-nginx-as-loadbalancer-for-different-webserver/228409/4 "2013-04-22T08:39:02Z")

</div>

Hi.

I found the mistake. The Firewall-Policy was configured but the used  
Host-Object was wrong. There were the wrong ip addresses for my  
webservrs  
added. I changed this and can reach my webservers now.

Thanks for your help.

Mit freundlichen Gren

Jrg Kastning  
IT-Systemadministrator

SYNAXON AG  
Falkenstrae 31  
33758 Schlo Holte-Stukenbrock  
Fon: +49(0)5207 9299-282  
Fax +49(0)5207 9299-296  
mailto: removed\_email\_address@domain.invalid  
XMPP: removed\_email\_address@domain.invalid

Vorstand: Frank Roebers (Vorsitzender), Andreas Wenninger, Mark Schrder  
Aufsichtsratsvorsitzender: Dr. Gnter Lewald  
Handelsregister Bielefeld HRB 36014

Weitere Infos unter: [http://www.synaxon.de](http://www.synaxon.de)

2013/4/21 mex [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
