Host multiple domains under the same server IP

How to host multiple, different domains under the same server IP using Nginx

How to host multiple, different domains under the same server IP using Nginx.

Prerequisites

  • Root access to the web server to update the nginx.conf file.
  • Unique public folders for each website (i.e. /var/www/mydomain1, /var/www/mydomain2)

Instructions

  1. Log into your web server via SSH.

  2. Edit the /etc/nginx/nginx.conf file and update it with 2 server { } block directives:

     server {
         listen 80;
         root /var/www/mydomain1;
         index index.html;
         server_name mydomain1.com;
     }
     server {
         listen 80;
         root /var/www/mydomain.com; 
         index index.html;
         server_name mydomain2.com;
     }
  3. Save the nginx.conf file

  4. Reload the configuration file to test the web server:

     sudo nginx -t
  5. Restart Nginx:

     sudo systemctl restart nginx