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 finally moving away from Apache and I've got the latest development version of nginx running on a fully updated Ubuntu 10.04 VPS. I've got a single dedicated IP for the box (1.2.3.4) but I've got two separate domains pointing to the server: www.example1.com and www.example2.net.

I would like to map the fallowing relationships between urls and document roots in the config:

 www.example1.com / example1.com -> /var/www/pub/example1.com/
 subdomain.example1.com -> /var/www/dev/subdomain/example1.com/
 www.example2.net / example2.net -> /var/www/pub/example2.net/
 subdomain.example2.net -> /var/www/dev/subdomain/example2.net/

Where the name of the requested subdomain is a folder under /var/www/dev/. Ideally a request for a non-existent subdomain(no matching folder found) would result in a rewrite to the public site (eg: invalid.example1.com --> www.example1.com) however a mere "404 Not Found" wouldn't be the worst thing in the world. It would also be nice if I didn't need to modify the config every time I mkdir a new subdomain folder - even better if I don't need to edit it for a new domain either...but now I'm getting greedy... :p Although in my defense Apache did all of this with a single directive.

Does anyone know how I can efficiently mimic this behavior in nginx?

Thanks in advance,

Peter Hanneman

share|improve this question
add comment

1 Answer

You can do the following. You can set the root based on the $host request.

location / {

root /usr/local/$host/www/;

}

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.