
Using Nginx server block you can run more than one website on a single machine. You can specify the separate document root for each domain. In addition, you also can set a separate security policy and use different SSL certificates for each site and much more. In this tutorial, you will learn how to set up Nginx Server Blocks on a Ubuntu 20.04 Focal Fossa.
Create Nginx Server Blocks On Ubuntu 20.04
Nginx server blocks are same as virtual hosts as in Apache.
Prerequisites
- Logged in on a Ubuntu server with a root or user with sudo privileges.
- Nginx must installed and configured, as shown in How to Install Nginx on Ubuntu 20.04 Server.
- A domain name should pointing to your server IP address.
Create the Directory Structure
First, we will create a directory structure to store the site data and serve response to visitor’s requests.
The top level directory is considered as DocumentRoot
directory. You can set any location as document root as per your choice. It is best practice to define in directory structure. We will store all document root at /var/www
directory.
/var/www/
├── example1.com
│ └── public_html
├── example2.com
│ └── public_html
We will create separate directory for each domain inside /var/www
directory. Within this directory, we’ll create a public_html
directory as domain document root directory to store website data.
sudo mkdir -p /var/www/example.com/public_html
After that, we will create a index.html
file within the domain document root directory for testing purpose. By default, this page will display when visitors visit your website.
You can create a new index.html
file using your favorite text editor type:
sudo nano /var/www/example.com/public_html/index.html
Add the following lines into it:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Welcome!!</title>
</head>
<body>
<h1>Great! example.com set up completed!</h1>
</body>
</html>
We need to change ownership of document root directories to avoid permission issue later for regular user.
sudo chown -R www-data: /var/www/example.com
Creating a Server Block
In Debian systems, default location for Nginx server blocks configuration files is at /etc/nginx/sites-available
directory. Those can be used by enabling through symbolic links to the /etc/nginx/sites-enabled/
directory.
Create a new file for example.com using your choice text editor by typing :
sudo nano /etc/nginx/sites-available/example.com.conf
Now, add the following lines in to this file:
server {
listen 80;
listen [::]:80;
root /var/www/example.com/public_html;
index index.html;
server_name example.com www.example.com;
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
location / {
try_files $uri $uri/ =404;
}
}
You can define any name to your configuration file but it’s good practice to give file name same as domain name.
Next, We need to enable nginx server block by making symbolic link at /etc/nginx/sites-enabled/
directory. Run the below command to create symbolic link:
sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/
To test the Nginx configuration for correct syntax by typing:
sudo nginx -t
It will show following output if there is no error.
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
You must restart the Nginx service to take effect. Execute below command:
sudo systemctl restart nginx
Finally, to verify the server block is working as expected open http://example.com
to your web browser and it should show you as following :

Conclusion
We have shown how to create Nginx server blocks to host multiple website on a single Ubuntu 20.04 server. Repeat the steps to create multiple server blocks for all your domains.
If you have any question, feel free to leave a comment.
Leave a Reply