Server Load Balancer (SLB) 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 load balancers. 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-based forwarding rules include exact matching and wildcard matching.
  • Exact domain name: www.aliyun.com
  • Wildcard domain name: *.aliyun.com, *.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 displays the priority of domain-based forwarding rules.

    Type Request URL Domain-based forwarding rule
    www.aliyun.com *.aliyun.com *.market.aliyun.com
    Exact matching www.aliyun.com × ×
    Exact wildcard matching market.aliyun.com × ×
    Less exact wildcard matching 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 consists of multiple ECS instances. For example, you can configure a listener to forward read requests to one VServer group and write requests to another VServer group. This allows you to optimize load balancing among your server resources.

After you configure forwarding rules, SLB applies the rules to distribute traffic based on the following models:

  • Model 1: Requests that contain domain names are forwarded based on domain-based forwarding rules.
    • If a forwarding rule matches the domain name, the system continues to match the URL.

      If the URL is matched, requests are forwarded to the target VServer group. If no forwarding rule matches the URL, the requests are forwarded based only on root domain-based forwarding rules that contain only domain names.

      If you do not add root domain-based forwarding rules, the error message 404 is returned to the client in this case.

    • If no forwarding rule matches the domain name, requests are forwarded in Model 2.
  • Model 2: If the requests do not contain domain names or no forwarding rule matches the domain name, requests are forwarded based on URL-based forwarding rules that contain only URLs.

    If a forwarding rule is matched, requests are forwarded to the target VServer group. If no forwarding rule is matched, requests are forwarded to the default VServer group.

Add domain-based and URL-based forwarding rules

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

Take the following steps to configure domain-based and URL-based forwarding rules.

  1. Log on to the SLB console.
  2. Select the region where the SLB instance is created.
  3. Click the instance ID.
  4. On the page that appears, click the Listener tab.
  5. Find the target Layer-7 listener, click Set Forwarding Rule. Set Forwarding Rule
  6. In the Add Forwarding Rules dialog box, set the following parameters:
    1. Domain Name: Enter the domain name to which requests are forwarded. The domain name can contain letters, digits, hyphens (-), and periods (.).
    2. URL: Enter the URL to which requests are forwarded. The URL must start with a forward slash (/), and can contain letters, digits, hyphens (-), periods (.), forward slashes (/), percent signs (%), question marks (?), number signs (#), and ampersands (&).
      Note
      • If the URL in a request contains special characters, you must use URL escape codes to convert special characters. For example, if a listener forwards requests by matching a URL that contains the special character (/#/), you must convert the special character (#) to the escape code (% 23) first, and use the escape code (% 23) in the requested URL. Then, the requests can be forwarded to the target server based on the specified forwarding rule.
    3. VServer Group: Select the VServer Group to be associated with the forwarding rule.
    4. Description: Enter a description.
    5. Click Add Forwarding Rules.
    Add forwarding rules
  7. 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 specified for an HTTP or HTTPS listener, see Limits.

Edit forwarding rules

You can change the backend servers associated with forwarding rules.

Take the following steps to edit forwarding rules.
  1. Log on to the SLB console.
  2. Select the region where the target SLB instance is created.
  3. Find the target SLB instance and click the instance ID.
  4. On the page that appears, click the Listener tab.
  5. Find the target Layer-7 listener, click Set Forwarding Rule.
  6. In the Forwarding Rules section in the Add Forwarding Rules dialog box, find the target forwarding rule and click Edit.
  7. Set the Scheduling Algorithm, Session Persistence, and Health Check parameters based on the following information.
    Note You can configure the preceding settings only for SLB instances in the following regions.
    • China (Beijing)
    • China (Hangzhou)
    • China (Shanghai)
    • China (Zhangjiakou-Beijing Winter Olympics)
    • China (Hohhot)
    • China (Hong Kong)
    • Singapore (Singapore)
    • Japan (Tokyo)
    Advanced setting Description
    Scheduling Algorithm SLB supports the following scheduling algorithms: round robin, weighted round robin (WRR), and weighted least connections (WLC).
    • Weighted Round-Robin (WRR): A backend server with a higher weight is more likely to be scheduled and receives more requests.
    • Round Robin: Requests are sequentially distributed to backend servers.
    • Weighted Least Connections (WLC): Requests are distributed to backend servers based on the combination of the weight and the number of active connections to the backend servers. If two backend servers have the same weight, the backend server with fewer connections is more likely to be scheduled and receives more requests.
    Enable Session Persistence

    Specify whether to enable session persistence.

    After session persistence is enabled, the specified listener forwards all requests from the same client to a specific backend server for the duration of a session.

    HTTP session persistence is implemented through cookies. SLB allows you to use the following methods to process cookies:

    • Insert cookie: You only need to specify the cookie timeout period.

      SLB inserts a cookie (SERVERID) into the first HTTP or HTTPS response packet that is sent to a client. The next request from the client will contain this cookie, and the listener will distribute this request to the recorded backend server.

    • Rewrite cookie: You can specify the cookie that will be inserted into an HTTP or HTTPS response to meet your specific needs. You must maintain the timeout period and lifecycle of this cookie on the backend server.

      SLB overwrites the original cookie with a new cookie when the new cookie is specified. The next time the client carries the new cookie to access SLB, the listener will distribute the request to the recorded backend server. For more information, see Configure session persistence rules.

    Enable Health Check
    • Health Check Port: Set the detection port used by the health check to access backend servers.

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

    • Health Check Path: The URI for health checks. We recommend that you use health check on static web pages.
    • Health Check Domain Name (Optional): The internal 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 waiting time for a health check response. If the backend ECS instance does not send an expected response within the specified response timeout period, the health check fails.
    • Health Check Interval: Specify the interval between two consecutive health checks.

      The default value is 2 seconds.

    • Unhealthy Threshold: Specify the number of consecutive failed health checks that must occur before an ECS instance is deemed unhealthy.

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

    • Healthy Threshold: Specify the number of consecutive successful health checks that must occur before an ECS instance is deemed healthy.

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

  8. Click OK.

Delete a forwarding rule

Take the following steps to delete a forwarding rule.
  1. Log on to the SLB console.
  2. Select the region where the target SLB instance is created.
  3. Find the target SLB instance and click the instance ID.
  4. On the page that appears, click the Listener tab.
  5. Find the target Layer-7 listener, click Set Forwarding Rule.
  6. In the Forwarding Rules section in the Add Forwarding Rules dialog box, find the target forwarding rule and click Delete.