How to set up the Nginx server configuration block on CentOS 8

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.

1. Prerequisites##

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.

Second, create a file structure##

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

Three, 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 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:

Four, summary##

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

How to set up the Nginx server configuration block on CentOS 8
How to set up Shiny Server on Ubuntu 14.04
How to set up SSH keys on CentOS 8
How to set up a DNS server on Ubuntu 18.04
How to install Nginx on CentOS 8
How to set up an Apache virtual host on CentOS 7
How to set up password authentication with Nginx on Ubuntu 14.04
Detailed steps to set up a Git server on CentOS
How to set up Gogs on Ubuntu 14.04
How to set up R on Ubuntu 14.04
How to modify the hostname on CentOS 8
How to set static IP on Ubuntu 18.04 Server
How to quickly build Nginx server under CentOS
How to set static IP on Ubuntu 18.04 Server
How to set up time synchronization on Ubuntu 18.04
How to modify the CentOS server time to Beijing time
How to run the parsing server on Ubuntu 14.04
How to add the gzip module to Nginx on Ubuntu 14.04
How to configure FTP server with Vsftpd on CentOS 8
How to install and configure NFS server on CentOS 8
How to configure FTP server with Vsftpd on CentOS 8
How to use Let&#39;s Encrypt to protect Nginx on CentOS 8
Install Nginx server on CentOS 7
How to open https on nginx server under Ubuntu
How to set up Java Home on Ubuntu and Raspbian
How to install and configure Postfix mail server on CentOS8
How to set or modify the time zone on Ubuntu 20.04
How to set up vsftpd for anonymous downloads on Ubuntu 16.04
How to set or modify the time zone on Ubuntu 20.04
How to set up a production Elasticsearch cluster on Ubuntu 14.04
How to set up an Apache virtual host on Ubuntu 16.04
How to set up an Apache virtual host on Ubuntu 20.04
How to set up vsftpd for user directories on Ubuntu 16.04
How to set or modify the time zone in CentOS 8
How to install jdk1.8 on centOS7
How to install MySQL on CentOS 8
How to install R on CentOS 8
How to install FFmpeg on CentOS 8
How to install Virtualbox on CentOS 8
How to install Nginx on Ubuntu 20.04
How to install TensorFlow on CentOS 8
How to Update to gcc4.9.x on Centos7
How to install TeamViewer on CentOS 8
How to install Gradle on CentOS 8
How to install Elasticsearch on CentOS 8
How to install Jenkins on CentOS 8
How to install Go on CentOS 8
How to install GCC on CentOS 8
How to install Yarn on CentOS 8
How to install Asterisk on CentOS 7
How to install Jenkins on CentOS 8
How to install Vagrant on CentOS 8
How to install Skype on CentOS 8
How to install htop on CentOS 8
How to install Python on CentOS 8
How to install Elasticsearch on CentOS 8
How to secure Nginx on Ubuntu 14.04
How to install Postgresql on CentOS 8
How to install Wordpress on Centos
How to install Nginx on Ubuntu 20.04
How to install htop on CentOS 8