Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


Nginx will look inside


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?

share|improve this question
up vote 4 down vote accepted

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.

share|improve this answer
+1 This has been my recent approach. – Xeoncross Nov 9 '12 at 21:13

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.

share|improve this answer
hmmm... I guess that is the answer - but I have nginx 0.7.65 and that requires at least 0.8.25. – Xeoncross Feb 28 '11 at 3:37

As root directive is not allowed in if condition. If you have complicated judgement, please refer to this solution.

set $PROJECT "release";
if ($host ~ "") {
    set $PROJECT "beta";
root /home/northtree/$PROJECT/;
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.