Setting the Nginx server configuration block on CentOS 8

Introduction:   A server configuration block is an Nginx instruction, which defines the settings for a specified domain name and allows you to run multiple websites on a single server. For each website, you can set the root directory of website files (the directory containing website files), create an independent security policy, use different SSL certificates, and so on. This article will introduce you how to set up the Nginx server block on CentOS 8.


For image download, domain name resolution and time synchronization, please click   Alibaba open source mirror station

A server configuration block is an Nginx instruction, which defines the settings for a specified domain name and allows you to run multiple websites on a single server. For each website, you can set the root directory of website files (the directory containing website files), create an independent security policy, use different SSL certificates, and so on. This article describes how to set up the Nginx server block on CentOS 8.

1, Preconditions

Before continuing with this guide, make sure you meet the following prerequisites:

  • The domain name that points to your public server IP
  • Nginx is installed on your CentOS system
  • Log in as root or other user with sudo privileges

In some documents, the term Server Blocks is also referred to as Virtual host. A Virtual host is an Apache term.

2, Create file structure

The document root directory is the place where the website file corresponding to each domain name is stored and responds to requests.
The document root directory can be set anywhere you want.
We will use the following folder structure:

/var/www/
├── example.com
│   └── public_html
├── example2.com
│   └── public_html
├── example3.com
│   └── public_html

For each domain name hosted on the server, we will create a separate folder in the / var/www directory. Under the folder domain name folder, we create a public_html folder, which will be the root directory of the domain name file, and will store the website file of the domain name.
Let's start creating a root directory for the domain name example.com:

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

To facilitate testing, we create an index.html file in the document root directory of the domain name.

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>

To avoid any permission problems, modify the owner of the document root directory of the domain name to the user nginx:

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

3, Create a server block

On CentOS, the Nginx server block configuration file ends with. Conf by default and is stored in the / etc/nginx/conf.d directory.
Open your text editor and create a profile for the domain name:

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

The configuration file can be named whatever you want, but generally, we'd better use the domain name.
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 check the Nginx configuration file 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 to make the application effective:

sudo systemctl restart nginx

Finally, verify that the server configuration block is working properly and open it in your browser[ http://example.com ]( http://example.com ), you will see the following picture:


4, Summary

We have shown you how to create the Nginx server configuration block and host many domain names on a simple CentOS server.
You can repeat the above steps and add additional server configuration blocks for all your domain names.
If you want to encrypt your website with SSL certificate, you can generate and install a free SSL certificate   Letencrypt free SSL certificate.

This article is transferred from:   Setting the Nginx server configuration block on CentOS 8 - Alibaba cloud developer community

 

Tags: CentOS Nginx server

Posted on Fri, 03 Dec 2021 12:01:16 -0500 by navid