How To Set Up Nginx Server Blocks on Ubuntu 20.04

Posted 

3 min read

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 20.04.

Prerequisites

Ensure that you have met the following requirements before continuing:

In some articles, 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:

/var/www/
├── domain1.com
│   └── public_html
├── domain2.com
│   └── 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/domain1.com/public_html

We’ll also create an index.html file inside the domain document root directory that will be shown when you visit the domain in your browser:

/var/www/example.com/public_html/index.html
<!DOCTYPE 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>

Since the commands above are executed as a sudo user, the newly created files and directories are owned by root. 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/domain1.com

Creating a Server Block

On Ubuntu systems, Nginx server block configuration files are located in /etc/nginx/sites-available directory. They can be enabled by creating symbolic links to the /etc/nginx/sites-enabled directory, which Nginx read during the startup.

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

/etc/nginx/sites-available/example.com
server {
    listen 80;

    server_name example.com www.example.com;

    root /var/www/example.com/public_html;

    index index.html;

    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;
}
  • server_name: The domains that should match for 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 read 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 that the server block is working as expected, open http://example.com in your browser of choice, and you will see something like this:

Conclusion

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 are facing any problem, feel free to leave a comment.