Updated on 2025-08-06 GMT+08:00

Setting Up Multiple Websites Using Nginx

You can set up multiple websites on an ECS to reduce operating costs and manage multiple websites (including updating software, security settings, and backing up data) conveniently. You can flexibly adjust resource allocation based on the requirements of each website. This section describes how to use Nginx to set up multiple websites on a Linux ECS.

Prerequisites

You have created an ECS with a public IP address and deployed the LNMP environment on the ECS. For details, see Setting Up an LNMP Environment.

Creating Test Websites

  1. Remotely connect to the ECS where the LNMP environment has been deployed.

    For details, see Login Overview (Linux).

  2. Run the following command to go to the root directory of the website:
    cd /usr/share/nginx/html
  3. Run the commands below to create two test folders.

    The folders are used to store test website information, that is, project code.

    sudo mkdir page-one
    sudo mkdir page-two
  4. Configure the test website page-one.
    1. Run the following command to go to the page-one folder:
      cd /usr/share/nginx/html/page-one/
    2. Run the following command to create and edit the index.html file:
      sudo vim index.html
    3. Press i to enter the editing mode and enter the following test content:
      test page one
    4. Press Esc, enter :wq, and press Enter to save the settings and exit.
  5. Configure the test website page-two.
    1. Run the following command to go to the page-two folder:
      cd /usr/share/nginx/html/page-two/
    2. Run the following command to create and edit the index.html file:
      sudo vim index.html
    3. Press i to enter the editing mode and enter the following test content:
      test page two
    4. Press Esc, enter :wq, and press Enter to save the settings and exit.

Configuring Nginx

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

    In the http{} module, view the include configuration information.

    In this example, include /etc/nginx/conf.d/*.conf; indicates that Nginx obtains website information from all .conf files in this path.

  2. Run the following command to go to the /etc/nginx/conf.d directory:
    cd /etc/nginx/conf.d
  3. Create and configure the Nginx configuration file for test website page-one.
    1. Run the following command to create and edit the configuration file:
      sudo vim page-one.conf
    2. Press i to enter the editing mode and enter the information below.

      Configure the server domain name and project path parameters according to the comments.

      server {
          listen       80;
          server_name  linux1.com;    #The test domain name is used in this test. In the actual configuration, you use your server domain name.
       
          location / {
              root   /usr/share/nginx/html/page-one;    #The test website path (the path of the project code) is used in this test. In the actual configuration, you can use the path of your project code.
              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;
          }
      }
    3. Press Esc, enter :wq, and press Enter to save the settings and exit.
  4. Create and configure the Nginx configuration file for test website page-two.
    1. Run the following command to create and edit the configuration file:
      sudo vim page-two.conf
    2. Press i to enter the editing mode and enter the information below.

      Configure the server domain name and project path parameters according to the comments.

      server {
          listen       80;
          server_name  linux2.com;    #The test domain name is used in this test. In the actual configuration, you use your server domain name.
       
          location / {
              root   /usr/share/nginx/html/page-two;    #The test website path (the path of the project code) is used in this test. In the actual configuration, you can use the path of your project code.
              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;
          }
      }
    3. Press Esc, enter :wq, and press Enter to save the settings and exit.
  5. Run the following command to restart the Nginx service:
    sudo systemctl restart nginx

Verification

The preceding domain names are test domain names. You need to add the hosts file to the ECS and configure it to point the test domain names to the ECS IP address for testing.

The following figure shows the content of the hosts file. The path of the hosts file is /etc/hosts.

  • Access http://linux1.com and view the content of website page-one.

  • Access http://linux2.com and view the content of website page-two.

The websites have been set up. In actual website setup scenarios, ensure that the project paths are correctly configured in the configuration file to achieve multi-website access.