Configure multiple Nginx sites
Hosting multiple websites on a single ECS instance is a cost-effective way to manage your web presence. It simplifies tasks like software updates, security configurations, and data backups, and lets you flexibly allocate resources based on each site's needs. This topic shows you how to use Nginx to host multiple websites on a Linux ECS instance.
Prerequisites
You have created an ECS instance with a public IP address and deployed an LNMP stack. For more information, see Deploy an LNMP stack or (To be determined) Deploy an LNMP stack using Docker.
This tutorial offers a one-click solution for deploying an Nginx environment to host multiple websites. You can run the code to deploy the environment directly. Run with one click
Create test websites
-
Remotely connect to the ECS instance where the LNMP stack is deployed.
For connection instructions, see Connect to a Linux instance using Workbench.
-
Run the following command to navigate to the configured website root directory.
cd /usr/share/nginx/html -
Run the following commands to create two directories for the test websites.
These directories will store your project code (website content).
sudo mkdir Testpage-1 sudo mkdir Testpage-2 -
Configure the Testpage-1 website.
-
Run the following command to go to the Testpage-1 directory.
cd /usr/share/nginx/html/Testpage-1/ -
Run the following command to create and edit the
index.htmlfile.sudo vim index.html -
Press
ito enter insert mode and add the following test content.Test page 1After editing, press the
Esckey, enter:wq, and press Enter to save the file and exit.
-
-
Configure the
Testpage-2website.-
Run the following command to go to the
Testpage-2directory.cd /usr/share/nginx/html/Testpage-2/ -
Run the following command to create and edit the
index.htmlfile.sudo vim index.html -
Press
ito enter insert mode and add the following test content.Test page 2After editing, press the
Esckey, enter:wq, and press Enter to save the file and exit.
-
Configure Nginx
-
Run the following command to view the
nginx.confconfiguration file.cat /etc/nginx/nginx.confIn the
http{}block, locate theincludedirective.In this example, the configuration
include /etc/nginx/conf.d/*.conf;indicates that Nginx obtains site information from all.conffiles in the specified path. The lineinclude /etc/nginx/conf.d/*.conf;is theincludeconfiguration line to note, and is used to load modular configuration files from the /etc/nginx/conf.d directory.http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory. # See http://nginx.org/en/docs/ngx_core_module.html#include # for more information. include /etc/nginx/conf.d/*.conf; server { listen 80 default_server; listen [::]:80 default_server; server_name _; root /usr/share/nginx/html; } } -
Run the following command to go to the
/etc/nginx/conf.ddirectory.cd /etc/nginx/conf.d -
Create and configure the Nginx configuration file for the
Testpage-1website.-
Run the following command to create and edit the configuration file.
sudo vim Testpage1.conf -
Press
ito enter insert mode and add the following content.Replace the server domain name and project path parameters as indicated in the comments.
server { listen 80; server_name testpage1.com; # A test domain name. Replace with your actual server domain name. #charset koi8-r; access_log /var/log/nginx/b.access.log main; location / { root /usr/share/nginx/html/Testpage-1; # Path to this site's root directory. 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; } }After editing, press the
Esckey, enter:wq, and press Enter to save the file and exit.
-
-
Create and configure the Nginx configuration file for the
Testpage-2website.-
Run the following command to create and edit the configuration file.
sudo vim Testpage2.conf -
Press
ito enter insert mode and add the following content.Replace the server domain name and project path parameters as indicated in the comments.
server { listen 80; server_name testpage2.com; # A test domain name. Replace with your actual server domain name. #charset koi8-r; access_log /var/log/nginx/b.access.log main; location / { root /usr/share/nginx/html/Testpage-2; # Path to this site's root directory. 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; } }After editing, press the
Esckey, enter:wq, and press Enter to save the file and exit.
-
-
Run the following command to restart the Nginx service.
sudo systemctl restart nginx
Verify the results
On your local machine, edit the hosts file to map the test domain names to your ECS instance's public IP address. After saving the file, you can verify the websites in your browser.
-
Visit
http://testpage1.comto view the content of theTestpage-1site.The page displays Test page 1, which indicates that the Testpage-1 website is correctly deployed and accessible.
-
Visit
http://testpage2.comto view the content of theTestpage-2site.The page displays Test page 2, which indicates that the Testpage-2 website is correctly deployed and accessible.
For your own projects, ensure the root directive in each site's configuration file points to the correct project directory.
Next steps
To make your website publicly available, we recommend using a domain name for improved accessibility and security. If you need to register a domain name or already have one, proceed with the following steps.
-
Register a domain name.
For instructions, see Domain name registration.
-
Apply for an ICP filing.
If your domain name points to a website hosted on an Alibaba Cloud server in the Chinese mainland, you must apply for an ICP filing. For instructions, see ICP filing process.
-
Configure domain name resolution.
Domain name resolution is required to access your website via its domain name. To point your domain name to the instance's public IP address, see Add a website resolution record.
Related documentation
To learn how to host multiple websites on a Windows instance, see Configure IIS to Host Multiple Websites.