Read in 3 minutes

How To Set Up Nginx Server Blocks on Ubuntu 18.04

Nginx Server Blocks allows you to run more than one website on a single machine. With Server Blocks, you can specify the site document root (the directory which contains the website files), create a separate security policy for each site, use different SSL certificates for each site and much more.

In this tutorial, we’ll provide a step by step instructions about how to set up Nginx server blocks (similar to Apache Virtual Hosts) on Ubuntu 18.04.


Make sure that you have met the following prerequisites before continuing with this tutorial:

  • You have a domain name pointing to your public server IP. In this tutorial we will use
  • You have Nginx installed by following this instructions.

Create the Directory Structure

Document root is the directory where the website files for a domain name are stored and served in response to requests. We can set the document root to any location we want but in this guide we will use the following directory structure:

│   └── public_html
│   └── public_html
│   └── public_html

Basiacly we will create a separate directory for each domain we want to host on our server inside the /var/www directory. Within each of these directories, we will create a public_html directory that will store the domain website files.

Let’s create the root directory for our domain

sudo mkdir -p /var/www/

For testing purposes we will create an index.html file inside our domain document root directory, Open your editor and create the index.html file with the following contents:

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

Create the Server Block

By default on Ubuntu systems Nginx server blocks configuration files are stored in /etc/nginx/sites-available directory , which are enabled through symbolic links to /etc/nginx/sites-enabled/ directory. Open your editor of choice and create the following basicconfiguration file:

server {
    listen 80;
    listen [::]:80;

    root /var/www/;

    index index.html;


     access_log /var/log/nginx/;
     error_log /var/log/nginx/;

    location / {
        try_files $uri $uri/ =404;

You can name the configuration file as you like but usually it is best to use the domain name.

To enable the new server block file we need to create a symbolic link from the file to the sites-enabled directory, which is read by Nginx during startup:

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

Test the Nginx configuration for correct syntax:

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

and if there are no errors, restart the Nginx service to enable the new changes:

sudo systemctl restart nginx

Finally to verify the server block is working as expected open in your browser of choice, and you should see see something like this:

Where to go from here

If you want to secure your website with a free LetsEncrypt SSL certificate, you can check the following guide:

Secure Nginx with Let’s Encrypt on Ubuntu 18.04

That’s all! If you are facing any problem, feel free to leave a comment.