YouTube Icon

Code Playground.

How to Set Up Nginx Server Blocks on CentOS 8


How to Set Up Nginx Server Blocks on CentOS 8

A worker block is a Nginx mandate that characterizes settings for a particular space, permitting you to run more than one site on a solitary worker. For every site, you can set the webpage archive root (the index which contains the site documents), make a different security strategy, utilize diverse SSL testaments, and significantly more. 

This article portrays how to set up Nginx worker impedes on CentOS 8. 


Guarantee that you have met the accompanying prerequisites before proceeding with this instructional exercise: 

  • Area name highlighting your public worker IP. 
  • Nginx introduced on your CentOS framework. 
  • You are signed in as root or client with sudo benefits . 

In some documentation, the term Server Blocks is alluded to as a Virtual host. A virtual host is an Apache expression. 

Creating the Directory Structure

The report root is the catalog wherein the site documents for an area name are put away and served because of solicitations. The report root can be set to any area you need. 

We will utilize the accompanying index structure: 

?   ??? public_html
?   ??? public_html
?   ??? public_html

For every space that will be facilitated on the worker, we'll make a different registry inside/var/www. Inside the space index, we'll make a public_html registry that will be the area archive root catalog and will store the area site records. 

We should begin by making the root registry for the space 

sudo mkdir -p /var/www/

For testing purposes, make an index.html record inside the area's report root registry: 

sudo nano /var/www/

Reorder the accompanying code into the record: 


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

To stay away from any authorization issues change the responsibility for area archive root index to client nginx: 

sudo chown -R nginx: /var/www/

Creating a Server Block

Naturally on CentOS, the Nginx worker block setup records must end with .conf and are put away in the/and so on/nginx/conf.d index. 

Open your content manager and make the arrangement record for the space: 

sudo nano /etc/nginx/conf.d/

The setup document can be named anything you need, however typically, it is ideal to utilize the area name. 

Reorder the accompanying code into the record: 


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;

Spare the record and check the Nginx arrangement for grammar blunders: 

sudo nginx -t

On the off chance that there are no blunders, the yield will resemble 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 administration for the progressions to produce results: 

sudo systemctl restart nginx

At long last, to check whether the worker block is functioning true to form, open in your program of decision, and you will see something like this: 


We have told you the best way to make Nginx worker squares and host various spaces on a solitary CentOS worker. You can rehash the means sketched out above and make extra worker blocks for every one of your areas. 

On the off chance that you need to protect your site with a SSL testament, you can create and introduce a free Letsencrypt SSL endorsement Don't hesitate to leave a remark on the off chance that you have any inquiries.