With nginx is there a method to setup mass dynamic virtual hosts similar to the way apache2 can be configured? I'm setting up an saas application each user will have their own subdomain and i'd like to use nginx over apache2.


Code below should be how to configure

map $http_host $subdir {
default "default";
.foo.bar.com "foo";
.baz.bar.com "baz";

server {
root /path/to/$subdir;

  • Nginx will let you use a regex to capture part of the server_name (e.g. the subdomain) and you can use that elsewhere in your configuration - it should do what you want without the necessity of setting up a virtualhost for every user. See: nginx.org/en/docs/http/server_names.html#regex_names
    – cyberx86
    Nov 20 '11 at 4:44
  • I'm not sure I can use regex to capture *.domain.com because I will be setting up server aliases to let users setup a cname for some plans and map one of their own domains to our subdomain. I assume the regex would work similar to a wildcard capturing any entry coming in.
    – Anagio
    Nov 20 '11 at 4:51
  • You can probably use the HttpMapModule to map server_names to the root location, so that whatever host name is sent to the server can be looked up and the correct files served. There may be another way - using the EmbeddedPerlModule to lookup information from a data-store. I think (not quite sure) that using Perl will let you avoid needing to reload the server with each added user. On the other hand, there could be a performance hit using Perl to look up information for each request.
    – cyberx86
    Nov 20 '11 at 5:28
  • I read nginx -s reload will reload I suppose the config files without users getting disconnected. On another forum a user also suggested the HttpMapModule, I added it to my code above.
    – Anagio
    Nov 20 '11 at 6:47

Yes, setting up virtual hosts is definitely possible, please check Nginx documentation on server configuration directive and default config samples. The easiest way is to put server config sections in separate files under /etc/nginx/conf.d and to include them by a pattern, like include /etc/nginx/conf.d/*.conf; in the main nginx config file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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