How to set up Nginx server blocks (virtual hosts) on Ubuntu

Last updated on June 29, 2020

Nginx server blocks allow you to run multiple websites on a single server. Using this server block, you can specify the website document root, different SSL certificates for different sites, different security policies for different sites and reverse proxy configuration, and much more. Here the tutorial we explain the step by step instruction about how to set up Nginx server blocks on Ubuntu.

Prerequisites

Before going to follow this tutorial you need to install Nginx and make sure it was running properly. Next, point your domains to your server and also make sure you have a non-root user account with sudo privileges

Create and set up domain root directories

Domain root directories where the website files stored and served by the server. We will use the following directory structure:

/var/www/
├── domainname1.com
│   └── html
├── domainname2.com
│   └── html
├── domainname3.com
│   └── html

In the above, we create a root directory for all domains /var/www/ next, create a separate directory for each domain on that directory again create a new directory called html and stored the domain root files. Let's create a directory for our domain example.com

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

For testing, we create a new html file and place some html code in it.

<!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>

Create a server block

By default on Ubuntu, Nginx server blocks configuration files are stored in /etc/nginx/sites-available directory, and the symbolic links to the /etc/nginx/sites-enabled/ directory.

Open the editor (in our case we choice nano editor) and create the following server block file:

sudo nano /etc/nginx/sites-available/example.com
/etc/nginx/sites-available/example.com
server {
    listen 80;
    listen [::]:80;

    root /var/www/example.com/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;
    }
}

Enable the server block

To enable the new server block file, 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 Nginx configuration

To test the Nginx configuration by entering the below command:

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

Now, we are ready to restart Nginx to enable your changes. You can do that by typing:

sudo systemctl restart nginx

Test Nginx server block

Now, you should test that your server blocks are functioning correctly. You can do that by visiting the domain http://example.com in your web browser:

Conclusion

Congratulation, Nginx server block to host multiple domains on a single Ubuntu server. You can host another domain by repeating the above steps. If you face any issues feel free to comment below we will assist you shortly.