# Virtual hosting

**URL:** <https://www.ruby-forum.com/t/virtual-hosting/218878>\
**Category:** NGINX\
**Created:** [April 14, 2012, 1:45pm UTC](https://www.ruby-forum.com/t/virtual-hosting/218878 "2012-04-14T13:45:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gurdipe\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b77776/32.png) [@Gurdipe\_D](https://www.ruby-forum.com/u/Gurdipe_D)\
**Post date:** [April 14, 2012, 1:45pm UTC](https://www.ruby-forum.com/t/virtual-hosting/218878/1 "2012-04-14T13:45:41Z")

</div>

Hello,

on my server I have several IP-Addresses and for some of them I want to  
use Nginx to server port 80. I want to host several different domains.  
Let’s say [A.org](http://A.org) and [B.org](http://B.org) to IP-Address 1.1.1.1:80 and [C.org](http://C.org) to  
IP-Address 1.1.1.2:80, while 1.1.1.3:80 shall not be used by Nginx.

Shall I configure Nginx something like  
server {  
listen 1.1.1.1;  
server\_name [A.org](http://A.org) [B.org](http://B.org);  
if ($host ~ “[A.org](http://A.org)”) {root /A; … break;}  
if ($host ~ “[B.org](http://B.org)”) {root /B;… break;}  
}  
server {  
listen 1.1.1.2;  
server\_name [C.org](http://C.org);  
}  
or is there a way to configure three different server{}s?  
server { server\_name [A.org](http://A.org); }  
server { server\_name [B.org](http://B.org); }  
server { server\_name [C.org](http://C.org); listen 1.1.1.2; }

In the former form, I cannot use directly “root /A;” in if ().

In the latter form, I cannot use for [A.org](http://A.org) and [B.org](http://B.org) listen 1.1.1.1, as  
Nginx says it cannot bind twice IP1.1.1.1 (it can bind to it for A, but  
since the socket is already occupied, it cannot bind for for [B.org](http://B.org)}.

Moreover, as the Nginx module documentation is available both trough  
[wiki.nginx.org](http://wiki.nginx.org) ([wiki.nginx.org/HttpCoreModule](http://wiki.nginx.org/HttpCoreModule)) and  
[nginx documentation](http://www.nginx.org/en/docs/), I would like to ask which is the  
reference documentation (and what is the point to have the documentation  
on two different places – e.g. why isn’t the wiki enough).

nginx version: nginx/1.0.15  
TLS SNI support enabled  
configure arguments: --pid-path=/var/run/nginx.pid  
–conf-path=/etc/nginx/core.conf  
–error-log-path=/var/log/nginx/nginx.log --user=wwwrun --group=nogroup  
–with-ipv6 --without-poll\_module --without-select\_module  
–with-file-aio --with-http\_ssl\_module --with-http\_addition\_module  
–with-http\_xslt\_module --with-http\_image\_filter\_module  
–with-http\_sub\_module --with-http\_dav\_module --with-http\_flv\_module  
–with-http\_mp4\_module --with-http\_gzip\_static\_module  
–with-http\_random\_index\_module --with-http\_degradation\_module  
–with-http\_stub\_status\_module --http-log-path=/var/log/nginx  
–with-pcre --prefix=/usr --with-cc-opt=‘-O3 -march=native  
-Wl,–hash-style=gnu -Wl,-O1 -flto -Wl,-z,relro’  
–with-ld-opt=‘-L/usr/lib64 -L/lib64’ --without-http\_ssi\_module  
–without-http\_uwsgi\_module --without-http\_scgi\_module  
–without-http\_upstream\_ip\_hash\_module  
–without-http\_split\_clients\_module --without-http\_empty\_gif\_module

Thanks in advance for your help  
Дилян

---

<div class="post-metadata">

**Author:** ![SS\_S\_S\_SSSSS\_S\_S\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@SS\_S\_S\_SSSSS\_S\_S\_S](https://www.ruby-forum.com/u/SS_S_S_SSSSS_S_S_S)\
**Post date:** [April 14, 2012, 2:04pm UTC](https://www.ruby-forum.com/t/virtual-hosting/218878/2 "2012-04-14T14:04:16Z")

</div>

2012/4/14 Дилян Палаузов [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> server\_name [A.org](http://A.org) [B.org](http://B.org);  
> if ($host ~ “[A.org](http://A.org)”) {root /A; … break;}  
> if ($host ~ “[B.org](http://B.org)”) {root /B;… break;}  
> }

This is a really nasty way of dealing with vhosts. I don’t know  
offhand if it’s even valid.

> or is there a way to configure three different server{}s?  
> server { server\_name [A.org](http://A.org); }  
> server { server\_name [B.org](http://B.org); }  
> server { server\_name [C.org](http://C.org); listen 1.1.1.2; }
> 
> In the former form, I cannot use directly “root /A;” in if ().

_Please_ read [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/IfIsEvil).

> In the latter form, I cannot use for [A.org](http://A.org) and [B.org](http://B.org) listen 1.1.1.1, as  
> Nginx says it cannot bind twice IP1.1.1.1 (it can bind to it for A, but  
> since the socket is already occupied, it cannot bind for for [B.org](http://B.org)}.

The assertion that you cannot use the same IP twice is wrong, and  
probably the root of your misunderstanding. This _is_ legitimate  
configuration:

* * *

http {  
server {  
listen 1.1.1.1:80;  
server\_name [A.org](http://A.org);  
root /srv/A.org/public;  
}

server {  
listen 1.1.1.1:80;  
server\_name [B.org](http://B.org);  
root /srv/B.org/public;  
}

## server { listen 1.1.1.2:80; server\_name [C.org](http://C.org); root /srv/C.org/public; } }

There are more concise ways of expressing this setup, especially if  
you’re dealing with significant numbers of domains, but this is pretty  
unambiguous.

## Jonathan

Jonathan M.  
Oxford, London, UK

> **[Jonathan Matthews :: Words Typed, Great Rates For Cash](https://www.jpluscplusm.com)**

---

<div class="post-metadata">

**Author:** ![SS\_S\_S\_SSSSS\_S\_S\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@SS\_S\_S\_SSSSS\_S\_S\_S](https://www.ruby-forum.com/u/SS_S_S_SSSSS_S_S_S)\
**Post date:** [April 14, 2012, 2:06pm UTC](https://www.ruby-forum.com/t/virtual-hosting/218878/3 "2012-04-14T14:06:45Z")

</div>

On Sat, Apr 14, 2012 at 01:44:58PM +0200, wrote:

> ```
> server_name A.org B.org;
> 
> ```
> 
> server { server\_name [C.org](http://C.org); listen 1.1.1.2; }
> 
> In the former form, I cannot use directly “root /A;” in if ().
> 
> In the latter form, I cannot use for [A.org](http://A.org) and [B.org](http://B.org) listen 1.1.1.1, as  
> Nginx says it cannot bind twice IP1.1.1.1 (it can bind to it for A, but  
> since the socket is already occupied, it cannot bind for for [B.org](http://B.org)}.

```
server { listen 1.1.1.1; server_name A.org; }
server { listen 1.1.1.1; server_name B.org; }
server { listen 1.1.1.2; server_name C.org; }

```

> Moreover, as the Nginx module documentation is available both trough  
> [wiki.nginx.org](http://wiki.nginx.org) ([GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/HttpCoreModule)) and  
> [nginx documentation](http://www.nginx.org/en/docs/), I would like to ask which is the  
> reference documentation (and what is the point to have the documentation  
> on two different places – e.g. why isn’t the wiki enough).

The right place is [nginx documentation](http://www.nginx.org/en/docs/)  
Until this year the only English documentation was in wiki,  
but the wiki contains errors and inaccurancies.

–  
Igor S.
