Nginx - Change root directory based on server_name?

I'm trying to save some time on my development machine by not creating a new nginx server config for each new site I test. I'm wondering if there is a way for nginx to change the root directory based on the domain I'm loading.

So if I have

mysite.loc
myothersite.loc
something.loc

Nginx will look inside

www/mysite.loc
www/myothersite.loc
www/something.loc

That would allow me to test new things on mock domains just a little bit faster since I don't have to mess with a new /etc/nginx/sites-available config for each one.

server {
    listen 80;
    server_name *.loc;
    root /www/$host;
    index index.html;
}

Is this possible? Is there another way to do it?


you could also do

  server_name _; 
  root   /opt/railo/tomcat/webapps/$host;

Not sure if this is a good idea but I have tested it and it works.


The nginx configuration has something that can help you:

server {
  server_name   ~^(www\.)?(?<domain>.+)$;
  root  /sites/$domain;
}

I think that it is exactly what you want right? Search for named captures to get to this part of the documentation, but I urge you to read all description of server_name.