Virtual hosting

Jonathan Matthews contact at jpluscplusm.com
Sat Apr 14 12:03:33 UTC 2012


2012/4/14 Дилян Палаузов <dilyan.palauzov at aegee.org>:
> 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 and B.org to IP-Address 1.1.1.1:80 and 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 B.org;
>   if ($host ~ "A.org") {root /A; ... break;}
>   if ($host ~ "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; }
>  server { server_name B.org; }
>  server { server_name C.org; listen 1.1.1.2; }
>
> In the former form, I cannot use directly "root /A;" in if ().

*Please* read http://wiki.nginx.org/IfIsEvil.

> In the latter form, I cannot use for A.org and 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}.

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;
   root /srv/A.org/public;
  }

  server {
   listen 1.1.1.1:80;
   server_name B.org;
   root /srv/B.org/public;
  }

  server {
   listen 1.1.1.2:80;
   server_name 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 Matthews
Oxford, London, UK
http://www.jpluscplusm.com/contact.html



More information about the nginx mailing list