×


Configure Nginx Server Blocks on CentOS 7 - Step by Step Process ?

Nginx Server Blocks allows you to host multiple domains on a single server. It is very useful to manage configurations of each site independently. We can set separate security policy and use different SSL certificates and much more. 

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform related Nginx queries.

In this context, we shall look into how to configure Nginx Server Blocks on a CentOS 7.


How to Create Nginx Server Blocks On CentOS ?

Nginx Server Blocks are similar to Apache Virtual Hosts.

Before proceeding with this Configuration procedure, you need to ensure that the following Prerequisites are met:


1. Create the Directory Structure

First, we will design a directory structure to store the site data to serve to visitors.

The top level directory is considered as DocumentRoot directory. We can set the document root to any location that you want but it's best practice to set in directory structure. 

So we will store all at /var/www

/var/www/
 ├── example1.com
 │   └── public_html
 ├── linuxapt.com
 │   └── public_html

Here, we are creating separate directory for each domain under /var/www directory. 

Within this directory, we'll create a public_html directory as domain document root directory to store website data:

$ sudo mkdir -p /var/www/example.com/public_html

Create a index.html file within the domain document root directory for testing purpose. 

By default, This page will display when visitors visit your website.

You can create a new index.html file using your favorite text editor type:

$ sudo nano /var/www/example.com/public_html/index.html

After that, add the below lines into it:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Welcome!!</title>
  </head>
  <body>
    <h1>Great! example.com set up completed!</h1>
  </body>
</html>

In this guide, all commands run as sudo user and newly created files and directories are owned by the root user. So we will change ownership of document root directories to avoid permission issue later for regular user. So our regular user can modify files in our web directories without any problems:

$ sudo chown -R nginx: /var/www/example.com


2. Create a Server Block

Nginx server block configuration files must end with .conf extension. Those files should store in /etc/nginx/conf.d directory.

Create a new file for example.com using your choice text editor by typing :

$ sudo nano /etc/nginx/conf.d/example.com.conf

Now, add the following lines in to this file:

server {
    listen 80;
    listen [::]:80;
    root /var/www/example.com/public_html;
    index index.html;
    server_name example.com www.example.com;
    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;
    location / {
        try_files $uri $uri/ =404;
    }
}

You can give any names to your configuration file but it’s best practice to give file name same as domain name.

Next, Save the file and test the Nginx configuration for correct syntax:

$ sudo nginx -t

You will get following output if there is no error:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

You must restart the Nginx service to take effect. Execute below command:

$ sudo systemctl restart nginx

Finally, you can verify by opening your http://example.com to your web browser and it should show you as following :

Great! example.com set up completed!


[Need to fix Nginx configuration errors? We can help you. ]


Conclusion

This article covers how to create an Nginx server block configuration to host multiple website on a single CentOS server.

Server Blocks, often referred to as Nginx virtual host are a feature of the Nginx web server that allows you to host multiple websites on one server.

To Test NGINX configuration file, run the command:

$ sudo nginx --t

If the syntax is OK, the output tells you the test was successful.


To restart your Nginx web server and confirm that it's running as expected:

$ sudo systemctl restart nginx
$ sudo systemctl status Nginx


To 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