I’m a big fan of nginx (way better than IIS). If you’re not running a website that’s getting a ton of traffic, you might want to run ANOTHER website on the same server, without taking down your other website. More specifically, you have FIRSTDOMAIN.com running on your server and you’re now looking to implement SECONDDOMAIN.com. If you’re new to nginx, it might be a bit confusing how to set this up at first. That’s ok because that’s what this article is for!

For the sake of the article, we are going to use FIRSTDOMAIN.com as the domain that already has been setup, and SECONDDOMAIN.com as the second domain we are NOW looking to implement.

We’re going to start by assuming you already SSH’d into your server. I’m also going to assume you already have SECONDDOMAIN.com’s A (or @) record pointed at your web server. To clarify, both FIRSTDOMAIN.com’s A record and SECONDDOMAIN.com’s A record should be pointed at your web server (your web server’s static public IP).

Once you’re SSH’d in run (replace SECONDDOMAIN with your own domain name):

cd /etc/nginx/sites-enabled
touch SECONDDOMAIN

What did we just do? We just created our config file for our second website with nothing in it. If you already have a site up, you have already did this once before. All we are doing is just creating another config (each new site will have it’s own config).

Before we add anything to our new config (SECONDDOMAIN), we will want to create a symlink to the same file in sites-available. We don’t need to “touch” that file in sites-available. We can just create the symlink.

Run:

ln -s /etc/nginx/sites-enabled/SECONDDOMAIN /etc/nginx/sites-available/SECONDDOMAIN

Next, make sure you are still in your sites-enabled dir and run (replacing SECONDDOMAIN with your domain):

nano SECONDDOMAIN

We will now want to add the following to our config file. Make sure you replace SECONDDOMAIN.com with your own domain name and your path to your code:

server {
        listen 80;
        root /var/www/html/SECONDDOMAIN;
        index index.html index.htm;

        server_name SECONDDOMAIN.com www.SECONDDOMAIN.com;
}

Next we need to edit our hosts file. Make sure to replace 8.8.8.8 with your server’s IP and seconddomain with your domain.

nano /etc/hosts

#ADD THIS TO YOUR HOSTS FILE. MAKE SURE TO REPLACE IP WITH YOUR SERVER'S PUBLIC IP AND SECOND DOMAIN WITH YOUR DOMAIN
8.8.8.8 SECONDDOMAIN.com www.SECONDDOMAIN.com

Finally, restart nginx.

nginx -s reload

Next, go to your website and test it out!

nginx second domain

Note: If you get a nginx error in your browser, you set up everything correctly. You just have to correct the path to your code!