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.

Prerequisites

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

In some documentation, you’ll see Server Blocks being referred as Virtual host. A virtual host is an Apache term.

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:

/var/www/
├── domain1.com
│   └── public_html
├── domain2.com
│   └── public_html
├── domain3.com
│   └── public_html

Basically 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 example.com:

sudo mkdir -p /var/www/example.com/public_html

For testing purposes we will create an index.html file inside the domain’s document root directory.

Open your editor and create the demo file:

/var/www/example.com/public_html/index.html


<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Welcome to example.com</title>
  </head>
  <body>
    <h1>Success! example.com home page!</h1>
  </body>
</html>

In this guide, we are running the commands as sudo user and the newly created files and directories are owned by the root user.

To avoid any permission issues we can change the ownership of the domain document root directory to the Nginx user (www-data):

sudo chown -R www-data: /var/www/example.com

Create a 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 the /etc/nginx/sites-enabled/ directory.

Open your editor of choice and create the following server block file:

/etc/nginx/sites-available/example.com

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 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/example.com /etc/nginx/sites-enabled/

Test the Nginx configuration for correct syntax:

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

Conclusion

You have learned how to create an Nginx server block configuration to host multiple domains on a single Ubuntu server. You can repeat the steps we outlined above and create additional server blocks for all your domains.

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

If you are facing any problem, feel free to leave a comment.