Multiple site configuration

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:

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 (knowing how
nginx
processes a request
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.htmldocument
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 Re: Q: about "best" way for remove www in hostname in nginxish

# 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
requesthttp://nginx.org/en/docs/http/request_processing.htmlto learn
how nginx will address special cases.

Hope I helped.

B. R.

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. [email protected]