configure nginx for multiple subdomains

Posted October 11, 2016 27.3k views
NginxPHPPHP FrameworksUbuntu 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., 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.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
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

by Justin Ellingwood
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 16.04 server.
  • 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 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 ./ 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 :)

      by Erika Heidi
      As a broader subject, configuration management (CM) refers to the process of systematically handling changes to a system in a way that allows the system to maintain integrity over time. In this tutorial, we will discuss how configuration management works for servers, and what to consider when choosing a tool for building your configuration management infrastructure.
      • @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 domain docroot

          domain can be for example - while root can be for example /var/www/

          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.

      • Even if this is a bit old, i find it very usefull, thanks for that, i would have a question, if by chance you were still around :)

        in the server template i would also needs a location/ with a variable for the port, example:

        location / {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-NginX-Proxy true;
                proxy_pass http://localhost:3000/;
                proxy_ssl_session_reuse off;
                proxy_set_header Host $http_host;
                proxy_cache_bypass $http_upgrade;
                proxy_redirect off;

        Where 3000 would be different for each subdomain (nodejs app running on different ports)

        Do i simply replace 3000 in the template with location_port and add in bash script:



        sed -i -e 's/location_port/'$port'/g' /etc/nginx/sites-available/$domain

        Then call:

        sudo sh domain docroot port

        Or its more complicated than that?