How to Set Up Nginx Server Blocks (Virtual Hosts) on CentOS 8

An Nginx server block is the equivalent of an Apache virtual host and makes it possible for you to host more than one domain or website on your server.

In this article, you will learn how to set up an Nginx server blocks (virtual hosts) on CentOS 8 and RHEL 8 Linux.


  • An A record for your domain. In simple terms, an A records refers to a DNS entry where the domain name is pointed to the Public IP of the server, in this case the Nginx web server. Throughout this guide , we will use the domain name
  • An Nginx Server or LEMP Stack installed on CentOS 8 or RHEL 8 instance.
  • A login user with Sudo privileges.

Let’s get started!

Step 1: Create an Nginx Document Root Directory

Right off the bat, you need to create a custom web root directory for the domain you want to host. For our case, we will create the directory as shown using the mkdir -p option to create all the necessary parent directories:

$ sudo mkdir -p /var/www/

Thereafter assign the directory permissions using the $USER environment variable. As you do so, ensure that you are logged in as a regular user and not the root user.

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

Next, assign the right directory permissions recursively as shown:

$ sudo chmod -R 755 /var/www/

Step 2: Create a Sample Page for the Domain

Next, we are going to create an index.html file inside the custom web root directory that will be served by the domain once a request is made.

$ sudo vim /var/www/

Inside the file, paste the following sample content.

        <title>Welcome to your_domain!</title>
  <h1>Awesome! Your Nginx server block is working!</h1>

Save and exit the configuration file.

Step 3: Create an Nginx Server Block in CentOS

For the Nginx web server to serve the content in the index.html file we created in step 2, we need to create a server block file with the appropriate directives. Therefore, we shall create a new server block at:

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

Next, paste the configuration that appears below.

server {
        listen 80;
        listen [::]:80;

        root /var/www/;
        index index.html index.htm index.nginx-debian.html;


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

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

Create Nginx Server Block in CentOS
Create Nginx Server Block in CentOS

When you are done, save the changes and exit the configuration file. To confirm that all Nginx configurations are sound and error-free, execute the command:

$ sudo nginx -t

The output below should be a confirmation that you are good to go!

Check Nginx Configuration File in CentOS
Check Nginx Configuration File in CentOS

Finally, restart your Nginx web server and confirm that it’s running as expected:

$ sudo systemctl restart nginx
$ sudo systemctl status nginx
Check Nginx Status in CentOS
Check Nginx Status in CentOS

Step 4: Testing the Nginx Server Block in CentOS

We are all done with the configurations. The only part remaining is to confirm if our server block is serving content in the web root directory defined earlier in the index.html file.

To do this, simply open your browser and go to your server’s domain as shown:

Check Nginx Server Block
Check Nginx Server Block

As observed, our content is being served by the server block, a clear indication that all went well.

Step 5: Enable HTTPS on Domain Hosted on Nginx

You may consider encrypting your domain using Lets Encrypt SSL to add a layer of protection and secure traffic to and from the webserver.

$ sudo dnf install certbot python3-certbot-nginx
$ sudo certbot --nginx

To confirm that your domain is correctly configured on HTTPS, visit in your browser and look for the lock icon in the URL bar.


We have successfully set up a Nginx server block on CentOS 8 and RHEL 8. You can repeat the same for multiple domains using the same procedure.

Tutorial Feedback...
Was this article helpful? If you don't find this article helpful or found some outdated info, issue or a typo, do post your valuable feedback or suggestions in the comments to help improve this article...

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

2 thoughts on “How to Set Up Nginx Server Blocks (Virtual Hosts) on CentOS 8”

  1. Just a quick Note:

    $ sudo systemctl restart nginx
    $ sudo systemctl status Nginx

    should be:

    $ sudo systemctl restart nginx
    $ sudo systemctl status nginx

    The Capitalised “Nginx” on line two caused an issue :)


Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.