All Products
Search
Document Center

Server Load Balancer:Forward requests based on domain names or URLs

Last Updated:Oct 25, 2023

The Layer 7 listeners of Classic Load Balancer (CLB), including HTTPS listeners and HTTP listeners, support forwarding rules based on domain names or URLs. You can forward requests from different domain names or URLs to different backend server groups. This ensures that server resources are allocated in an optimized manner.

How it works

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

Domain name-based forwarding rules support exact matching and wildcard pattern matching.

  • Exact domain name: www.aliyun.com.

  • Wildcard domain names: *.aliyun.com and *.market.aliyun.com.

    If a request matches multiple domain name-based forwarding rules, exact matching prevails over wildcard pattern matching. If a request matches multiple wildcard domain names, a higher-level wildcard domain name prevails over a lower-level wildcard domain name.

    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.

    Mode

    Request URL

    Domain name-based forwarding rule

    www.aliyun.com

    *.aliyun.com

    *.market.aliyun.com

    Exact matching

    www.aliyun.com

    ×

    ×

    Wildcard pattern 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 Elastic Compute Service (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 name-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 no root domain-based forwarding rules are 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 by using 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 server group that is configured for the listener.

Prerequisites

Configure domain name-based or URL-based forwarding rules

  1. Log on to the CLB console.

  2. Select the region to which the CLB instance that you want to manage belongs.

  3. On the Instances page, click the ID of the CLB instance.

  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 Routing Method panel, configure the following parameters and click Add Forwarding Policy.

    • Domain Name: Enter the domain name from which requests are forwarded.

    • URL: Enter the URL from which requests are forwarded.

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

    • Remarks: Enter a description.

  6. Optional: Click Add Domain Name or Add Rule to add a domain name or 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 associated with forwarding rules.

  1. Log on to the CLB console.

  2. Select the region to which the CLB instance that you want to manage belongs.

  3. On the Instances page, click the ID of the CLB instance.

  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 Routing Method panel, find the forwarding rule that you want to manage and click Modify in the Actions column.

  6. In the Modify Forwarding Rule panel, change the description and the vServer group.

  7. You can turn on Advanced Setting, configure the following parameters, and then click OK.

    Parameter

    Description

    Scheduling Algorithm

    Select a scheduling algorithm.

    • Weighted Round-robin (WRR): Backend servers that have higher weights receive more requests than those that have lower weights.

    • Round Robin (RR): Requests are distributed to backend servers in sequence.

    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.

    CLB enables persistence of HTTP sessions based on cookies. CLB allows you to use the following methods to process cookies:

    Cookie Option:

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

      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 the cookie, and the listener forwards the request to the recorded backend server.

      Session Persistence Timeout Period: If you select Insert Cookie, specify a timeout period for session persistence.

    • Rewrite Cookie: If you select this option, you must specify the cookie that you want to insert into an HTTP or HTTPS response. In this case, you must specify the timeout period and lifetime of a cookie on a backend server.

      After you specify a cookie, CLB overwrites the original cookie with the specified cookie. The next time CLB receives a client request that carries the specified cookie, the listener distributes the request to the recorded backend server.

      Cookie Name: If you select Rewrite Cookie, you must specify a name for the cookie.

    For more information, see Configure server cookies.

    Health Check

    • Health Check Method: The default value is HEAD.

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

      By default, the backend port specified in the listener configuration is probed.

    • Health Check Path: Specify the URI of the page that is used for health checks. We recommend that you perform health checks on static web pages.

    • Health Check Domain Name (Optional): Specify the domain name that is used for health checks. By default, the private IP addresses of backend servers are converted into domain names that are used for health checks.

    • Healthy Status Codes: Select the HTTP status codes that indicate a successful health check.

      By default, http_2xx and http_3xx are selected.

    • Health Check Response Timeout: Specify the timeout period for a health check response. If a backend server does not respond within the specified timeout period, the backend server fails the health check. Default value: 5. Unit: seconds.

    • Health Check Interval: Specify the time interval between two consecutive health checks.

      Default value: 2. Unit: seconds.

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

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

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

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

Delete a forwarding rule

  1. Log on to the CLB console.

  2. Select the region to which the CLB instance that you want to manage belongs.

  3. On the Instances page, click the ID of the CLB instance.

  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 Routing Method panel, find the forwarding rule that you want to delete and click Delete in the Actions column.

References