How to set up Nginx server blocks on Ubuntu 20.04

s2 0

A server block is an Nginx statement that defines settings for a particular domain and allows you to run more than one website on a single server. For each website, you can set the Site Document Root (the directory that contains the website files), create a separate security policy, use different SSL certificates, and much more.

This article describes how to set up Nginx server blocks on Ubuntu 20.04.


Make sure you meet the following requirements before proceeding:

  • Domain name pointing to your public server IP.
  • Nginx installed on your Ubuntu system.

  • You are logged in as root or as a user with sudo rights.

In some articles, the term “server blocks” is referred to as “virtual host”. A virtual host is an Apache term.

Create the directory structure

The document root is the directory in which the website files for a domain name are stored and made available in response to requests. You can set the document master to any storage location. In this example we use the following directory structure:

│   └── public_html
│   └── public_html

For each domain hosted on the server, the document root directory is set to /var/www/<domain_name>/public_html.

First, create the root directory for the domain:

sudo mkdir -p /var/www/

We also create one index.html File in the root of the domain document that appears 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>

Since the above commands are run as the sudo user, the newly created files and directories are owned by root. To avoid permissions problems, change ownership of the domain document root directory and all files in the directory to the Nginx user (www-data):

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

Create a server block

On Ubuntu systems, the configuration files of the Nginx server block are located in /etc/nginx/sites-available Directory. They can be activated by adding symbolic links to the /etc/nginx/sites-enabled Directory that Nginx read on startup.

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

server {
    listen 80;


    root /var/www/;

    index index.html;

    access_log /var/log/nginx/;
    error_log /var/log/nginx/;
  • server_name: The domains that should match for this server block configuration.
  • root: The directory from which Nginx will host the domain files.
  • access_log, error_log: Specifies the location for log files.

The configuration file can be named anything, but it is usually best to use the domain name.

To activate the new server block file, create a symbolic link from the file to the sites-enabled Directory that Nginx read on 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 looks like this:

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 check that the server block is working as expected, open in your browser of your choice and you will see something like this:


We showed you how to create Nginx server blocks and host multiple domains on a single Ubuntu server. You can repeat the steps above and create additional server blocks for all of your domains.

If you have any problems, please feel free to leave a comment.