
Using Nginx Server Blocks 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 create a separate security policy for each site and use different SSL certificates for each site and much more. In this guide, we will learn how to set up Nginx Server Blocks on a Debian 9.
Create Nginx Server Blocks On Debian
Nginx Server Blocks are once kind of Virtual Hosts as in Apache.
Prerequisites
- Logged in on a Debian 9 server with a non-root user with sudo privileges.
- Nginx should installed and configured, as shown in How to Install Nginx on Debian 9 Server.
- A domain name should pointing to your server IP address.
Create the Directory Structure
At 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 but it is best practice to define in directory structure. So 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>
All above commands run as sudo user so root user is owner of all new created files and directories. We need to change ownership of document root directories to avoid permission issue later for regular user. So our regular user can modify files in our web directories without any problems.
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 give any names to your configuration file but it’s best 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
You learned how to create an Nginx server blocks to host multiple website on a single Debian machine. You can repeat the steps given above and create additional server blocks for all your domains.
If you have any question, feel free to leave a comment.
Leave a Reply