This topic describes how to use NGINX to build multiple websites on an ECS instance
that runs CentOS 7.
Background information
This tutorial is intended for users who are familiar with Linux and want to improve
O&M efficiency by making efficient use of resources and managing sites in a centralized
manner. For example, you can configure multiple blogging platforms of different categories
or build multiple websites for sophisticated businesses on an instance.
In this tutorial, the Testpage-1 and Testpage-2 sites are simultaneously built on an instance deployed with an LNMP environment and
then accessed.
The following instance configurations are used in the example:
- Instance type: ecs.c6.large
- Operating system: CentOS 7.8 64-bit
Create test sites
- Connect to the instance that is deployed with an LNMP environment.
- Run the following command to go to the configured website root directory:
- Run the following commands to create two test folders.
The folders are used to store information of the test websites, which is the project
code.
mkdir Testpage-1
mkdir Testpage-2
- Configure information of Testpage-1.
- Run the following command to go to Testpage-1:
cd /usr/share/nginx/html/Testpage-1/
- Run the following command to create and edit the
index.html
file:
- Press the I key to switch to the edit mode and enter the following test content:
Test page 1
Press the
Esc key, enter
:wq, and then press the Enter key to save the file and exit the edit mode.
- Configure information of Testpage-2.
- Run the following command to go to Testpage-2:
cd /usr/share/nginx/html/Testpage-2/
- Run the following command to create and edit the
index.html
file:
- Press the I key to switch to the edit mode and enter the following test content:
Test page 2
Press the
Esc key, enter
:wq, and then press the Enter key to save the file and exit the edit mode.
Configure NGINX
- Run the following command to check the nginx.conf configuration file:
cat /etc/nginx/nginx.conf
View the
include
configuration information in the
http{}
module.
include /etc/nginx/conf.d/*.conf;
indicates that NGINX will obtain site information from all files in the
.conf format in this path, as shown in the following figure.

- Run the following command to go inside the /etc/nginx/conf.d path:
- Create and configure the NGINX configuration file for Testpage-1.
- Run the following command to create and edit the configuration file:
- Press the I key to switch to the edit mode and enter the following content.
For the commented content, replace the server domain name and the project path with
your actual parameter values.
server {
listen 80;
server_name testpage1.com; #The test domain name is used here. Use the domain name of your server in the actual configuration.
#charset koi8-r;
access_log /var/log/nginx/b.access.log main;
location / {
root /usr/share/nginx/html/Testpage-1; #The test site path is used here. Use the path of your project code in the actual configuration.
index index.html index.htm;
}
#error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
Press the
Esc key, enter
:wq, and then press the Enter key to save the file and exit the edit mode.
- Create and configure the NGINX configuration file for Testpage-2.
- Run the following command to create and edit the configuration file:
- Press the I key to switch to the edit mode and enter the following content.
For the commented content, replace the server domain name and the project path with
your actual parameter values.
server {
listen 80;
server_name testpage2.com; #The test domain name is used here. Use the domain name of your server in the actual configuration.
#charset koi8-r;
access_log /var/log/nginx/b.access.log main;
location / {
root /usr/share/nginx/html/Testpage-2; #The test site path is used here. Use the path of your project code in the actual configuration.
index index.html index.htm;
}
#error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
Press the
Esc key, enter
:wq, and then press the Enter key to save the file and exit the edit mode.
- Run the following command to restart the NGINX server:
(Optional) Configure the hosts file on the local host
You must configure IP mapping in the local hosts file because all of the information
used in this tutorial is test values. If you use the actual server domain name when
you configure the sites, skip this step. In this tutorial, the local physical machine
uses the Windows operating system.
- Go to the C:\Windows\System32\drivers\etc directory.
- Copy the hosts file for backup.
Keep the hosts - copy file to restore the hosts file to its initial status after the configuration is completed.
- Modify the hosts file.
Append the following content to the end of the file:
<The public IP address of the instance> testpage1.com
<The public IP address of the instance> testpage2.com
Save the file and exit.
- Go back to the Windows desktop and press Win+R.
- In the Run dialog box that appears, enter cmd and click OK.
- Run the following command in the command line to immediately apply the configurations
of the hosts file:
Result
You can access the two test sites from a browser on the local host.
- If you go to
testpage1.com/
, you can view the content of the Testpage-1 site, as shown in the following figure.
- If you go to
testpage2.com/
, you can view the content of the Testpage-2 site, as shown in the following figure.
Multiple websites have been built. In your actual operation, you only need to make
sure that the domain names and project paths are correctly configured, and then you
can access these websites.