Updated on 2024-04-19 GMT+08:00

Advanced Forwarding

Overview

Advanced forwarding policies are available only for dedicated load balancers. If you have enabled Advanced Forwarding, you can add advanced forwarding policies to HTTP and HTTPS listeners of dedicated load balancers.

You can add advanced forwarding policies to HTTP or HTTPS listeners to forward requests to different backend server groups based on HTTP request method, HTTP header, query string, or CIDR block in addition to domain names and URLs. Table 1 describes the rules and actions that you can configure for request forwarding.

Figure 1 How advanced forwarding works

The following describes how an advanced forwarding policy works:

  1. The client sends a request to the load balancer.
  2. The load balancer matches the request based on the forwarding rule you configure.
  3. The load balancer forwards the request to the corresponding backend server or returns a fixed response to the client based on the action you configure.
  4. The load balancer sends a response to the client.
Table 1 Rules and actions supported by an advanced forwarding policy

Forwarding Policy

Description

Forwarding rule

There are six types of forwarding rules: domain name, URL, HTTP request method, HTTP header, query string, and CIDR block

For details, see Forwarding Rule.

Action

The following actions are supported: forward to a backend server group, redirect to another listener, redirect to another URL, and return a specific response body.

For details, see Action Types.

How Requests Are Matched

After you add an HTTP or HTTPS listener to a load balancer, a default forwarding policy is generated. This policy uses the protocol and port specified for the listener to match requests and forward the requests to the backend server group you specified when adding the listener.

The default forwarding policy has the lowest priority and is not included when you sort forwarding policies. It can be edited but cannot be deleted.

Each request is matched based on the forwarding policy priority (a smaller value indicates a higher priority). Once a forwarding policy is matched, the request is forwarded based on this forwarding policy.

  • If the request is matched with any forwarding policy of the listener, it is forwarded based on this forwarding policy.
  • If the request is not matched with any forwarding policy, it is forwarded based on the default forwarding policy.

Forwarding Rule

Advanced forwarding policies support the following types of forwarding rules: domain name, URL, HTTP request method, HTTP header, query string, and CIDR block (source IP addresses).

Table 2 Forwarding rules

Forwarding Rule

Description

Domain name

  • Description

    Route requests based on the domain name.

    • You can configure multiple domain names in a forwarding policy. Each domain name contains at least two labels separated by periods (.). Max total: 100 characters. Max label: 63 characters.
    • Each label can contain letters, digits, hyphens (-), periods (.), and asterisks (*). A label must start with a letter, digit, or asterisk (*) and cannot end with a hyphen (-). An asterisk (*) must be used as the leftmost label if you want to configure a wildcard domain name.
  • Matching rules

    Exact match domains and wildcard domains are supported.

Example
Request URL: https://www.example.com/login.php?locale=en-us=#videos
Domain name in the forwarding rule: www.example.com

URL

  • Description

    Route requests based on URLs. You can configure multiple URLs in a forwarding policy. A URL can contain letters, digits, and special characters _~';@^-%#$.*+?,=!:|\/()[]{}.

    If the URL contains special characters such as question marks (?) or pound keys (#), escape the special characters before configuring the forwarding rule.

  • Matching rules
    • Exact match: The request URL must exactly match that specified in the forwarding policy. The URL must start with a slash (/) and can use asterisks (*) and question marks (?) as wildcards.
    • Prefix match: The requested URL starts with the specified URL string. The URL must start with a slash (/) and can use asterisks (*) and question marks (?) as wildcard characters.
    • Regular expression match: The URLs are matched using a regular expression.

For more information about URL matching rules, see URL Matching.

Example
Request URL: https://www.example.com/login.php?locale=en-us#videos
URL in the forwarding rule: /login.php

Query string

Route requests based on the query string.

A query string consists of a key and one or more values. You need to set the key and values separately.
  • The key can contain only letters, digits, and special characters !$'()*+,./:;=?@^-_'
  • A key can have one or more values. The value can contain letters, digits, and special characters !$'()*+,./:;=?@^-_' Asterisks (*) and question marks (?) can be used as wildcard characters.
Example
Request URL: https://www.example.com/login.php?locale=en-us#videos
A query string needs to be configured for the forwarding rule:
Key: locale
Value: en-us

HTTP request method

Route requests based on the HTTP method.

  • You can configure multiple request methods in a forwarding policy.
  • The following methods are available: GET, POST, PUT, DELETE, PATCH, HEAD, and OPTIONS.
Example
GET

HTTP header

Route requests based on the HTTP header.

An HTTP header consists of a key and one or more values. You need to configure the key and values separately.
  • The key can contain only letters, digits, underscores (_), and hyphens (-).
  • A key can have one or more values. The value can contain letters, digits, and special characters !#$%&'()*+,.\/:;<=>?@[]^-_'{|}~ Asterisks (*) and question marks (?) can be used as wildcard characters.
Example
Key: Accept-Language
Value: en-us

CIDR block

Route requests based on the source IP addresses from where requests originate.

Example
192.168.1.0/24 or 2020:50::44/127

Action Types

Advanced forwarding policies support the following actions: forward to a backend server group, redirect to another listener, redirect to another URL, and return a specific response body.

Table 3 Actions of an advanced forwarding policy

Action

Description

Forward to a backend server group

Requests are forwarded to the specified backend server group.

Redirect to another listener

Requests are redirected to another listener, which then routes the requests to its associated backend server group.

NOTE:

If you select Redirect to another listener and create a redirect for the listener, it will redirect the requests to the specified HTTPS listener, but access control configured for the listener will still take effect.

For example, if you configure a redirect for an HTTP listener, HTTP requests to access a web page will be redirected to the HTTPS listener you select and handled by the backend servers associated with the HTTPS listener. As a result, the clients access the web page over HTTPS. The configuration of the HTTP listener will become invalid.

Redirect to another URL

Requests are redirected to the configured URL.

When clients access website A, the load balancer returns 302 or any other 3xx status code and automatically redirects the clients to website B. You can custom the redirection URL that will be returned to the clients.

Configure at least one of the following components:
  • Protocol: ${protocol}, HTTP, or HTTPS ${protocol}: retains the protocol of the request.
  • Domain Name: A domain name consists of at least two labels separated by periods (.). Each label can contain only letters, digits, hyphens (-), and periods (.), must start with a letter, digit, or asterisk (*), and cannot end with a hyphen (-). ${host}: retains the domain name of the request.
  • Port: ranges from 1 to 65535. ${port}: retains the port number of the request.
  • Path: A path can contain letters, digits, and special characters _~';@^-%#&$.*+?,=!:|\/()[]{} and must start with a slash (/). ${path}: retains the path of the request.
    NOTE:

    If you select regular expression match, the request path will be overwritten by the variables that match the regular expressions. For details, see URL Matching Based on Regular Expressions.

  • Query String: A query string can contain only letters, digits, and special characters !$'()*+,./:;=?@&^-_'. Ampersand (&) can only be used as separators.
  • HTTP Status Code: 301, 302, 303, 307, or 308
Example
URL for redirection: http://www.example1.com/index.html?locale=en-us#videos
Protocol: HTTP
Domain name: www.example1.com
Port: 8081
Path: /index.html
Query String: locale=en-us
HTTP Status Code: 301

Return a specific response body

Load balancers return a fixed response to the clients.

You can custom the status code and response body that load balancers directly return to the clients without the need to route the requests to backend servers.

Configure the following components:
  • HTTP Status Code: By default, 2xx, 4xx, and 5xx status codes are supported.
  • Content-Type: text/plain, text/css, text/html, application/javascript, or application/json
  • Message Body: This parameter is optional. The value is a string of 0 to 1,024 characters.

Example

text/plain

Sorry, the language is not supported.

text/css

<head><style type="text/css">div {background-color:red}#div {font-size:15px;color:red}</style></head>

text/html

<form action="/" method="post" enctype="multipart/form-data"><input type="text" name="description" value="some text"><input type="file" name="myFile"><button type="submit">Submit</button></form>

application/javascript

String.prototype.trim = function() {var reExtraSpace = /^\s*(.*?)\s+$/;return this.replace(reExtraSpace, "$1")}

application/json

{ "publicip": { "type": "5_bgp","ip_version": 4},"bandwidth": {"name": "bandwidth123","size": 10,"share_type": "PER"}}
NOTE:

Ensure that the response body does not contain carriage return characters. Otherwise, it cannot be saved.

URL Matching

Table 4 shows how URLs configured in the forwarding policies match the URLs in the requests.

Table 4 URL matching examples

Request URL

Forwarding Policy

URL in the Forwarding Policy

Matching Mode

Forwarding Policy Priority

Destination Backend Server Group

/elb/abc.html

Forwarding policy 01

/elb/abc.html

Prefix match

1

Backend server group 01

Forwarding policy 02

/elb

Prefix match

2

Backend server group 02

/exa/index.html

Forwarding policy 03

/exa[^\s]*

Regular expression match

3

Backend server group 03

Forwarding policy 04

/exa/index.html

Regular expression match

4

Backend server group 04

/mpl/index.html

Forwarding policy 05

/mpl/index.html

Exact match

5

Backend server group 05

URLs are matched as follows:

  • When the request URL is /elb/abc.html, it matches both forwarding policy 01 and forwarding policy 02. However, the priority of forwarding policy 01 is higher than that of forwarding policy 02. Forwarding policy 01 is used, and requests are forwarded to backend server group 01.
  • When the request URL is /exa/index.html, it matches both forwarding policy 03 and forwarding policy 04. However, the priority of forwarding policy 03 is higher than that of forwarding policy 04. Forwarding policy 03 is used, and requests are forwarded to backend server group 03.
  • If the request URL is /mpl/index.html, it matches forwarding policy 05 exactly, and requests are forwarded to backend server group 05.

URL Matching Based on Regular Expressions

A path can contain letters, digits, and special characters _~';@^-%#&$.*+?,=!:|\/()[]{} and must start with a slash (/). ${path} retains the path of the request.

If you select regular expression match, the request path will be overwritten by the variables that match the regular expressions.

How Request Paths Are Overwritten

  1. URL matching: The client sends a request, and the request matches a regular expression in the forwarding rule. You can specify one or more regular expressions as the match conditions and set multiple capture groups represented by parentheses ( ) for one regular expression.
  2. Extraction and replacement: extracts the content from the capture groups.
  3. Destination path: writes them to $1, $2, all the way to $9 configured for the path.

Example

When a client requests to access /test/ELB/elb/index, which matches the regular expression /test/(.*)/(.*)/index, $1 will be replaced by ELB and $2 by elb, and then the request will be redirected to /ELB/elb.

Table 5 URL matching based on regular expressions

Matching Step

Description

Forwarding rule: URL

Regular expression match

  • Matching condition: /test/(.*)/(.*)/index
  • Request URL: /test/ELB/elb/index

Action: redirect to another URL

Path

  • Path: /$1/$2
  • Extracting content

    $1: ELB

    $2: elb

  • Destination path: /ELB/elb