Configure multiple Nginx sites

更新时间:
复制 MD 格式

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.

Note

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

  1. Remotely connect to the ECS instance where the LNMP stack is deployed.

    For connection instructions, see Connect to a Linux instance using Workbench.

  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 directories for the test websites.

    These directories will store your project code (website content).

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

    1. Run the following command to go to the Testpage-1 directory.

      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 i to enter insert mode and add the following test content.

      Test page 1

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

  5. Configure the Testpage-2 website.

    1. Run the following command to go to the Testpage-2 directory.

      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 i to enter insert mode and add the following test content.

      Test page 2

      After editing, press the Esc key, enter :wq, and press Enter 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{} block, locate the include directive.

    In this example, the configuration include /etc/nginx/conf.d/*.conf; indicates that Nginx obtains site information from all .conf files in the specified path. The line include /etc/nginx/conf.d/*.conf; is the include configuration 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;
        }
    }
  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 the Testpage-1 website.

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

      sudo vim Testpage1.conf
    2. Press i to 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 Esc key, enter :wq, and press Enter 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 i to 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 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 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.com to view the content of the Testpage-1 site.

    The page displays Test page 1, which indicates that the Testpage-1 website is correctly deployed and accessible.

  • Visit http://testpage2.com to view the content of the Testpage-2 site.

    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.

  1. Register a domain name.

    For instructions, see Domain name registration.

  2. 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.

  3. 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.