Classic Load Balancer (CLB) supports domain-based and URL-based forwarding rules. You can forward requests from different domain names or URLs to separate backend servers. This allows you to optimize load balancing among your server resources.

Note You can configure forwarding rules only for Layer 7 listeners that use HTTPS or HTTP.

Introduction

You can configure domain-based or URL-based forwarding rules for Layer 7 Classic Load Balancer (CLB) instances. After you configure the rules, requests are distributed to Elastic Compute Service (ECS) instances based on domain names or URLs.

URL-based forwarding rules support string matching. Longest prefix matching is applied. For example, if two forwarding rules /abc and /abcd are configured and a request with the prefix of /abcde is received, the rule /abcd is applied.

Domain name-based forwarding rules support exact matching and wildcard matching.
  • Exact domain name: www.aliyun.com.
  • Wildcard domain name: *.aliyun.com and *.market.aliyun.com.

    When a request matches multiple forwarding rules, exact matching prevails over wildcard matching, and exact wildcard matching prevails over less exact wildcard matching. The following table shows the priorities of domain name-based forwarding rules.

    Note "√" indicates that the domain name of the request matches the domain name specified in the forwarding rule. "×" indicates that the domain name of the request does not match the domain name specified in the forwarding rule.
    Type Request URL Domain name-based forwarding rule
    www.aliyun.com *.aliyun.com *.market.aliyun.com
    Exact matching www.aliyun.com × ×
    Wildcard matching market.aliyun.com × ×
    info.market.aliyun.com × ×

You can add multiple forwarding rules to a listener. Each forwarding rule is associated with a vServer group. A vServer group contains one or more ECS instances. You can configure a listener to forward read requests to one vServer group and forward write requests to another vServer group. This allows you to optimize load balancing among your server resources.

After you configure forwarding rules, CLB applies the rules to distribute traffic by using the following models:

  • Model 1: If a request contains a domain name, the system matches the domain name of the request against domain-based forwarding rules.
    • If the domain name of the request matches the domain name specified in a forwarding rule, the system continues to match the URL.

      In this case, if the URL of the request matches the URL specified in a forwarding rule, the request is forwarded to the vServer group specified in this forwarding rule. If no forwarding rule matches the URL, the request is forwarded based on a root domain-based forwarding rule. Root domain-based forwarding rules contain only domain names. URLs are not specified in root domain-based forwarding rules.

      If root domain-based forwarding rules are not configured for the domain name, the error message 404 is returned to the client.

    • If no forwarding rule matches the domain name of the request, the request is forwarded in Model 2.
  • Model 2: If a request does not contain a domain name or no forwarding rule matches the domain name, the request is forwarded based on URL-based forwarding rules. URL-based forwarding rules contain only URLs. Domain names are not specified in URL-based forwarding rules.

    In this case, if the URL of the request matches the URL specified in a URL-based forwarding rule, the request is forwarded to the vServer group specified in the forwarding rule. If no URL-based forwarding rule matches the URL, the request is forwarded to the default server group.

Configure domain-based or URL-based forwarding rules

Before you configure forwarding rules for a listener, make sure that the following requirements are met:

To configure domain-based and URL-based forwarding rules, perform the following steps:

  1. Log on to the CLB console.
  2. Select the region where the CLB instance that you want to manage is deployed.
  3. Find the CLB instance that you want to manage and click its ID.
  4. On the Listener tab, find the Layer 7 listener that you want to manage, and click Set Forwarding Rule in the Actions column.
  5. In the Add Forwarding Rules panel, set the following parameters:
    • Domain Name: Enter the domain name to which requests are forwarded. The domain name can contain only letters, digits, hyphens (-), and periods (.).
    • URL: Enter the URL to which requests are forwarded. The URL must start with a forward slash (/). The URL can contain only letters, digits, hyphens (-), periods (.), forward slashes (/), percent signs (%), question marks (?), number signs (#), and ampersands (&).
      Note If the URL of a request contains special characters, you must use URL escape codes to convert the special characters. For example, if the URL of a request contains a number sign (#), the number sign must be converted to the escape code (% 23). This way, the system can match the URL of the request against the URL-based forwarding rule.
    • VServer Group: Select the vServer group that you want to associate with the forwarding rule.
    • Description: Enter a description.
    • Click Add Forwarding Rules.
  6. Click Add Domain to add a domain-based forwarding rule, or click Add Rule to add a URL-based forwarding rule.

    For more information about the maximum number of forwarding rules that can be configured for an HTTP or HTTPS listener, see Limits.

Modify forwarding rules

You can change the backend servers specified in forwarding rules.
  1. Log on to the CLB console.
  2. Select the region where the CLB instance that you want to manage is deployed.
  3. On the Instances page, find the CLB instance that you want to manage and click its ID.
  4. On the Listener tab, find the Layer 7 listener that you want to manage and click Set Forwarding Rule in the Actions column.
  5. In the Forwarding Rules section of the Add Forwarding Rules panel, find the forwarding rule that you want to manage and click Edit.
  6. You can configure the forwarding rule based on the information in the following table and click OK. For example, you can specify a scheduling algorithm and configure session persistence and health checks.
    Note You can configure the advanced settings only if the CLB instance is deployed in one of the following regions:
    • China:

      China (Qingdao), China (Beijing), China (Hangzhou), China (Shanghai), China (Zhangjiakou), China (Hohhot), China (Chengdu), China (Shenzhen), and China (Hong Kong).

    • Asia Pacific:

      Japan (Tokyo), Singapore (Singapore), Australia (Sydney), Malaysia (Kuala Lumpur), and Indonesia (Jakarta).

    • Europe and Americas:

      Germany (Frankfurt), US (Virginia), and US (Silicon Valley).

    • Middle East and India:

      India (Mumbai) and UAE (Dubai)

    Advanced setting Description
    Scheduling Algorithm Select a scheduling algorithm.
    • Weighted Round-Robin: Backend servers that have higher weights receive more requests than backend servers that have lower weights.
    • Round Robin: Requests are distributed to backend servers in sequence.
    Enable Session Persistence

    Specify whether to enable session persistence.

    After session persistence is enabled, CLB forwards all requests from a client to the same backend server.

    HTTP session persistence is implemented through cookies. You can use one of the following methods to handle cookies:

    • Insert cookie: If you select this option, you need only to specify the timeout period of cookies.

      CLB inserts a cookie (SERVERID) into the first HTTP or HTTPS response that is sent to a client. The next request from the client contains this cookie and the listener forwards this request to the recorded backend server.

    • Rewrite cookie: If you select this option, you can specify the cookie to be inserted into an HTTP or HTTPS response. You must specify the timeout period and lifetime of cookies on backend servers.

      When CLB detects a user-defined cookie, CLB rewrites the original cookie with the user-defined cookie. The next request from the client contains the user-defined cookie, and the listener forwards the request to the recorded backend server. For more information, see Configure session persistence.

    Enable Health Check
    • Health Check Port: Set the port that is used by health checks to access backend servers.

      By default, the backend port configured for the listener is used for health checks.

    • Health Check Path: The URL that is used for health checks. We recommend that you perform health checks on static web pages.
    • Health Check Domain Name (Optional): The private IP addresses of backend servers are converted into the domain names that are used for health checks.
    • Normal Status Code: Select the HTTP status code that indicates a successful health check.

      Default values: http_2xx and http_3xx.

    • Response Timeout: Specify the timeout period for a health check response. If a backend ECS instance does not send an expected response within the specified response timeout period, the health check fails.
    • Health Check Interval: Specify the time interval between two consecutive health checks.

      Default value: 2. Unit: seconds.

    • Unhealthy Threshold: Specify the number of failed health checks that must be consecutively performed before an ECS instance can be declared unhealthy.

      Valid values: 2 to 10. Default value: 3.

    • Healthy Threshold: Specify the number of successful health checks that must be consecutively performed before an ECS instance can be declared healthy.

      Valid values: 2 to 10. Default value: 3.

Delete a forwarding rule

To delete a forwarding rule, perform the following operations:
  1. Log on to the CLB console.
  2. Select the region where the CLB instance that you want to manage is deployed.
  3. On the Instances page, find the CLB instance that you want to manage and click its ID.
  4. On the Listener tab, find the Layer 7 listener that you want to manage, and click Set Forwarding Rule in the Actions column.
  5. In the Forwarding Rules section of the Add Forwarding Rules panel, find the forwarding rule that you want to delete and click Delete.