Multiple site configuration

Francis Daly francis at
Mon May 13 21:50:14 UTC 2013

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 domain and also on localhost with
> 192.168.x.x/site2 (root: /var/www/site2)

  server {
    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 and also on localhost with
> 192.168.x.x/subsite2 (root: /var/www/subsite2)

  server {
    root /var/www/subsite2;

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

Francis Daly        francis at

More information about the nginx mailing list