In our last post, we explained that if you want to handle more than one domain using Nginx, you need to set up the Nginx Server block. We also explained half of the steps so if you haven’t checked that out yet, here is the link:
We completed setting up the Nginx server block for the first domain so let’s go and setup the second server block.
Setup the second Nginx server block
We need to do the exact same thing for the second domain. There is only going to be one difference through this process and it’s that we need to remove default_server from the listen directory in config file. This is because we can only have one default_server or else the webserver won’t work.
There is a default server block in this path:
Now we copy the file with a new name based on the domain:
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/mytest2.com
Then we need to edit the file:
sudo nano /etc/nginx/sites-available/mytest2.com
Scroll the file to find the “listen” section:
Remove default_server from the “listen” section:
Find the root /var/www/html in the mentioned file and replace it with the path of the directory you made earlier. For example /var/www/mytest2.com/public_html.
Now find the server_name _; line and change the domain name. (You can search for this line using Ctrl+W)
Save the changes and close the file. (Use Ctrl+X then Y to save changes)
Just like the other one let’s make sure the Nginx config file exists:
sudo nginx -t
You should see this as a result:
You are done setting up the second Nginx server block.
Creating a Symbolic Link
Now we are going to create a symbolic link from sites-available in the sites-enabled directory. This will technically call the Nginx web server.
sudo ln -s /etc/nginx/sites-available/mytest1.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/mytest2.com /etc/nginx/sites-enabled/
Remove the symbolic link from the default server:
sudo rm /etc/nginx/sites-enabled/default
sudo service nginx restart
Hope you enjoyed this post if you did, you can visit our blog for more!