All Products
Search
Document Center

Server Load Balancer:Configure domain name-based and URL-based forwarding rules for ALB

Last Updated:Sep 02, 2024

This topic describes how to configure domain name-based and path-based forwarding conditions for Application Load Balancer (ALB) listeners. This topic also describes how to configure paths for rewrites and redirects.

Domain name-based forwarding rules

You can specify a specific domain name, a wildcard domain name, or a regular expression as the match condition in a domain name-based forwarding rule. For more information about how to create a forwarding rule, see the Create a forwarding rule section of the Manage forwarding rules for a listener topic.

Match type

Description

Exact match and wildcard match

  • Match conditions

    • Exact match: The requested domain name must be the same as the specified domain name.

    • Wildcard match: The requested domain name must match the wildcard pattern of the specified domain name.

  • Requirements

    The domain name must be 3 to 128 characters in length and can contain only letters, digits, and the following special characters: . - ? = ~ _ + \ ^ * ! $ & | ( ) [ ]. You can use asterisks (*) and question marks (?) as wildcard characters.

  • Examples

    Requested domain name: www.example.com

    • Exact match: The requested domain name is matched only if www.example.com is specified in the forwarding rule.

    • Wildcard match: The requested domain name is matched if *.example.com or www.example.* is specified in the forwarding rule.

Regular expression match

  • Match conditions

    The requested domain name must match the specified regular expression.

  • Requirements

    The domain name must be 3 to 128 characters in length and can contain only letters, digits, and the following special characters: . - ? = ~ _ - + \ ^ * ! $ & | ( ) [ ].

  • Examples

    Requested domain name: www.example.com

    Case-insensitive regular expression: The requested domain name is matched if ^www.example.com$ is specified in the forwarding rule.

URL-based forwarding rules

You can specify a specific URL, a wildcard URL, or a regular expression as the match condition in a URL-based forwarding rule. For more information about how to create a forwarding rule, see the Create a forwarding rule section of the " Manage forwarding rules for a listener" topic.

Match type

Description

Exact match and wildcard match

  • Match conditions

    • Exact match: The requested path must be the same as the specified path.

    • Wildcard match: The requested path must match the wildcard pattern of the specified path.

  • Requirements

    The path must start with a forward slash (/) and can contain only letters, digits, and the following special characters: $ - _ . + / & ~ @ :. You can use asterisks (*) and question marks (?) as wildcard characters.

  • Examples

    Requested path: /example/text

    • Exact match: The requested path is matched only if /example/text is specified in the forwarding rule.

    • Wildcard match: The requested path is matched if /example/* is specified in the forwarding rule.

    Note

    The path matching rules of ALB are different from the rules of NGINX. ALB does not support the longest prefix match rule.

    For example, a common path configured for NGINX is location /abc. The longest prefix match rule is used to match the path. To perform longest prefix match, ALB uses wildcard characters. You can configure /abc/* in ALB to perform longest prefix match.

Regular expression match

  • Match conditions

    The requested path matches the specified regular expression.

  • Requirements

    The regular expression can contain only letters, digits, and the following special characters: . - _ / = ? ~ ^ * $ : ( ) [ ] + |.

  • Example

    Requested path: /sys/aaa/HOST

    • Case-sensitive regular expression: The requested path is matched if ^/sys/(.*)/HOST$ is specified in the forwarding rule.

    • Case-insensitive regular expression: The requested path is matched if ^/sys/(.*)/host$ is specified in the forwarding rule.

Advanced URL-based forwarding rule settings for rewrites and redirects

If you specify a regular expression as the match condition in a rewrite or redirect rule, the original URL is overwritten by the variables that match the regular expression. For more information about how to create a forwarding rule, see the Create a forwarding rule section of the Manage forwarding rules for a listener topic.

Note

For more information about how to configure regular expressions in URL-based forwarding conditions, see the URL-based forwarding rules section of this topic.

  • Usage notes

    • You can specify one or more regular expressions as the match conditions in a rewrite or redirect rule and use parentheses ( ) to define them as capture groups. The number of capture groups must be the same as the number of variables in the rewrite or redirect rule.

    • You can use up to three variables (${1}, ${2}, and ${3}) to form the URL that overwrites the original URL. The variables cannot be replaced with other characters.

  • Procedure

    1. URL matching: The client sends a request. The request matches a regular expression that is specified in a forwarding rule.

    2. Extraction and replacement: extracts the content from the capture groups ( ) and writes the content to ${1}, ${2}, and ${3}. The content is used to replace the rewriting or redirection path of the forwarding action.

    3. Splicing: splices the strings that overwrote the variables ${1}, ${2}, and ${3} to form the new URL.

    No.

    Step

    Example

    1

    Configure the forwarding condition and the forwarding action in a forwarding rule.

    • Path in the forwarding condition: /sys/(.*)/(.*)/aaa

    • Path to which requests are rewritten or redirected: /${1}/${2}

    2

    A client request matches the path in the forwarding rule.

    • Requested path: /sys/ccc/bbb/aaa

    • Matched path: /sys/(.*)/(.*)/aaa

    3

    The system extracts values from the regular expression and replaces the variables with the values.

    ccc and bbb are extracted from (.*) in the regular expression and replace the variables ${1} and ${2} in the path to which requests are rewritten or redirected.

    • ${1} is replaced by ccc.

    • ${2} is replaced by bbb.

    4

    The values are concatenated to form the actual path.

    The path received by the backend server is /ccc/bbb.

  • Examples

    You can create forwarding rules in the ALB console based on the usage notes and procedure. The following examples show how to configure forwarding rules.

    Example 1: Configure the Rewrite or Forward action示例1

    Parameter

    Description

    If Match All Conditions

    Regular expression match

    • Match conditions

      The requested path matches the specified regular expression.

    • Requirements

      The regular expression can contain only letters, digits, and the following special characters: . - _ / = ? ~ ^ * $ : ( ) [ ] + |.

    • Examples

      Requested path: /sys/ccc/bbb/aaa

      • Case-sensitive regular expression: The requested path is matched if /sys/(.*)/(.*)/aaa is specified in the forwarding rule.

      • Case-insensitive regular expression: The requested path is matched if /sys/(.*)/(.*)/aaa is specified in the forwarding rule.

    Action

    Rewrite

    • Domain Name: ${host}

    • Path: /${1}/${2}

    • Search: ${query}

    Forward

    Select a server group from the drop-down list.

    Example 2: Configure the Redirect action示例2

    Parameter

    Description

    If Match All Conditions

    Regular expression match

    • Match conditions

      The requested path matches the specified regular expression.

    • Requirements

      The regular expression can contain only letters, digits, and the following special characters: . - _ / = ? ~ ^ * $ : ( ) [ ] + |.

    • Examples

      Requested path: /sys/ccc/bbb/aaa

      • Case-sensitive regular expression: The requested path is matched if /sys/(.*)/(.*)/aaa is specified in the forwarding rule.

      • Case-insensitive regular expression: The requested path is matched if /sys/(.*)/(.*)/aaa is specified in the forwarding rule.

    Action

    Redirect

    • Protocol: $protocol

    • Domain Name: ${host}

    • Port: ${port}

    • Path: /${1}/${2}

    • Search: ${query}

    • Status Code: 301

References

For more information about how to configure other forwarding rules for an ALB listener, see Configure forwarding rules for an ALB listener.