
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. In this tutorial, we will learn how to set up Nginx Server Blocks on a CentOS 7.
Create Nginx Server Blocks On CentOS 7
Nginx Server Blocks are similar as Apache Virtual Hosts.
Prerequisites
- A CentOS 7 server with a non-root user with sudo privileges.
- A domain name should pointing to your server IP address.
- Nginx should installed and configured, as shown in How to Install Nginx on CentOS 7 Server.
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
├── example2.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 tutorial, 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
Creating 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 :

Conclusion
In this guide, you learned how to create an Nginx server block configuration to host multiple website on a single CentOS server. You can repeat the steps given above and create additional server blocks for all your domains.
Leave a Reply