How to configure Nginx Server Blocks on a Debian 9 VPS or Dedicated Server

Posted on


Nginx Server Blocks enable web administrators to run multiple websites on one VPS or dedicated server.

The server blocks allow you to encapsulate web configuration details, making it easy to utilize different SSL certificates, create a unique security policy, and stipulate the document root for each website.

This tutorial will help you configure the Nginx Server Blocks on your Debian 9 server.

Before you Start

For this tutorial to flow seamlessly, you require the following:

  • Nginx installed on your Debian 9 server at a service that supports Linux hosting
  • A domain pointing to the Debian server. We’ll utilize as the domain name in our tutorial; remember to replace this name with your unique domain name.

If everything is in place, let’s get started!

Step 1 – Creating A Document Structure

The first thing when setting up Nginx server blocks is creating a directory structure (document root). The document root is a location where your domain’s web files are store and presented upon any request. Feel free to structure your document root (main directory) to be on any location on your system.

In our tutorial we’ll have the structure below for the root directory:

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

If you are hosting multiple domains, you should come up with a distinct directory structure for each of the domain. Here, we will create a root directory for the domain name and host it on the directory,  /var/www. That is, We will create a directory called public_html, within the main directory, /var/www. The public_html is a directory used to store the website files for each of the domain.

Log in to your Debian 9 server and issue the command below:

$ sudo mkdir -p /var/www/

The command above will create a directory, public_html for the domain in the main directory, /var/www.

Next, run the command below to create a file called index.html that is hosted in the directory  public_html.

$ sudo nano /var/www/

Copy and add the lines below into this file:

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

Save the changes and exit the text editor.

Now, issue the command below to transfer your document root’s ownership to a www-data (Nginx User). This prevents the occurrence of permission issues:

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

Step 2 – Creating A Nginx Server Block

Debian 9 system stores the configuration files for Nginx server blocks in the directory, /etc/nginx/sites-available. These configuration files are enabled to another directory /etc/nginx/sites-enabled/, via symbolic links.

To create a file for the Nginx Server Block, issue the command below

$ sudo nano /etc/nginx/sites-available/

Copy and add the content below into the file:

server {
    listen [::]:80;

    root /var/www/;

    index index.html;


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

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

Save the changes and exit the text editor.

Note: we’ve used as the name for our configuration file. You can give this file a different name, but it’s advisable you use your domain as the name for the file.

By now, the configuration file is ready but it’s enabled. To enable this file, execute the command below:

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

Next, run the command below to confirm if the format for this configuration file is correct:

$ sudo nginx -t

If everything was implemented successfully, you will get an output similar to the one below:

nginx: the configuration file /etc/nginx/nginx.confsyntax is ok
nginx: configuration file /etc/nginx/nginx.conftest is successful

Now, issue the command below to restart Nginx and implement the changes made:

$ sudo systemctl restart nginx

To check if the new server block is functioning properly, open your website using your favorite browser. If the server block is running correctly, you will get this message:

How to configure Nginx Server Blocks on a Debian 9 VPS or Dedicated Server


That is it! You have successfully created and configured a Nginx Server Block on your Debian 9 server. Follow the steps above to set up a server block for each of the other domains.

Check out these top 3 Best web hosting services


Was this article helpful?


Leave a Reply

Your email address will not be published. Required fields are marked *