Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

3 Answers

up vote 3 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
add comment

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
    
add comment

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

set $PROJECT "release";
if ($host ~ "beta.my.site") {
    set $PROJECT "beta";
}
root /home/northtree/$PROJECT/;
share|improve this answer
add comment

Your Answer

 
discard

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.