How To Set Up Nginx Server Blocks on Ubuntu 22.04

Published on

4 min read

Set Up Nginx Server Blocks on Ubuntu 22.04

Nginx pronounced “engine x” is an open-source, high-performance HTTP and reverse proxy server responsible for handling the load of some of the largest sites on the Internet. It can be used as a standalone web server, load balancer, content cache, and reverse proxy for HTTP and non-HTTP servers.

A server block is an Nginx directive that defines settings for a specific domain, allowing you to run more than one website on a single server. For each website, you can set the site document root (the directory which 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 22.04. You’ll learn how to set up multiple websites on a single server and customize each site’s settings to suit your needs.


Ensure that you have met the following requirements before continuing:

It is worth noting that in certain articles related to web servers, the term “Server Blocks” is referred to as a “Virtual host”. A virtual host is an Apache term.

Creating the Directory Structure

The document root is the directory where the website files for a domain name are stored and served in response to requests. You can set the document root to any 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 its document root set to /var/www/<domain_name>/public_html.

Start by creating the root directory for the domain:

sudo mkdir -p /var/www/

Next, create an index.html file and place it in the domain’s root directory. This file will be displayed as the default page when you access the domain URL in your web browser.

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

When executing the commands as a sudo user, the newly created files and directories will be owned by the root user. To avoid any permission issues, change the ownership of the domain document root directory and all files within the directory to the Nginx user (www-data) :

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

Creating a Server Block

On Ubuntu systems, the configuration files for Nginx server blocks are stored in the /etc/nginx/sites-available directory. They can be enabled by creating symbolic links to the /etc/nginx/sites-enabled directory, which Nginx reads during the 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 will match this server block configuration.
  • root: The directory from which Nginx will serve the domain files.
  • access_log, error_log: Specifies the location for log files.

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

To enable the new server block file, create a symbolic link from the file to the sites-enabled directory, which Nginx reads during startup:

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

To ensure that the syntax of Nginx configuration is correct, perform a test:

sudo nginx -t

If there are no errors, the output will look 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 verify that the server block is working as expected, open in your browser of choice, and you will see something like this:


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

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