A server configuration block is an Nginx directive, which defines the settings for a specified domain name, allowing you to run multiple websites on a single server. For each website, you can set the website file root directory (the directory containing website files), create an independent security policy, use a different [SSL certificate] (https://cloud.tencent.com/product/symantecssl?from=10680), etc.
This article describes how to set up the Nginx server block on CentOS 8.
Before proceeding with this guide, please make sure you meet the following prerequisites:
In some documents, the term Server Blocks
is also referred to as Virtual host
. A virtual host is an Apache term.
The document root directory is the place where the corresponding website files of each domain name are stored and in response to requests.
The document root directory can be set to any place 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 under the /var/www
directory. Under the folder domain name folder, we create a public_html
folder, which will serve as the file root directory of the domain name and will store the website files of the domain name.
We start to create the 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, change the owner of the document root directory of the domain name to the user nginx
:
sudo chown -R nginx:/var/www/example.com
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 configuration file for the domain name:
sudo nano /etc/nginx/conf.d/example.com.conf
The configuration file can be named whatever you want, but in general, 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 whether the Nginx configuration file has 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 take effect:
sudo systemctl restart nginx
Finally, verify that the server configuration block is running normally, open http://example.com
in your browser, you will see the following screen:
We have shown you how to create an Nginx server configuration block and host many domains on a simple CentOS server.
You can repeat the above steps and add additional server configuration blocks for all your domains.
If you want to use SSL certificate to encrypt your website, you can generate and install a free Letsencrypt free SSL certificate.
Recommended Posts