Host multiple domains under the same server IP

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

You can host multiple, different domains under the same server IP using Nginx easily.

Host multiple domains under the same server IP

Prerequisites

  • Root access to the web server to update the nginx.conf file.
  • Unique public folders for each website (ie. /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 { } blocks:

     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 file

  4. Reload the configuration file to test it:

     sudo nginx -t
  5. Restart Nginx:

     sudo systemctl restart nginx