Nginx Server Blocks allows you to run more than one website on a single machine. This is useful because for each site you can specify the site document root (the directory which contains the website files), create a separate security policy, use different SSL certificates, and much more.



  • Ensure that you have met the following prerequisites before continuing with this tutorial:
  • Domain name pointing to your public server IP. We will use
  • Nginx installed on your CentOS system.
  • Logged in as root or user with sudo privileges.


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. We can set the document root to any location you want.

We will use the following directory structure:

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


Basically we are creating a separate directory for each domain we want to host on our server inside the /var/www directory. Within this directory, we’ll create a public_html directory that will be the domain document root directory and will store the domain website files.


Let’s start by creating the root directory for our domain

$ sudo mkdir -p /var/www/


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


Open your text editor and create the demo index.html file:

$ sudo nano /var/www/


Copy and paste the following code into the file:

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


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


To avoid any permission issues, change the ownership of the domain document root directory to the Nginx user (nginx):

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


Create a Server Block


Nginx server block configuration files must end with .conf and are stored in /etc/nginx/conf.d directory.


Open your editor of choice and create a server block configuration file for

$ sudo nano /etc/nginx/conf.d/


You can name the configuration file as you want. Usually, it is best to use the domain name.


Copy and paste the following code into the file:

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;


Save the file and 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:

 successful! home page



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


Was this answer helpful? 0 Users Found This Useful (0 Votes)