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.

Prerequisites:

  • 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 crazytechgeek.info.
  • 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/crazytechgeek.info/html

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/crazytechgeek.info/html

Next, assign the right directory permissions recursively as shown:

$ sudo chmod -R 755 /var/www/crazytechgeek.info/html

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/crazytechgeek.info/html/index.html

Inside the file, paste the following sample content.

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

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/crazytechgeek.info.conf

Next, paste the configuration that appears below.

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

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

        server_name crazytechgeek.info www.crazytechgeek.info;

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

		
    access_log /var/log/nginx/crazytechgeek.info.access.log;
    error_log /var/log/nginx/crazytechgeek.info.error.log;

}
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:

http://domain-name
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 https://yourwebsite.com/ in your browser and look for the lock icon in the URL bar.

Conclusion

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.

If you liked this article, then do subscribe to email alerts for Linux tutorials. If you have any questions or doubts? do ask for help in the comments section.

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.

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.

This site uses Akismet to reduce spam. Learn how your comment data is processed.