How to Set Up Nginx Server Blocks on CentOS 7
Updated on
•6 min read

Nginx Server Blocks allows you to run more than one website on a single machine. This is useful because for each site you can specify the site document root (the directory which contains the website files), create a separate security policy, use different SSL certificates, and much more.
In this tutorial, we’ll explain how to set up Nginx server blocks on CentOS 7.
Prerequisites
Ensure that you have met the following prerequisites before continuing with this tutorial:
- Domain name pointing to your public server IP. We will use
example.com
. - Nginx installed on your CentOS system.
- Logged in as root or user with sudo privileges .
Server Blocks
being referred to as a Virtual host
.
A virtual host
is an Apache term.Create the Directory Structure
The document root is the directory where the website files for a domain name are stored and served in response to requests. We can set the document root to any location you want.
We will use the following directory structure:
/var/www/
├── example.com
│ └── public_html
├── example2.com
│ └── public_html
├── example3.com
│ └── public_html
Basically we are creating a separate directory for each domain we want to host on our server inside the /var/www
directory. Within this directory, we’ll create a public_html
directory that will be the domain document root directory and will store the domain website files.
Let’s start by creating the root directory for our domain example.com
:
sudo mkdir -p /var/www/example.com/public_html
For testing purposes, we will create an index.html
file inside the domain’s document root directory.
Open your text editor
and create the demo index.html
file:
sudo nano /var/www/example.com/public_html/index.html
Copy and paste the following code into the file:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Welcome to example.com</title>
</head>
<body>
<h1>Success! example.com home page!</h1>
</body>
</html>
In this example, we are running the commands as a sudo user and the newly created files and directories are owned by the root user.
To avoid any permission issues, change the ownership
of the domain document root directory to the Nginx user (nginx
):
sudo chown -R nginx: /var/www/example.com
Create a Server Block
Nginx server block configuration files must end with .conf
and are stored in /etc/nginx/conf.d
directory.
Open your editor of choice and create a server block configuration file for example.com
.
sudo nano /etc/nginx/conf.d/example.com.conf
Copy and paste the following code into the 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;
}
}
Save the file and test the Nginx configuration for correct syntax:
sudo nginx -t
If there are no errors, the output 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 the server block is working as expected open http://example.com
in your browser of choice, and you will see something like this:

Conclusion
You have learned how to create an Nginx server block configuration to host multiple domains on a single CentOS server. You can repeat the steps we outlined above and create additional server blocks for all your domains.
If you want to secure your website with a free LetsEncrypt SSL certificate, you can check the following guide:
Secure Nginx with Let’s Encrypt on CentOS 7
If you are facing any problems, feel free to leave a comment.
This post is a part of the install-lemp-stack-on-centos-7 series.
Other posts in this series: