configure nginx for multiple subdomains

October 11, 2016 121 views
Nginx PHP Frameworks PHP Ubuntu 16.04

Hello all,
im sure ill find some helpful buddies here.

How can i maintain multiple subdomains in nginx with same configuration. in dept, we want to host a SAAS based app where client will have their own site hosted on our subdomains. eg. site.mydomain.com, site2.mydomain.com etc. dose it require individual server block for each subdomain? or i can point all in one server block? i must add the configuration for nginx will be same for all.

also could somebody point me to any script that automate this process? i really appreciate you all for your help.

1 Answer


It mostly depend on you app. If every user have their own document root you should go with individual server block for each subdomain. Even if it's not the case, it is recommended.

How to setup Nginx Server Blocks will explain you how to make it.

In addition to that, you could utilize Nginx snippets. It allows you to store configuration in one file and use it in multiple blocks.

Snippets are stored in /etc/nginx/snippets and usually have extension .conf.
There you can write any configuration that you want to use under server block.

When you want to use it in block, simple include snippets/example.conf will use it in block.


root /var/www/html
server {
include snippets/example.conf

For automatization Bash script can do job. Or something more complex like Ansible

When using the Nginx web server, server blocks (similar to the virtual hosts in Apache) can be used to encapsulate configuration details and host more than one domain off of a single server. In this guide, we'll discuss how to configure server blocks in Nginx on an Ubuntu...
  • thanks a lot mate for the reply.
    could you give me some details about this part?

    For automatization Bash script can do job. Or something more complex like Ansible

    thanks again in advance.

    • You have multiple solutions, I will give you some and case uses of.


      Situation 1: You only change server name (and possibly document root).

      You can make script that write file from scratch or write template, copy it with desired name and change needed parameters.
      Let's say we will go with template.
      I will create template in /etc/nginx/sites-available/template:

      server {
              listen 80;
              listen [::]:80;
              root _;
              index index.html index.htm index.nginx-debian.html;
              server_name _;
              include snippets/example.conf;

      I will make script that takes two arguments: server name and document root. It will copy template to srvname.example.com and script will execute sed command to update values. sed (stream editor) is very powerful command-line text editor, it can be used to update values in files and much more. You can learn more use cases in sed GNU docs.

      cp /etc/nginx/sites-available/template /etc/nginx/sites-available/$domain
      sed -i -e 's/server_name _/server_name '$domain'/g' /etc/nginx/sites-available/$domain
      sed -i -e 's/root _/root '$root'/g' /etc/nginx/sites-available/$domain
      ln -s /etc/nginx/sites-available/$domain /etc/nginx/sites-enabled/
      systemctl restart nginx

      We used here Bash command-line arguments. When you call script with ./script.sh x y you can get x and y by $1 and $2.
      First sed will try to find server_name _ and change it to server_name $domain.
      Second one will do the same but now for root In case you don't need it, you can just remove that line and root=$2 line.
      ln will create symbolic link to sites-enabled which will enable your site.

      This is easiest solution. I can write you script that will make config from scratch but I think template will be best.

      About Ansible. It's configuration management system. Follow link to learn more about it, you can also take a look at Ansible tutorial by Erika Heidi.

      If you need more help, feel free to ask, I will try to help you :)

      As a broader subject, configuration management (CM) refers to the process of systematically handling changes to a system in a way that it maintains integrity over time. Even though this process was not originated in the IT industry, the term is broadly used to refer to **server configuration management**.
      • @xMudrii
        your the man. :)

        ill definitely contact you if i need further assistance. thanks a lot brother....

        • I forgot part how to run script...
          Anyways, script must be run with sudo because basic user can't write into /etc/nginx and can't restart nginx.

          • sudo sh create-site.sh domain docroot

          domain can be for example - subdomain.example.com while root can be for example /var/www/subdomain.example.com.

          No problem, glad to help, if you need help with it, fell free to ask me :)

          • i think i dont need individual web root for my purpose. only subdomain will do.
            one thing i forgot to ask, is it necessary to restart nginx each time or simply reload will work.

        • @newbie I'm not able to reply to latest comment so I will here.

          systemctl reload nginx will do the job. It will reload configs, new subdomain will work and there will be zero downtime. Maybe some very very rare cases can occur when restart is need, but in 99.9% of time reload does its job, and I think it is very safe to it.

          • @xMudrii
            Thank you so much brother for all your kind help. ill work on this and get back to you when its done. it might take some days coz some other works are on line but ill surely get back to you to let you know the result.

            really really appreciate your detail help.

Have another answer? Share your knowledge.