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 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 ( but I've got two separate domains pointing to the server: and

I would like to map the fallowing relationships between urls and document roots in the config: / -> /var/www/pub/ -> /var/www/dev/subdomain/ / -> /var/www/pub/ -> /var/www/dev/subdomain/

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: --> 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

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

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.