# How to configure multiple virtual server on nginx?

**URL:** <https://www.ruby-forum.com/t/how-to-configure-multiple-virtual-server-on-nginx/209951>\
**Category:** NGINX\
**Created:** [July 27, 2011, 4:06am UTC](https://www.ruby-forum.com/t/how-to-configure-multiple-virtual-server-on-nginx/209951 "2011-07-27T04:06:58Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [July 27, 2011, 4:06am UTC](https://www.ruby-forum.com/t/how-to-configure-multiple-virtual-server-on-nginx/209951/1 "2011-07-27T04:06:58Z")

</div>

Hi,all  
Pardon please,I’m not an English native speaker.So,I may make mistake in  
grammar.  
I have 4 IP address on my ubuntu server computer.For the convenience to  
ditinguish them ,I name them as IP-1,IP-2,IP-3 and IP-4.  
Now,I configure 3 virtual servers on nginx.The key content of  
configuration is following.

#upstream  
upstream my\_upstream{  
server IP-3:8080;  
server IP-4:8080;  
}

#the first server  
server {  
listen IP-1:80;  
location / {  
index index.php;  
root /usr/share/nginx/www;  
}  
location ~.\*.(php|php5)$ {  
root /usr/share/nginx/www;  
fastcgi\_pass 127.0.0.1:9000;  
fastcgi\_index index.php;  
include fastcgi\_params;  
}  
}

#the second server  
server {  
listen IP-2:80;  
location / {  
index index.php;  
root /usr/share/nginx/www;  
}  
location ~.\*.(php|php5)$ {  
root /usr/share/nginx/www;  
fastcgi\_pass 127.0.0.1:9000;  
fastcgi\_index index.php;  
include fastcgi\_params;  
}  
}

#the third server  
server {  
listen IP-3:80;  
location / {  
proxy\_pass http://my\_upstream;  
}  
location ~.\*.(php|php5)$ {  
root /usr/share/nginx/www;  
fastcgi\_pass 127.0.0.1:9000;  
fastcgi\_index index.php;  
include fastcgi\_params;  
}  
}  
########################################################

In my opinion,those 3 virtual servers can wholely work right.But I met a  
very surprising problem:the 1st and 2nd server can work  
right,nevertheless，the third virtual server can not access.I look over  
nginx.conf,and never find out any configuration mistake.Then,I change  
the 3rd server’s LISTEN IP address IP-3 to IP-2,and change the 2nd  
server’s LISTEN IP address IP-2 to IP-3.Oh my god,the 2nd server can not  
access,but the 3rd can.I just exchange their LISTEN IP address.If you  
have understood my problem description,would you please give me some  
good suggestion?  
Thanks!

Posted at Nginx Forum:

> **[how to configure multiple virtual server on nginx?](https://forum.nginx.org/read.php?2,212966,212966#msg-212966)**
>
> Hi,all Pardon please,I'm not an English native speaker.So,I may make mistake in grammar. I have 4 IP address on my ubuntu server computer.For the convenience to ditinguish them ,I name them as IP-1,IP-2,IP-3 and IP-4. Now,I configure 3 virtual...

---

<div class="post-metadata">

**Author:** ![debugger87](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/919ad9/32.png) [@debugger87](https://www.ruby-forum.com/u/debugger87)\
**Post date:** [July 27, 2011, 4:22am UTC](https://www.ruby-forum.com/t/how-to-configure-multiple-virtual-server-on-nginx/209951/2 "2011-07-27T04:22:25Z")

</div>

I have ever guess that IP-3:80 is not listened by nginx.But when I run  
“netstat -antup” in command line of shell,the result following tell me  
that I guess wrong.  
Proto Recv-Q Send-Q Local Address Foreign Address  
State PID/Program name  
tcp 0 0 IP-3:80 0.0.0.0:\* LISTEN  
7346/nginx  
tcp 0 0 IP-2:80 0.0.0.0:\* LISTEN  
7346/nginx  
tcp 0 0 IP-2:80 0.0.0.0:\* LISTEN  
7346/nginx

OMG,It’s a very strange problem.

Posted at Nginx Forum:

> **[Re: how to configure multiple virtual server on nginx?](https://forum.nginx.org/read.php?2,212966,212968#msg-212968)**
>
> Hi,all Pardon please,I'm not an English native speaker.So,I may make mistake in grammar. I have 4 IP address on my ubuntu server computer.For the convenience to ditinguish them ,I name them as IP-1,IP-2,IP-3 and IP-4. Now,I configure 3 virtual...

---

<div class="post-metadata">

**Author:** ![debugger87](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/919ad9/32.png) [@debugger87](https://www.ruby-forum.com/u/debugger87)\
**Post date:** [July 27, 2011, 4:24am UTC](https://www.ruby-forum.com/t/how-to-configure-multiple-virtual-server-on-nginx/209951/3 "2011-07-27T04:24:16Z")

</div>

Pardon  
##############################################  
I have ever guess that IP-3:80 is not listened by nginx.But when I run  
“netstat -antup” in command line of shell,the result following tell me  
that I guess wrong.  
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program  
name  
tcp 0 0 IP-3:80 0.0.0.0:\* LISTEN 7346/nginx  
tcp 0 0 IP-2:80 0.0.0.0:\* LISTEN 7346/nginx  
tcp 0 0 IP-1:80 0.0.0.0:\* LISTEN 7346/nginx

OMG,It’s a very strange problem.

Posted at Nginx Forum:

> **[Re: how to configure multiple virtual server on nginx?](https://forum.nginx.org/read.php?2,212966,212969#msg-212969)**
>
> Hi,all Pardon please,I'm not an English native speaker.So,I may make mistake in grammar. I have 4 IP address on my ubuntu server computer.For the convenience to ditinguish them ,I name them as IP-1,IP-2,IP-3 and IP-4. Now,I configure 3 virtual...

---

<div class="post-metadata">

**Author:** ![debugger87](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/919ad9/32.png) [@debugger87](https://www.ruby-forum.com/u/debugger87)\
**Post date:** [July 27, 2011, 10:58am UTC](https://www.ruby-forum.com/t/how-to-configure-multiple-virtual-server-on-nginx/209951/4 "2011-07-27T10:58:49Z")

</div>

Thank you,Maxim D…  
I also think that the problem is outside of nginx.But I’m confused why  
IP-1 and IP-2 work,IP-3 don’t.  
On my computer,IP-1:8080,IP-2:8080,IP-3:8080 and IP-4:8080 are LISTENED  
by tomcat application server.  
I can access my tomcat by the whole IP address.It looks like a  
contradiction that I can access tomcat via IP-3:8080,  
and I can not access nginx via IP-3:80.It’s very terrible!

Posted at Nginx Forum:

> **[Re: how to configure multiple virtual server on nginx?](https://forum.nginx.org/read.php?2,212966,212988#msg-212988)**
>
> Hi,all Pardon please,I'm not an English native speaker.So,I may make mistake in grammar. I have 4 IP address on my ubuntu server computer.For the convenience to ditinguish them ,I name them as IP-1,IP-2,IP-3 and IP-4. Now,I configure 3 virtual...

---

<div class="post-metadata">

**Author:** ![debugger87](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/919ad9/32.png) [@debugger87](https://www.ruby-forum.com/u/debugger87)\
**Post date:** [July 27, 2011, 9:21am UTC](https://www.ruby-forum.com/t/how-to-configure-multiple-virtual-server-on-nginx/209951/5 "2011-07-27T09:21:24Z")

</div>

Hello!

On Tue, Jul 26, 2011 at 10:06:12PM -0400, debugger87 wrote:

> Hi,all  
> Pardon please,I’m not an English native speaker.So,I may make mistake in  
> grammar.  
> I have 4 IP address on my ubuntu server computer.For the convenience to  
> ditinguish them ,I name them as IP-1,IP-2,IP-3 and IP-4.  
> Now,I configure 3 virtual servers on nginx.The key content of  
> configuration is following.

[…]

> In my opinion,those 3 virtual servers can wholely work right.But I met a  
> very surprising problem:the 1st and 2nd server can work  
> right,nevertheless，the third virtual server can not access.I look over  
> nginx.conf,and never find out any configuration mistake.Then,I change  
> the 3rd server’s LISTEN IP address IP-3 to IP-2,and change the 2nd  
> server’s LISTEN IP address IP-2 to IP-3.Oh my god,the 2nd server can not  
> access,but the 3rd can.I just exchange their LISTEN IP address.If you  
> have understood my problem description,would you please give me some  
> good suggestion?

From your description it looks like the problem lies somewhere  
outside of nginx, e.g. your firewall preventing you from accessing  
IP-3.

Maxim D.

---

<div class="post-metadata">

**Author:** ![debugger87](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/919ad9/32.png) [@debugger87](https://www.ruby-forum.com/u/debugger87)\
**Post date:** [July 27, 2011, 12:04pm UTC](https://www.ruby-forum.com/t/how-to-configure-multiple-virtual-server-on-nginx/209951/6 "2011-07-27T12:04:23Z")

</div>

## Igor S. Wrote:

> address.It looks like a  
> proxy\_set\_header Host  
> 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)

Thanks,Igor.  
I have changed the configuration file nginx.conf as what you say.  
Things go athwart,the 3rd server doesn’t work as before.  
To describe my problem more definetely,I will give more information as  
following.  
IP-1:80 \<=====\> 1st server IP-1:8080 \<=========\>tomcat both work  
right  
IP-2:80 \<=====\> 2nd server IP-2:8080 \<=========\>tomcat both work  
right  
IP-3:80 \<=====\> 3rd server IP-3:8080 \<=========\>tomcat I can  
access tomcat by IP-3:8080,but can not access nginx by IP-3:80.

Then,I try another configuration  
IP-1:80 \<=====\> 1st server IP-1:8080 \<=========\>tomcat both work  
right  
IP-3:80 \<=====\> 2nd server IP-2:8080 \<=========\>tomcat I can  
access tomcat by IP-2:8080,but can not access nginx by IP-3:80.  
IP-2:80 \<=====\> 3rd server IP-3:8080 \<=========\>tomcat both work  
right

In my eyes,my nginx configuration is correct.But the IP address IP-3  
looks like very special.It can work for tomcat,but nginx not.  
Have you ever met that strange problem?I’m struglling to sovle that  
problem.  
It’s pleasure to wait for your reply.  
Thanks!

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,212966,212992#msg-212992](http://forum.nginx.org/read.php?2,212966,212992#msg-212992)

---

<div class="post-metadata">

**Author:** ![debugger87](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/919ad9/32.png) [@debugger87](https://www.ruby-forum.com/u/debugger87)\
**Post date:** [July 27, 2011, 11:03am UTC](https://www.ruby-forum.com/t/how-to-configure-multiple-virtual-server-on-nginx/209951/7 "2011-07-27T11:03:10Z")

</div>

On Wed, Jul 27, 2011 at 04:58:14AM -0400, debugger87 wrote:

> Thank you,Maxim D..  
> I also think that the problem is outside of nginx.But I’m confused why  
> IP-1 and IP-2 work,IP-3 don’t.  
> On my computer,IP-1:8080,IP-2:8080,IP-3:8080 and IP-4:8080 are LISTENED  
> by tomcat application server.  
> I can access my tomcat by the whole IP address.It looks like a  
> contradiction that I can access tomcat via IP-3:8080,  
> and I can not access nginx via IP-3:80.It’s very terrible!

Probably you need to add tomcat server name in Host header:

```
    location / {
            proxy_pass http://my_upstream;
            proxy_set_header Host TOMCAT.DOMAIN.NAME;
    }

```

Since by default nginx sends “Host: my\_upstream”.

–  
Igor S.

---

<div class="post-metadata">

**Author:** ![debugger87](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/919ad9/32.png) [@debugger87](https://www.ruby-forum.com/u/debugger87)\
**Post date:** [July 27, 2011, 12:26pm UTC](https://www.ruby-forum.com/t/how-to-configure-multiple-virtual-server-on-nginx/209951/8 "2011-07-27T12:26:40Z")

</div>

Hello!

On Wed, Jul 27, 2011 at 04:58:14AM -0400, debugger87 wrote:

> Thank you,Maxim D..  
> I also think that the problem is outside of nginx.But I’m confused why  
> IP-1 and IP-2 work,IP-3 don’t.  
> On my computer,IP-1:8080,IP-2:8080,IP-3:8080 and IP-4:8080 are LISTENED  
> by tomcat application server.  
> I can access my tomcat by the whole IP address.It looks like a  
> contradiction that I can access tomcat via IP-3:8080,  
> and I can not access nginx via IP-3:80.It’s very terrible!

Try setting something else to listen on IP-3:80 (e.g. “nc -l IP-3 80”)  
and check if you are able to reach it.

Maxim D.

---

<div class="post-metadata">

**Author:** ![debugger87](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/919ad9/32.png) [@debugger87](https://www.ruby-forum.com/u/debugger87)\
**Post date:** [July 28, 2011, 3:29am UTC](https://www.ruby-forum.com/t/how-to-configure-multiple-virtual-server-on-nginx/209951/9 "2011-07-28T03:29:01Z")

</div>

## Maxim D. Wrote:

> IP-4:8080 are LISTENED  
> and check if you are able to reach it.
> 
> Maxim D.
> 
> * * *
> 
> 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)

Thank you,Maxim D..  
I check the IP-3:80 in something else(e.g. apache2),and I found that I  
can not reach the apache2 via IP-3:80.  
Obviously,the true problem lies in the special IP address IP-3.So,I  
should not spend more time in nginx configuration.  
Any way ,thanks for your reply.  
With best wishes.

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,212966,213022#msg-213022](http://forum.nginx.org/read.php?2,212966,213022#msg-213022)

---

<div class="post-metadata">

**Author:** ![debugger87](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/919ad9/32.png) [@debugger87](https://www.ruby-forum.com/u/debugger87)\
**Post date:** [July 27, 2011, 12:13pm UTC](https://www.ruby-forum.com/t/how-to-configure-multiple-virtual-server-on-nginx/209951/10 "2011-07-27T12:13:04Z")

</div>

On Wed, Jul 27, 2011 at 06:03:59AM -0400, debugger87 wrote:

> > > by tomcat application server.  
> > > location / {
> 
> IP-1:80 \<=====\> 1st server IP-1:8080 \<=========\>tomcat both work  
> access tomcat by IP-2:8080,but can not access nginx by IP-3:80.  
> IP-2:80 \<=====\> 3rd server IP-3:8080 \<=========\>tomcat both work  
> right
> 
> In my eyes,my nginx configuration is correct.But the IP address IP-3  
> looks like very special.It can work for tomcat,but nginx not.  
> Have you ever met that strange problem?I’m struglling to sovle that  
> problem.  
> It’s pleasure to wait for your reply.  
> Thanks!

In this case only debug log may help:

> **[A debugging log](http://nginx.org/en/docs/debugging_log.html)**

–  
Igor S.
