Multiple roots for multiple sites

Hi,

I’m working on multiple sites & would like to specify different
roots for each one so that identical absolute urls such as
Home on each site don’t all get
directed to the same /usr/local/nginx/html/index.html

ie,

navigate to http://localhost/site1 & the root will be
/usr/local/nginx/html/site1

navigate to http://localhost/site2 & the root will be
/usr/local/nginx/html/site2

I tried this:

location /site1/ {
    root   /usr/local/nginx/html/site1;
    index  index.html index.htm;
}

But navigating to http://localhost/site1 gets ‘404 Not Found’.
Without the above location directive, the site comes up fine
(other than than the aforementioned issue with absolute urls).

At this point I’m only running the sites locally on my laptop.

My nginx.conf looks like this:

worker_processes 1;
events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;

server {
    listen       80;
    server_name  localhost;

    location / {
        root   html;
        index  index.html index.htm;
    }

  location /site1/ {
        root   /usr/local/nginx/html/site1;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}

}

Being new to nginx, I realize I’m probably overlooking something(s)
obvious with respect to how location directives & root works here.

TIA for any help,

John


John Magolske

Your config should look like that:

location / {
root /usr/local/nginx/html/$host;
index index.html index.htm;
}

So that you want ever have to touch the configuration file even if new
sites get created. It’s the easiest but there are drawbacks, if you can
spot them.

Your config should look like that:

location / {
root /usr/local/nginx/html/$host;
index index.html index.htm;
}

So that you want ever have to touch the configuration file even if new
sites get created.

I just now tried the above directive in my nginx.conf, but end up
getting ‘404 Not Found’ on all my sites.

John


John Magolske

Hello!

On Fri, May 14, 2010 at 03:31:00PM -0700, John Magolske wrote:

navigate to http://localhost/site2 & the root will be
/usr/local/nginx/html/site2

I tried this:

location /site1/ {
    root   /usr/local/nginx/html/site1;
  •     root   /usr/local/nginx/html/site1;
    
  •     alias  /usr/local/nginx/html/site1/;
    

Directive “root” defines path to a server root (i.e. to “/”
uri), and filename calculated as + .

Directive “alias” specifies path to a matched location, and
filename calculated as + .

See here for details:

http://wiki.nginx.org/NginxHttpCoreModule#root
http://wiki.nginx.org/NginxHttpCoreModule#alias

Maxim D.

I just now tried the above directive in my nginx.conf, but end up
getting ‘404 Not Found’ on all my sites.

Well things are more complicated than that. What gets printed out in
your error log?