[ad_1]
A server block is an Nginx directive that defines settings for a specific domain, allowing you to run more than one website on a single server. For each website, you can set the site document root (the directory which contains the website files), create a separate security policy, use different SSL certificates, and much more.
This article describes how to set up Nginx server blocks on CentOS 8.
Prerequisites #
Ensure that you have met the following requirements before continuing with this tutorial:
In some documentation, the term Server Blocks
is referred to as a Virtual host
.
A virtual host
is an Apache term.
Creating the Directory Structure #
The document root is the directory in which the website files for a domain name are stored and served in response to requests. The document root can be set 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
For each domain that will be hosted on the server, we’ll create a separate directory inside /var/www
. Within the domain 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 the domain example.com
:
sudo mkdir -p /var/www/example.com/public_html
For testing purposes, create an index.html
file inside the domain’s document root directory:
sudo nano /var/www/example.com/public_html/index.html
Copy and paste the following code into the file:
/var/www/example.com/public_html/index.html
<!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>
To avoid any permission issues change the ownership
of the domain document root directory to user nginx
:
sudo chown -R nginx: /var/www/example.com
Creating a Server Block #
By default on CentOS, the Nginx server block configuration files must end with .conf
and are stored in the /etc/nginx/conf.d
directory.
Open your text editor
and create the configuration file for the domain:
sudo nano /etc/nginx/conf.d/example.com.conf
The configuration file can be named anything you want, but usually, it is best to use the domain name.
Copy and paste the following code into the file:
/etc/nginx/conf.d/example.com.conf
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 check the Nginx configuration
for syntax errors:
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 whether the server block is working as expected, open http://example.com
in your browser of choice, and you will see something like this:
Conclusion #
We have shown you how to create Nginx server blocks and host multiple domains on a single CentOS server. You can repeat the steps outlined above and create additional server blocks for all your domains.
If you want to secure your website with an SSL certificate, you can generate and install a free Letsencrypt SSL certificate
.
Feel free to leave a comment if you have any questions.
[ad_2]
Source link