All Products
Search
Document Center

Terraform:Use Nginx to configure multiple websites

Last Updated:Mar 13, 2026

You can configure multiple websites on an Elastic Compute Service (ECS) instance to reduce operational costs and simplify website management tasks, such as updating software, configuring security settings, and backing up data. This configuration also lets you flexibly adjust resource allocation as website requirements change. This topic describes how to use Nginx to build multiple websites on a Linux ECS instance.

Prerequisites

An ECS instance with a public IP address has been created and an LNMP stack has been deployed. For more information, see Deploy an LNMP stack or (To be determined) Deploy an LNMP stack using Docker.

Note

This tutorial provides a one-click deployment solution for configuring multiple websites using Nginx. You can run the code to deploy the environment directly. Run with one click

Create test websites

  1. Connect to the ECS instance on which the LNMP stack is deployed.

  2. Run the following command to navigate to the configured website root directory.

    cd /usr/share/nginx/html
  3. Run the following commands to create two test folders.

    These folders are used to store the project code for the test websites.

    sudo mkdir Testpage-1
    sudo mkdir Testpage-2
  4. Configure the Testpage-1 website.

    1. Run the following command to navigate to the Testpage-1 folder.

      cd /usr/share/nginx/html/Testpage-1/
    2. Run the following command to create and edit the index.html file.

      sudo vim index.html
    3. Press the i key to enter edit mode, and then add the following content.

      Test page 1

      After you finish editing, press the Esc key, enter :wq, and then press the Enter key to save the file and exit.

  5. Configure the Testpage-2 website.

    1. Run the following command to navigate to the Testpage-2 folder.

      cd /usr/share/nginx/html/Testpage-2/
    2. Run the following command to create and edit the index.html file.

      sudo vim index.html
    3. Press the i key to enter edit mode, and then add the following content.

      Test page 2

      After you finish editing, press the Esc key, enter :wq, and then press the Enter key to save the file and exit.

Configure Nginx

  1. Run the following command to view the nginx.conf configuration file.

    cat /etc/nginx/nginx.conf

    In the http{} module, check the include configuration.

    In this example, the configuration is shown in the following figure. include /etc/nginx/conf.d/*.conf; indicates that Nginx loads site information from all .conf files in this path.nginx.conf

  2. Run the following command to navigate to the /etc/nginx/conf.d path.

    cd /etc/nginx/conf.d
  3. Create and configure the Nginx configuration file for the Testpage-1 website.

    1. Run the following command to create and edit the configuration file.

      sudo vim Testpage1.conf
    2. Press the i key to enter edit mode, and then 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;    # This is a test domain name. Replace it with your actual server domain name.
      
          #charset koi8-r;
          access_log  /var/log/nginx/b.access.log  main;
      
          location / {
              root   /usr/share/nginx/html;    # This is the test website path. Replace it with your actual project code path.
              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 you finish editing, press the Esc key, enter :wq, and then press the Enter key to save the file and exit.

  4. Create and configure the Nginx configuration file for the Testpage-2 website.

    1. Run the following command to create and edit the configuration file.

      sudo vim Testpage2.conf
    2. Press the i key to enter edit mode, and then add the following content.

      In the comments, replace the parameters for the server domain name and project path.

      server {
          listen       80;
          server_name  testpage2.com;    # This is a test domain name. Replace it with your actual server domain name.
      
          #charset koi8-r;
          access_log  /var/log/nginx/b.access.log  main;
      
          location / {
              root   /usr/share/nginx/html;    # This is the test website path. Replace it with your actual project code path.
              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 you finish editing, press the Esc key, enter :wq, and press Enter to save the file and exit.

  5. Run the following command to restart the Nginx service.

    sudo systemctl restart nginx

Verify the results

On your local host, open a browser and access the two test websites.

  • Access http://<Public IP address of the ECS instance>/Testpage-1 to view the content of the Testpage-1 website as shown in the following figure.

    image

  • Access http://<Public IP address of the ECS instance>/Testpage-2 to view the content of the Testpage-2 website as shown in the following figure.

    image

Multiple websites are now built. In a real-world scenario, you only need to ensure that the project paths are correctly configured in the configuration files to enable access to the websites.

What to do next

To make your website publicly accessible, use a domain name. This allows users to access your website more easily and securely. If you have a domain name or want to register one for your site, follow these steps.

  1. Register a domain name.

    For more information, see Register a domain name.

  2. Complete the ICP filing.

    If your domain name points to a website hosted on an Alibaba Cloud server in the Chinese mainland, you must complete an ICP filing. For more information, see ICP filing process.

  3. Resolve the domain name.

    Domain name resolution is required to access your website using its domain name. To point a domain name to the public IP address of an instance, see Add a website resolution record.

References

If you want to deploy multiple websites on a Windows instance, see Use IIS to configure multiple websites.