How to Configure/Serve Domains & SubDomains in NgInx server

Setup Records For Domain/SubDomain in Domain Provider(NameCheap/GoDaddy/Any)

The first step in the NgInx configuration is to check whether domain/subdomain is pointed to server(Hosting) address.

To point any Domain(www.tutuorialflow.com) to the hosting server, we need to configure server address in Manage DNS.

  1. Login to your Domain Provider, select your domain and click on “Manage DNS” or “Advance DNS”.
  2. Add “A” record to point your Domain to the Hosting Address.
    1. Select “A” in the Type Field
    2. Enter “@” in the Host Field, and Hosting server IP address in the value textfield.,
    3. Enter any recommended value in TTL field, and click the confirm and save Settings.

Like above, create “A” record to point your subdomain to the Hosting Address, so that when you try the subdomain name in browser, request will be sent to the hosting server.

  1. Select “A” in the Type Field
  2. Enter subdomain name, “chatservice” in the Host Field, and Hosting server IP address in the value textfield.
  3. Enter any recommended value in TTL field, and click the confirm and save Settings.

Repeat the above process to add any number of subdomains required.

Once the above setup is done, the configured domains/subdomains- e.g., (https://tutorialflow.com and https://chatservice.tutorialflow.com) when checked in browser will be reaching out the server IP address configured.

Installing NgInx server in any linux server

For installing NgInx, use the following commands step by step.

The above command will install the NgInx will simply install the NgInx in your server. Once installed you can check the version of NgInx using below command.

The commands to start/stop/restart the ngInx are below.

Start ngInx Server

Stop ngInx Server

Restart ngInx Server

Setup Directories for NgInx Domain/SubDomain in Hosting server

Once NgInx installed, the next setup is to modify the Configuration file and create our configuration.

In NgInx, the usual approach is to create configuration file for each domain, subdomain.

  1. Create separate .conf file for each domain/subdomain
  2. Place the configuration file in the configuration folder of ngInx
  3. Create a link to the configuration file

When NgInx started, all the conf files in the NgInx standard path will be included and NgInx will create routing for each configuration.

Configuration Path: /etc/nginx/sites-available/

For our example, we have to create 2 configuration files, one for (tutorialflow.com)domain & another for (chatservice.tutorialflow.com) subdomain.

  • Create a configuration file for tutorialflow.com & chatservice.tutorialflow.com and save it in the configuration path.
    • /etc/nginx/sites-available/tutorialflow.com.conf
    • /etc/nginx/sites-available/chatservice.tutorialflow.com.conf
  • Once the configuration is done, please create a link to this configuration from sites-enabled folder of NgInx as below.
    • sudo ln -s /etc/nginx/sites-available/tutorialflow.com.conf /etc/nginx/sites-enabled/tutorialflow.com.conf
    • sudo ln -s /etc/nginx/sites-available/chatservice.tutorialflow.com.conf /etc/nginx/sites-enabled/chatservice.tutorialflow.com.conf

Hence the when you list the configuration files in ngInx folder it will list down like below.

Configure NgInx(Conf), for Domain/SubDomain

Now we have created conf files., next step is to configure the domains/subdomains and point to the local html page/application urls.

File: /etc/nginx/sites-available/tutorialflow.com.conf

In the above server configuration, we have configured the server_name which shows the domain names, when invoked, automatically the URL(pages) present in the proxy_pass will be reached.

We can point directly to the html files, if we use configuration like below.,

For the Subdomain(https://chatservice.tutorialflow.com), we have to use the .conf similar like above, only difference we can change the server_name to subdomain, and proxy_pass we can point to another webapplication running on different port or different url.

Once we done with above configuration files, for both domain and subdomains,

Restart the NgInx Server.

Now try to access the urls https://<domain>.com and https://chatservice.<domain>.com.

It should be showing the pages whatever we configured in the proxy_pass in the conf files.

We can always the check the status of NgInx using the below command., below which shows service is running fine.

Leave a Comment

Your email address will not be published. Required fields are marked *