27

Been playing with nginx for about an hour trying to setup mass dynamic virtual hosts. If you ever done it in apache you know what I mean.

Goal is to have dynamic subdomains for few people in the office (more than 50)

1
  • 1
    What do you mean by "dynamic subdomains"? How "dynamic" are you looking for? Is restarting the server acceptable?
    – cdeszaq
    Commented Dec 2, 2011 at 14:22

7 Answers 7

77

Perhaps doing this will get you where you want to be:

server {

    root /sites/$http_host;

    server_name $http_host;

    ...

}

I like this as I can literally create sites on the fly, just create new directory named after the domain and point the DNS to the server ip.

9
  • 7
    I can't tell you how long I've been looking for this... Thank you!
    – jackweirdy
    Commented Jan 29, 2013 at 17:57
  • Great stuff! Can I do this with wildcard subdomains, to have both www.domain.com and domain.com point to /sites/domain.com/? I have a httpd.conf for apache that does this, but don't know how to replicate the configuration with nginx.
    – Juhani
    Commented May 31, 2013 at 10:13
  • @Juhani I'm new to nginx but to point www.domain.com to redirect to domain.com perhaps use some similar rules like for rewrite: nginx.org/en/docs/http/converting_rewrite_rules.html maybe with www.$http_host? And redirect it to $http_host?
    – user133408
    Commented Oct 11, 2013 at 6:26
  • 2
    I suggest you create a symlink for www. @Juhani
    – Matej
    Commented Jul 23, 2014 at 2:41
  • 2
    I'd suggest server_name _default_ instead of using $http_host, per a discussion in the #nginx IRC room on Freenode. That way you're catching the default vhost and it won't interfere with other redirect rules
    – djsumdog
    Commented Nov 13, 2017 at 14:15
22

You will need some scripting knowledge to put this together. I would use PHP, but if you are good in bash scripting use that. I would do it like this:

  1. First create some folder (/usr/local/etc/nginx/domain.com/).

  2. In main nginx.conf add command : include /usr/local/etc/nginx/domain.com/*.conf;

  3. Every file in this folder should be different vhost names subdomain.conf.

You do not need to restart nginx server for config to take action, you only need to reload it : /usr/local/etc/rc.d/nginx reload

OR you can make only one conf file, where all vhosts should be set. This is probably better so that nginx doesn't need to load up 50 files, but only one....

IF you have problems with scripting, then ask question about that...

4
  • this kinda it, but not it at the same time. I run batch script and created 50+ subdomains user##.domain.com Now many users would want to create custom subdomains test.user##.domain.com and maybe one for release and one for share. Controlling this manually is pain. Should mention that each user's subdomain has own directory /webroot/domain.com/userXX/user01/ there I have www for main directory.
    – jM2.me
    Commented Dec 4, 2011 at 0:57
  • 1
    In short: I can manage subdomains for each user, but can't and dont want to manage their subdomains
    – jM2.me
    Commented Dec 4, 2011 at 1:07
  • Like Cees said, you can do everything with scripting. You can even put nginx reload in cronjob, so that user does not have control over nginx reload...
    – Glavić
    Commented Dec 5, 2011 at 15:08
  • 1
    How is this the accepted answer? This is not dynamic vhosting since that wouldn’t require any change in the config.
    – RemyNL
    Commented Dec 5, 2019 at 13:26
8

Based on user2001260's answer, later edited by partlov, here's my outcome.

Bear in mind this is for a dev server located on a local virtual machine, where the .dev prefix is used at the end of each domain. If you want to remove it, or use something else, the \.dev part in the server_name directive could be edited or altogether removed.

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    # Match any server name with the format [subdomain.[.subdomain...]].domain.tld.dev
    server_name ~^(?<subdomain>([\w-]+\.)*)?(?<domain>[\w-]+\.[\w-]+)\.dev$;

    # Map by default to (projects_root_path)/(domain.tld)/www;
    set $rootdir "/var/www/$domain/www";

    # Check if a (projects_root_path)/(subdomain.)(domain.tld)/www directory exists
    if (-f "/var/www/$subdomain.$domain/www"){
        # in which case, set that directory as the root
        set $rootdir "/var/www/$subdomain.$domain/www";
    } 

    root $rootdir;

    index index.php index.html index.htm index.nginx-debian.html;

    # Front-controller pattern as recommended by the nginx docs
    location / {
        try_files $uri $uri/ /index.php;
    }

    # Standard php-fpm based on the default config below this point
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }

}

The regex in server_name captures the variables subdomain and domain. The subdomain part is optional and can be empty. I have set it so that by default, if you have a subdomain, say admin.mysite.com the root is set to the same root as mysite.com. This way, the same front-controller (in my case index.php) can route based on the subdomain. But if you want to keep an altogether different application in a subdomain, you can have a admin.mysite.com dir and it will use that directory for calls to admin.mysite.com.

Careful: The use of if is discouraged in the current nginx version, since it adds extra processing overhead for each request, but it should be fine for use in a dev environment, which is what this configuration is good for. In a production environment, I would recommend not using a mass virtual host configuration and configuring each site separately, for more control and better security.

1
server_name ~^(?<vhost>[^.]*)\.domain\.com$;
set $rootdir "/var/www/whatever/$vhost";
root $rootdir;
1

As @Samuurai suggested here is a short version Angular 5 with nginx build integration:

server {
    server_name ~^(?<branch>.*)\.staging\.yourdomain\.com$;
    access_log /var/log/nginx/branch-access.log;
    error_log /var/log/nginx/branch-error.log;
    index index.html;
    try_files $uri$args $uri$args/ $uri $uri/ /index.html =404;
    root /usr/share/nginx/html/www/theft/$branch/dist;
}
0

Another alternative is to have includes a few levels deep so that directories can be categorized as you see fit. For example:

include sites-enabled/*.conf;
include sites-enabled/*/*.conf;
include sites-enabled/*/*/*.conf;
include sites-enabled/*/*/*/*.conf;
-1

As long as you are comfortable with scripting, it is not very hard to put together some scripts that will quickly set up vhosts in nginx. This slicehost article goes through setting up a couple of vhosts and does it in a way that is easily scriptable and keeps the configurations separate. The only downside is having to restart the server, but that's to be expected with config changes.


Update: If you don't want to do any of the config maintaining yourself, then your only 2 options (the safe ones anyways) would be to either find a program that will let your users manage their own chunk of their nginx config (which will let them create all the subdomains they want), or to create such a user-facing management console yourself.

Doing this yourself would not be too hard, especially if you already have the scripts to do the work of setting things up. The web-based interface can call out to the scripts to do the actual work so that all the web interface has to deal with is managing who has access to what things.

1
  • 1
    Please refer to comment on other answer
    – jM2.me
    Commented Dec 4, 2011 at 0:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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