Configure nginx with multiple locations with different root folders on subdomain

simoes picture simoes · Jul 20, 2012 · Viewed 293.3k times · Source

I'm looking to serve the root url of a subdomain and directory of a subdomain to two different folders on my server. Here is the simple set-up that I have and is not working...

server {

    index index.html index.htm;
    server_name test.example.com;

    location / {
            root /web/test.example.com/www;
    }

    location /static {
            root /web/test.example.com/static;
    }
}

In this example going to test.example.com/ would bring the index file in /web/test.example.com/www

and going to test.example.com/static would bring the index file in /web/test.example.com/static

Answer

furq picture furq · Jul 20, 2012

You need to use the alias directive for location /static:

server {

  index index.html;
  server_name test.example.com;

  root /web/test.example.com/www;

  location /static/ {
    alias /web/test.example.com/static/;
  }

}

The nginx wiki explains the difference between root and alias better than I can:

Note that it may look similar to the root directive at first sight, but the document root doesn't change, just the file system path used for the request. The location part of the request is dropped in the request Nginx issues.

Note that root and alias handle trailing slashes differently.