nginx config woes for multiple subdomains & domains

Posted by Peter Hanneman on Server Fault See other posts from Server Fault or by Peter Hanneman
Published on 2011-01-04T09:31:12Z Indexed on 2011/01/04 9:55 UTC
Read the original article Hit count: 265

Filed under:

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

© Server Fault or respective owner

Related posts about nginx