# Multiple site configuration

**URL:** <https://www.ruby-forum.com/t/multiple-site-configuration/228937>\
**Category:** NGINX\
**Created:** [May 13, 2013, 6:27pm UTC](https://www.ruby-forum.com/t/multiple-site-configuration/228937 "2013-05-13T18:27:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![guillaumeserton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/0ea827/32.png) [@guillaumeserton](https://www.ruby-forum.com/u/guillaumeserton)\
**Post date:** [May 13, 2013, 6:27pm UTC](https://www.ruby-forum.com/t/multiple-site-configuration/228937/1 "2013-05-13T18:27:06Z")

</div>

Hello,

I would like to set nginx to use several website, and that they are  
reachable for some of them with a domain name or in localhost.

Example:  
Site1: only accessible on localhost with 192.168.x.x/site1 (root:  
/var/www/site1)  
Site2: accessible accessible on site2.eu domain and also on localhost  
with  
192.168.x.x/site2 (root: /var/www/site2)  
Site3: accessible on the subdomain sub.site2.eu and also on localhost  
with  
192.168.x.x/subsite2 (root: /var/www/subsite2)

I have set two sites (site2 and site3) on my configuration and reachable  
with domain and subdomain. But i can’t reach them locallly with the  
ipaddress/nameofsite. I have only one site accessible directly under my  
local ip address.

So, currently i have:

Site2 (var/www/site2: reachable with his domain site2.eu or www.site2.eu  
and  
also on the local ip 192.168.x.x  
SIte3 (var/www/site3: reachable with the domain sub.site2.eu. Otherwise  
i  
can’t reach him locally. I would like to reach him with  
192.168.x.x/site3

Thanks for your help.

Posted at Nginx Forum:

> **[Multiple site configuration](https://forum.nginx.org/read.php?2,239147,239147#msg-239147)**
>
> Hello, I would like to set nginx to use several website, and that they are reachable for some of them with a domain name or in localhost. Example: Site1: only accessible on localhost with 192.168.x.x/site1 (root: /var/www/site1) Site2: accessible...

---

<div class="post-metadata">

**Author:** ![guillaumeserton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/0ea827/32.png) [@guillaumeserton](https://www.ruby-forum.com/u/guillaumeserton)\
**Post date:** [May 13, 2013, 7:00pm UTC](https://www.ruby-forum.com/t/multiple-site-configuration/228937/2 "2013-05-13T19:00:23Z")

</div>

The problem I see is you try to address the process of requests  
differently  
whether from inside your network or from the outside.  
You may already solve the problem of your outside interface easily with  
server  
names [http://nginx.org/en/docs/http/server\_names.html](http://nginx.org/en/docs/http/server_names.html) (knowing how  
nginx  
processes a request  
[http://nginx.org/en/docs/http/request\_processing.html](http://nginx.org/en/docs/http/request_processing.html)),  
listening for ‘localhost’, ‘site2.eu’ and ‘sub.site.eu’.

What’s uncommon is your way of addressing local websites, all locations  
(/site2, /subsite2) inside a unique server listening on a local IP  
address.  
That breaks the paradigm and thus becomes incompatible with the previous  
work.

The ‘Mixed name-based and IP-based server names’ section of the How  
Nginx  
processes a request  
[http://nginx.org/en/docs/http/request\_processing.html](http://nginx.org/en/docs/http/request_processing.html)document  
provides you with intelligence on how things may be done:  
server {  
listen 192.168.x.x; # Default port is 80 when not specified  
# Not sure whether or not you can use CIDR notation here, such as  
192.168/16;

```
# No 'server_name' directive here, default is empty, which means all

```

request for this IP on port 80 without another server matching the  
server\_name will end up here

```
# Content of site 1 here

```

}

server {  
listen 80; # Default listens on all addresses/interfaces when only a  
port is specified  
server\_name site2.eu; # Use location redirection to remove the www  
in  
host, see [http://forum.nginx.org/read.php?2,238910,238911#msg-238911](http://forum.nginx.org/read.php?2,238910,238911#msg-238911)

```
# Content of site 2 here

```

}

server {  
listen 80;  
server\_name sub.site2.eu;

```
# Content of site 3 here

```

}

site2.eu and sub.site2.eu will be accessible from inside/outside your  
network using the same domain name.  
site 1 will only be accessible from the 192.168.x.x address which  
shouldn’t  
be routed from outside your network, thus making it locally accessible  
only. You may add whatever server name you wish to it, it will stick to  
that network.

Another addendum: document on how Nginx choose its default server for a  
particular request in How nginx processes a  
request[http://nginx.org/en/docs/http/request\_processing.html](http://nginx.org/en/docs/http/request_processing.html)to learn  
how nginx will address special cases.

## Hope I helped.

_B. R._

---

<div class="post-metadata">

**Author:** ![guillaumeserton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/0ea827/32.png) [@guillaumeserton](https://www.ruby-forum.com/u/guillaumeserton)\
**Post date:** [May 13, 2013, 11:50pm UTC](https://www.ruby-forum.com/t/multiple-site-configuration/228937/3 "2013-05-13T23:50:52Z")

</div>

On Mon, May 13, 2013 at 12:26:32PM -0400, guillaumeserton wrote:

Hi there,

> I would like to set nginx to use several website, and that they are  
> reachable for some of them with a domain name or in localhost.

Based on the connected ip:port and the provided Host: header, nginx will  
choose one server{} block to handle one request.

You can nominate the “server\_name” to be handled by each server{},  
and you can nominate one default server{} per ip:port.

I’m going to assume that when you say “on localhost with  
192.168.x.x/site1”, that that is equivalent to “using any  
not-otherwise-specified domain name”. If you really mean “only when  
using the IP address”, that can be added separately.

What you want can be done; but you will need to make sure that every  
relative link within the content makes sense both when it is relative  
to “/site1/” and to “/”. Essentially, this means that no relative link  
starts with “/”, but instead uses the correct number of “../” segments  
if appropriate.

Without that, you will find that lots of linked things do not work.

You will configure:

one server{} for each of the individual domain names;

one (default) server{} with multiple location{}s, for each of the  
individual domain names.

> Example:  
> Site1: only accessible on localhost with 192.168.x.x/site1 (root:  
> /var/www/site1)

server {  
# this is the default server  
location = /site1 {  
return 301 /site1/;  
}  
location ^~ /site1/ {  
alias /var/www/site1/  
# or  
# root /var/www;  
}  
}

> Site2: accessible accessible on site2.eu domain and also on localhost with  
> 192.168.x.x/site2 (root: /var/www/site2)

server {  
server\_name site2.eu;  
root /var/www/site2;  
}

and also, add to the earlier default server block:

```
location = /site2 {
  return 301 /site2/;
}
location ^~ /site2/ {
  alias /var/www/site2/
}

```

> Site3: accessible on the subdomain sub.site2.eu and also on localhost with  
> 192.168.x.x/subsite2 (root: /var/www/subsite2)

server {  
server\_name sub.site2.eu;  
root /var/www/subsite2;  
}

and two extra location{}s in the default server block.

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
