In our last tutorials, we have discussed how to install Nginx on Ubuntu using the default repositories as well from the source. In this tutorial, we will discuss how we can host multiple websites on Nginx webserver.
Similar to how we have virtual hosts on Apache to host multiple websites on apache, Nginx has Server Blocks to host multiple websites on Nginx.
By default, Nginx has a single website server block to host the default webpage available in /var/www/html. the default document directory for Nginx. Now let’s discuss how we can host multiple websites with Nginx by using multiple server blocks.
Also Read: Create a SELF-SIGNED SSL Certificate in Linux
Creating server blocks on Nginx
The first thing we need to do is to create a separate document directory for each of the websites that we will host. We will be hosting 2 more websites, so we will create 2 more document directories for those websites,
$ sudo mkdir /var/www/example1
$ sudo mkdir /var/www/example2
We can also use some other directory, other than ‘/var/www’ to create the document directories, but we using ‘/var/www’ is for ease of management.
Next, we need to create ‘Index.html’ page for these example websites,
$ sudo vi /var/www/example1/index.html
<html>
<head>
<title>This is Example1.com!</title>
</head>
<body>
<h1>EXAMPLE 1 WEBSITE IS WORKING</h1>
</body>
</html>
Similarly, create index.html file for second website as well,
$ sudo vi /var/www/example2/index.html
<html>
<head>
<title>This is Example2.com!</title>
</head>
<body>
<h1>EXAMPLE 2 WEBSITE IS WORKING</h1>
</body>
</html>
Next, we will create server blocks to host multiple websites on the Nginx server. To make configurations, go to the following folder,
$ cd /etc/nginx/conf.d
We will now create 2 more configuration files for hosting our websites,
$ vi /etc/nginx/conf.d/example1.conf
server {
listen 80;
listen [::]:80;
root /var/www/example1;
index index.html index.htm index.nginx-debian.html;
server_name example1.com www.example1.com;
location / {
try_files $uri $uri/ =404;
}
}
Similarly create file for second websites,
$ vi /etc/nginx/conf.d/example2.conf
server {
listen 80;
listen [::]:80;
root /var/www/example2;
index index.html index.htm index.nginx-debian.html;
server_name example2.com www.example2.com;
location / {
try_files $uri $uri/ =404;
}
}
All we now need is to reload or restart the nginx server to implement the changes made to nginx server & host multiple websites on nginx,
$ sudo sytemctl restart nginx
or
$ sudo systemctl reload ngnix
That’s it, we can now access the websites from the browser. Now to access the web server using the URL hostname, we need to add the web URL in the local DNS server but we can also use the host entries in the file ‘/etc/hosts’ to access websites with the hostname.
Create host entries in ‘/etc/hosts’,
To create hosts file,
$ sudo vi /etc/hosts
192.168.100.100 example1.com example2.com
Save the file & exit. Here ‘192.168.100.100’ is the IP address for the Nginx webserver. This entry needs to create on all the servers from which we have to access the website.
Now open the web browser & access the websites. This completes our tutorial on how to host multiple websites on Nginx using the server blocks. Please feel free to send in any questions or queries using the comment box below.