Help Center> Domain Name Service> FAQs> Public Zones> Configuring URL Forwarding Using Nginx

Configuring URL Forwarding Using Nginx

Scenarios

If multiple websites are deployed on your server and stored in different directories, you need to configure URL forwarding to forward domain names to different directory URLs when accessing these websites. This topic describes how to use Nginx to forward URLs.

Background

URL forwarding is classified into explicit URL forwarding and implicit URL forwarding.

  • Explicit URL forwarding

    Explicit URL forwarding uses 301 redirect (also called permanent redirect) or 302 redirect (also called temporary redirect). To resolve a domain name to an ECS IP address, you need to configure the domain name and URL on the ECS. The following describes how you can use Nginx for explicit URL forwarding.

    Status codes 301 and 302 indicate redirection. After receiving status code 301 or 302 from the server, the browser redirects to a new URL. 301 indicates that the resource on the old URL has been permanently removed and is inaccessible, and the search engine replaces the old URL with the new URL when capturing new content. 302 indicates that the resource on the old URL still exists and can still be accessed. The redirect is only a temporary relocation of old URL to the new URL, and the search engine will capture new content and save the old URL.

  • URL implicit forwarding

    Implicit URL forwarding uses the iframe technology.

Step 1: Install Nginx on an ECS

Ensure that http_rewrite_module is enabled. By default, http_rewrite_module is enabled.

The following uses CentOS 7 as an example to describe how to install Nginx:

  1. Install dependency packages.

    yum install -y gcc-c++

    yum install -y pcre pcre-devel

    yum install -y zlib zlib-devel

    yum install -y openssl openssl-devel

  2. Download and decompress the installation package.

    wget http://nginx.org/download/nginx-1.20.1.tar.gz

    tar -zxvf nginx-1.20.1.tar.gz

  3. Install Nginx.

    Replace /usr/local/nginx in the command with the actual installation path.

    cd nginx-1.20.1

    ./configure --prefix=/usr/local/nginx

    make && make install

  4. Check whether the installation is successful.

    cd /usr/local/nginx/sbin

    ./nginx -t

    If the following information is displayed, the installation is successful:

    nginx: the configuration file /usr/local/nginx//conf/nginx.conf syntax is ok 
    nginx: configuration file /usr/local/nginx//conf/nginx.conf test is successful

Step 2: Configure Nginx on the ECS to Implement URL Forwarding

The following procedure describes how to redirect a test domain name (www.example.cn) to the Huawei Cloud official website (https://www.huaweicloud.com/xxx).

  1. Open the Nginx configuration file and modify the server_name and location parameters in the server module.

    Run the following command to edit the nginx.conf file. Replace the path in the command with the actual Nginx installation path.

    vi /usr/local/nginx/conf/nginx.conf

    server_name: indicates the domain name to be accessed.

    location: indicates the URL to be redirected to. 301 is a permanent redirection, and 302 is a temporary redirection. In this example, 302 is used. For details about the differences, see Background. You can also modify the configuration if needed.

    The domain name and URL in Figure 1 are only examples. Replace them based on site requirements.

    Figure 1 Nginx configuration before modification
    Figure 2 Nginx configuration after modification
  1. Run the following command to reload the configuration file and restart Nginx:

    /usr/local/nginx/sbin/nginx -s reload

    /usr/local/nginx/sbin/nginx -s reopen

Step 3: Add Record Sets That Map the Domain Name to the ECS IP Address

  1. Log in to the DNS management console. In the navigation pane on the left, choose Public Zones.
  1. Click Create Public Zone and add the domain name to the DNS console.
  2. Click the domain name to go to the Record Sets page.
  3. Click Add Record Set.
    • Name: Leave it blank.
    • Type: Select A – Map domains to IPv4 addresses.
    • Value: Enter the ECS IP address.

Step 4: Check Whether URL Forwarding Takes Effect

Run the following command to check whether URL forwarding takes effect:

curl -v http://www.example.cn

If the command output is the same as the preset URL, the configured URL forwarding takes effect.

Figure 3 URL forwarding that takes effect