Automatically create users subdomain with sub-folders in DO with NGINX

March 31, 2015 12.1k views
Nginx Scaling Networking DNS DigitalOcean Control Panels PHP

I need to automatically create a user subdomain when the user signup to a new account. Each user will have a different folder. For example.

/html/user1
/html/user2

Should work like this:

user1.domain.com
user2.domain.com

Should be great if just creating a new folder will be enough to automatically make it work and available the subdomain.

We are using nginx for having better performance with our app.

How we can accomplish this?, need to change something in the DNS also?

Step by step answer will be very appreciate.
Thanks!

9 Answers

To add a new subdomain you will want to:

1.) Create the new subdomain in the DNS section of the control panel and point it to your droplet's IP address.

2.) Create your new folder on your droplet and load your web files to that location.

3.) Create a new file in /etc/nginx/sites-enabled for your new subdomain. You can use /etc/nginx/sites-enabled/default as a template. This guide will help with the specifics.

4.) Restart Nginx for your changes to take effect:

service nginx restart
by Etel Sverdlov
This tutorial shows you how to set up a new directory, how to grant the correct permissions, how to add content to a new page, how to turn on virtual hosting, and how to, if needed, set up local hosts. Virtual Hosts are used to run more than one website or domain off of a single server and IP address. When used with nginx, virtual hosts are called Server Blocks. This tutorial covers setting up server blocks on an Ubuntu server.
  • Another questions,

    1- always is necessary to restart nginx after creating a new subdomain, or there is an option for not restarting each time?

    2- need to create a new configuration file for each subdomain each time a user is created?, if I want to have 1.000 users, will be 1.000 files?

  • 1.) You should be able to tell nginx to reload the configuration without restarting by using:

    service nginx reload
    

    2.) If you plan for each user to have a web root directory of their own this would be necessary. While you can manage your virtualhosts all within one file by adding additional server {} blocks this can very quickly get messy with that many subdomains. For this reason having individual files (even if there are hundreds) is the easier way to manage this.

    You can save some time however on the DNS side. If you plan to have all the subdomains of your domain pointed to the same droplet and then simply manage them within nginx you can create a wildcard DNS entry to point any subdomain that is encountered to your server. To do this, simply create the following record:

    *     CNAME     @
    

    The @ record is set up as your main domain without a subdomain and should be pointed to your droplet. The * indicates a wildcard and will match any subdomain and direct it to the same location as your existing @ record.

  • Thank you very much for this amazing information I was looking all around Internet for doing this on nginx and was not luck until now.

    What do you think about my setup?, do you have a better idea? Maybe for improving the setup process? It's a good decision to use nginx?

    I am also considering starting with a droplet just for MySQL and another for the app and then create a private network for better load balancing.

  • Without knowing the details of your app this does sound like a good method to create your user accounts as long as you don't plan to offer your users direct SFTP/FTP/SSH access to their directory. For that you'd need to take some additional steps to restrict the user.

    For an app where the user will interact completely via a web interface there are two main methods of doing something like this:

    1.) The method you are working on where each user has their own nginx server block configured for their subdomain pointing to a different directory.

    2.) Having a single nginx site configured and handling the different subdomain names completely within your script by directing traffic through your script. In this instance the script would detect the domain and serve different content based on the name.

    If you can provide some more details on your app/service I'd be happy to provide some further suggestions =)

thanks, but is possible to automatize this process?

Hello marcel.acunis! What you are looking to do is possible.
Since you are using PHP check DigitalOceanV2 for PHP to create the subdomains. To create the folders you have to figure out something by your self I guess. It will be more like a script to be executed when a new user signs up and to create a configuration file of the subdomain for nginx.

  • Another questions,

    1- always is necessary to restart nginx after creating a new subdomain, or there is an option for not restarting each time?

    2- need to create a new configuration file for each subdomain each time a user is created?, if I want to have 1.000 users, will be 1.000 files?

Another questions,

1- always is necessary to restart nginx after creating a new subdomain, or there is an option for not restarting each time?

2- need to create a new configuration file for each subdomain each time a user is created?, if I want to have 1.000 users, will be 1.000 files?

  • Sorry for the late reply. You will have just to reload nginx, not restart, after a new configuration file is created.
    Depending on what you have in mind you can create a configuration and add something like this to support all subdomains.

    server {
      server_name ~^(.*)\.example\.com$ ;
      root www/pub/$1;
      access_log /var/log/nginx/$1.access.log;
    }
    

Do you have skype or a private mail for tell you more about my proyect?

I need each company to have their own intranet to chat and share files with their stuff.

Have another answer? Share your knowledge.