How to configure Nginx Server Block on CentOS 7

Server Blocks Nginx allows you to run more than one website on a single server. With Server Blocks, you can define the root of the site document (the directory that contains the website files), create separate security policies for each site, use a different SSL certificate for each site and much more.

In this tutorial, we will provide a step-by-step guide on how to set up Nginx server blocks (similar to Apache Virtual Hosts) on CentOS 7.

We recommend that you test every tutorial or guide present on the internet on a virtual machine (vmware or virtualbox) before deploying to a production server, so that you don’t mess up the OS when something goes wrong.


Make sure you meet the following prerequisites before continuing with this tutorial:

  • Get a domain name that points to your public server’s IP. In this tutorial we will be using
  • Nginx is installed on your CentOS system.
  • Log in as a user with sudo privileges.

In some documents, you will see Server Blocks It is referred to as Virtual host. The term virtual host is Apache.

Create the directory structure

Document root is the directory where website files for domain names are stored and served in response to requests. We can set the document root to anywhere we want but in this directory we will use the following directory structure:

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

We will create a separate directory for each domain we want to host in the directory /var/www. In this guide, we’ll create a directory public_html Which will be the document root directory for the domain which will keep the domain web site files.

See also  How to install and configure OrangeScrum on CentOS 8

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

sudo mkdir -p /var/www/

For testing purposes, we’ll create an index.html In the document root directory for the domain.

Open your text editor and create an experimental file index.html:

sudo nano /var/www/

Copy the following code and paste it into the file:

<!DOCTYPE html>
<html lang="en" dir="ltr">
    <meta charset="utf-8">
    <title>Selamat Datang di Server CentOS</title>
    <h1>Sukses, Server CentOS Online!</h1>

In this tutorial we run the command as sudo user and newly created files and folders are owned by the root user.

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

sudo chown -R nginx: /var/www/

Create a server cluster

The nginx server cluster configuration file should end in .conf And stored in the directory /etc/nginx/conf.d.

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

sudo nano /etc/nginx/conf.d/

You can name the configuration file as you like, but a domain name is recommended.

Copy the following code and paste it 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 your Nginx configuration for correct syntax:

sudo nginx -t

If there are no errors, then the result 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 that the server block is working as expected, open it In the browser of your choice, and you’ll see something like this:

See also  How to change permissions for folders and subfolders

CentOS Server


You learned how to configure a server cluster in Nginx to host multiple domains on a single CentOS server. You can repeat the steps we explained above and create additional server blocks for all of your domains.

If you want to secure your website with Let’s Encrypt’s free SSL certificate, you can check out the following guide: Secure Nginx with Let’s Encrypt on CentOS 7


Source link

Originally posted 2020-11-19 11:08:05.

About wahyuway

Check Also

How to install and configure Anaconda on CentOS 7

How to install and configure Anaconda on CentOS 7

Anaconda is the most popular machine learning and data science tool used in large scale …

Leave a Reply