How To Set Up Nginx Server Blocks on Debian 9

Updated on

3 min read

Set Up Nginx Server Blocks on Debian 9

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 will show you how to set up Nginx server blocks on Debian 9.


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

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

Create 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. The document root can be any directory on your Debian server.

We will use the following directory structure:

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

We’ll create a separate directory for each domain that will be hosted on the server inside the /var/www directory. Within each of these directories, we’ll create a public_html directory that will store the domain website files.

Start by creating the root directory for the domain

sudo mkdir -p /var/www/

Next, create an index.html file inside the domain’s document root directory.

sudo nano /var/www/

Open the file and paste the following lines:

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

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

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

Create a Server Block

By default on Debian 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:

sudo nano /etc/nginx/sites-available/
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.

Enable the new server block file by creating a symbolic link from the file to the sites-enabled directory:

sudo ln -s /etc/nginx/sites-available/ /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 in your browser of choice, and you will see something like this:


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

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

This post is a part of the How to Install LEMP Stack on Debian 9 series.
Other posts in this series: