Virtual Hosts is a feature of NGINX that allows you to host multiple web sites on the same server. In this guide, you’ll learn how to set up multiple NGINX Virtual Hosts on Ubuntu 16.04 and how to serve different content to different visitors depending on which domains they are requesting.
This guide has been tested on Ubuntu 16.04, however it may work on older/newer versions of Ubuntu and different flavours of Linux.
Once you have installed NGINX we are ready to begin.
In this guide, we will make two virtual hosts that will be accessible via the domains test1.com and test2.com. These domains will be referenced throughout the guide but you should substitute them for your own values while following along.
Step 1: Create Directories
The first thing we need to do is create a directory for each virtual host to store the different website content in. We will place the directories in /var/www and we will name them after each domain name.
Run the following commands to create the directories:
Remember to replace the name of each directory in green with your domain names.
Step 2: Create NGINX Virtual Hosts
The NGINX Virtual Hosts configurations are stored in /etc/nginx/sites-available on Ubuntu operating systems. You will see inside this folder a default configuration file called default. This configuration contains the mapping to the local directory at /var/www/html and is used for serving content via the hosts IP address or hostname.
To create a new Virtual Host, create a new file at /etc/nginx/sites-available/test1.com.conf with your favourite text editor:
sudo vim /etc/nginx/sites-available/test1.com.conf
Now add the following configuration to the file (remembering to change test1.com to your domain name):
Repeat the above step for test2.com and any extra domains you want to host on the server.
This configuration is as basic as you can get. With this config, NGINX will listen to requests on port 80 made to http://test1.com and will serve content located in /var/www/test1.com/html. We now need to create a default HTML page and enable the virtual host.
Step 3: Create Demo HTML Pages
For demonstration purposes let’s just create some simple HTML pages for each of our NGINX Virtual Hosts.
Create the first index.html page for test1.com with your favourite editor:
sudo vim /var/www/test1.com/html/index.html
Add the following HTML:
<title>Welcome to Test1.com!</title>
<h1>Success! The Test1.com NGINX Virtual Host is working!</h1>
Repeat the above step for test2.com replacing the test1.com with test2.com.
Step 4: Enable the Virtual Hosts
The last thing we need to do before testing is to enable the virtual hosts and then apply the NGINX configuration. Enabling NGINX Virtual Hosts is done by symbolic linking the configuration files inside the /etc/nginx/sites-available folder to the /etc/nginx/sites-enabled folder.
Run the following commands to create the symbolic links:
Now apply the changes to NGINX by running the following command:
sudo nginx -s reload
There should be no errors after running the reload command if the configuration files are OK. We are now ready to test the sites.
Step 5: Testing the Virtual Hosts
In order to test the virtual hosts you will first need to point your domain name A records at the IP address of the Ubuntu server. If you are using test domains that you do not have access to, you can use the hosts file of your local machine to map the test domains to your server.
On OS X and Linux operating systems the hosts file is located at /etc/hosts and on Windows it is located at C:\Windows\System32\drivers\etc\hosts. The following image shows a screenshot of my modified hosts file on OS X:
With the hosts file modified we should now be able to browse to each website at http://test1.com and http://test2.com.
Open a browser and then enter the domain name:
You should see the following output:
Now do the same with:
You should see the following output:
In this tutorial, we have successfully created two NGINX Virtual Hosts so that we can run two or more websites on the same Ubuntu server. The NGINX configurations were really basic and are only capable of serving static files. NGINX can do a lot more and in a future article we will take a look at how to host a dynamic website like WordPress with NGINX.