How to Manage Nginx Server Blocks on Ubuntu 20.04

How to Manage Nginx Server Blocks on Ubuntu 20.04

Server blocks are Nginx directives that define settings for specific domains, allowing you to run more than one website on one server. For each website, you can set the site document’s root (the directory containing website files), create separate security policies, use various SSL certificates, and more.

This article explains how to set up a Nginx server block on Ubuntu 20.04.


Make sure you meet the following requirements before continuing:

  • The domain name points to your public IP server.
  • Nginx is installed on your Ubuntu system.
  • You are logged in as root or as a user with sudo rights.

In some articles, the term “Server Block” is referred to as “Virtual Host”. Virtual host is an Apache term.

Creating a Directory Structure

A document root is a directory where web site files for domain names are stored and presented in response to requests. You can set the document root to whatever location you want. In this example, we will use the following directory structure:

│   └── public_html
│   └── public_html

Each domain hosted on the server will have the document root set to / var / www / / public_html.

Start by creating a root directory for the domain:

sudo mkdir -p /var/www/

We will also create an index.html file in the domain document’s root directory which will be displayed when you visit the domain in your browser:

<!DOCTYPE html>
<html lang="en" dir="ltr">
    <meta charset="utf-8">
    <title>Welcome to</title>
    <h1>Success! home page!</h1>

Because the above command is run as sudo user, the newly created file and directory are owned by root. To avoid permission issues, change ownership of the domain document’s root directory and all files in the directory to the Nginx user (www-data):

sudo chown -R www-data: /var/www/

Creating a Server Block

On Ubuntu systems, the Nginx server block configuration file is in the / etc / nginx / sites-available directory. They can be activated by creating symbolic links to the / etc / nginx / sites-enabled directory, which Nginx reads at startup.

Open your text editor and create the following server file block:

server {
    listen 80;


    root /var/www/;

    index index.html;

    access_log /var/log/nginx/;
    error_log /var/log/nginx/;
  • server_name: Domain must match this server’s configuration block.
  • root: The directory where Nginx will present domain files.
  • access_log, error_log: Specifies the location for the log file.

The configuration file can be named whatever you want, but usually, it is best to use a domain name.

To activate a new server block file, create a symbolic link from the file to the site-active directory, which Nginx reads at startup:

sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/

Test the Nginx configuration for the correct syntax:

sudo nginx -t

If there are no errors, the output will look like this:

Output :

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Restart the Nginx service for the changes to take effect:

sudo systemctl restart nginx

Finally, to verify that the server block works as expected, go to in the browser of your choice, and you will see something like this:



We have shown you how to create a Nginx server block and host multiple domains on one Ubuntu server. You can repeat the steps outlined above and create additional server blocks for all your domains.

If you encounter a problem, please leave a comment.

Related posts

How to Set Up WireGuard VPN on CentOS 8


How to Install Android Studio on Ubuntu 20.04


How to Set an SSH Key on Ubuntu 20.04


How to Install Puppets on CentOS 8 / RHEL 8


How to Install the SNMP Cacti Monitoring Tool on Debian 10


How to install Lightworks on Ubuntu


How to make a POST request with cURL


How to Download a Linux ISO File via a Torrent Client


How to compile the ONLYOFFICE Document Server from source code on Ubuntu