NGINX Web Server - Subdomain Setup

Paulo Bazzo

Creating a Subdomain on Nginx Server

Creating a subdomain

The first step is to create a subdomain record on our DNS server. Your DNS is hosted by godaddy, squarespace, bluehost, cloudflare, etc..

DNS A records configuration on cloudflare for subdomain

Moving files to Nginx

You now want to move your application into your server, the easier way to do this is by secure copy over ssh

The syntax for the scp command is the following

After moving your application into your server we can proceed to the next step. On this example we are assuming you are moving a a website into the server not a different type of application or service.

 scp -r localdirectory/* user@server_ip:/var/www/subdomain_folder

Configure conf.d

cd /etc/nginx

Our website is located inside the folder sites available

Inside the file named default you can see what is the main root location of our website, this is the folder that will be serve when someone goes directly into our url

Root Nginx

However, this is not the folder we are after. We want to move inside the folder conf.d. The folder is empty, we need to create a new file here that will hold the .conf for your subdomain.

A good tip is to make the file name the same name of the subdomain you are creating, if you are creating a games domains for example create a file named

conf.d/subdomain Content

Inside the conf.d folder you want to create a file named with the following

          root location/of/your_app;
          index index.html;

Testing Configuration

Its very important to test your configuration is correct before you apply the changes to the server, otherwise your server will break! sp make sure you have a handy backup file of whatever file you decide to thinker with 🦖
To do test your configuration files run:

sudo nginx -t

After checking your configuration is correct you need to restart your nginx server for the changes to take effect.

sudo systemctl reload nginx


nginx -s reload

The index line above is options, Also, If your application is not a web server and you are running something like python you can change it to be index for example.


If all went well you should now be able to see your application running on your subdomain. Your /var/www root folder should have a folder for each subdomain you are running. It will look something like this.

root folder how it looks inside the var/www

You can also apply this technique for hosting multiple websites under one Nginx server 😃


ChrisCourses CameronMckenzie NginxDox