In this article I will try to help you to setup multiple domains to point to a single Nginx server running multiple local servers.

Step 1:

We have to install the Nginx server. I will use an Ubuntu machine:

$ sudo apt-get update 
$ sudo apt-get install nginx

Step 2:

Now we have to configure the Nginx server. Lets create a new config file:
$ vim /etc/nginx/sites-enabled/example.conf

In this config file we have to specify how the Nginx server to redirect the incoming requests to the locally running servers on different ports.

For the needs of the article, we will have 2 servers running on:

So, our example.conf should contains this basic configuration:

// First domain configuration
server {
    server_name [DOMAIN];
    root [LOCAL DIR];
    
    location / {
         proxy_set_header   X-Real-IP $remote_addr;
         proxy_set_header   Host      $http_host;
         proxy_pass         http://localhost:80;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_set_header Host $http_host;
         proxy_set_header X-Forwarded-Proto $scheme;
         proxy_buffering off;
    }
}

// Second domain configuration
server {
    server_name [DOMAIN];
    root [LOCAL DIR];
    
    location / {
         proxy_set_header   X-Real-IP $remote_addr;
         proxy_set_header   Host      $http_host;
         proxy_pass         http://localhost:81;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_set_header Host $http_host;
         proxy_set_header X-Forwarded-Proto $scheme;
         proxy_buffering off;
    }
}

Where:

  • DOMAIN - should contain the www and non-www domain names separated by single interval, i.e.:
    server_name www.example.com example.com
  • LOCAL DIR - the absolute path to your local server's directory, i.e.:
    /var/www/example

After we save the example.conf file, we have to execute the following commands:

$ rm /etc/nginx/sites-available/default 
$ ln -s /etc/nginx/sites-available/example.conf /etc/nginx/sites-enabled/example.conf

And restart the Nginx service:

$ sudo systemctl restart nginx

Step 3

The last step is to configure our domains' DNS configurations. Open your provider's control panel and go to the domain DNS settings.

The domain configuration will be the same for both of our domains - www.example-one.com and www.example-two.com:

  1. Create or edit your A-Type record where:
    1.1. The Name value should be: @
    1.2. The Host value should be: YOUR SERVER'S PUBLIC IP
    1.3. The TTL should be any value suits you, for example 1/2 hour
  2. Create or edit your CNAME-Type record where:
    1.1. The Name value should be: www
    1.2. The Host value should be: @
    1.3. The TTL should be any value suits you, for example 1/2 hour

And that is all!
It's not that difficult to setup it, right?

My first try was to redirect the domains to the server IP's with different ports but it was not possible. The DNS configuration accepts only IP addresses without specified ports.