How to Set Up Nginx Server Blocks on CentOS 8

Published on

3 min read

Set Up Nginx Server Blocks on CentOS 8

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 CentOS 8.


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

In some documentation, 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 in which the website files for a domain name are stored and served in response to requests. The document root can be set to any location you want.

We will use the following directory structure:

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

For each domain that will be hosted on the server, we’ll create a separate directory inside /var/www. Within the domain 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 the domain

sudo mkdir -p /var/www/

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

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>

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

sudo chown -R nginx: /var/www/

Creating a Server Block

By default on CentOS, the Nginx server block configuration files must end with .conf and are stored in the /etc/nginx/conf.d directory.

Open your text editor and create the configuration file for the domain:

sudo nano /etc/nginx/conf.d/

The configuration file can be named anything you want, but 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 check the Nginx configuration for syntax errors:

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 whether the server block is working as expected, open in your browser of choice, and you will see something like this:


We have shown you how to create Nginx server blocks and host multiple domains on a single CentOS server. You can repeat the steps outlined above and create additional server blocks for all your domains.

If you want to secure your website with an SSL certificate, you can generate and install a free Letsencrypt SSL certificate .

Feel free to leave a comment if you have any questions.