You can create forwarding rules for an ALB listener to control how requests are distributed to one or more backend servers.

Background information

You can create an inbound or an outbound forwarding rule for an ALB listener. Each forwarding rule consists of one or more conditions and one or more actions. Architecture
  • After an ALB instance receives a request, the ALB instance distributes the request to a backend server based on an inbound forwarding rule. Then, the backend server returns a response based on an outbound forwarding rule of the ALB instance.
    • When you create an inbound forwarding rule, you can specify only inbound conditions and actions.
    • When you create an outbound forwarding rule, you can specify both inbound and outbound conditions. However, you can specify only outbound actions in an outbound forwarding rule.
  • You must specify at least one Forward action, one Redirect action, or one Return Fixed Responses action in each forwarding rule.
    • Inbound conditions: Domain Name, Path, HTTP Header, Query String, HTTP Request Method, Cookie, and Source IP Address.
    • Inbound actions: Forward, Redirect, Return Fixed Responses, Rewrite, Add Header, Remove Header, Throttle Traffic, Mirror Traffic, and CORS.
    • Conditions that you can specify in an outbound forwarding rule:
      • Inbound Conditions (Optional): Domain Name, Path, HTTP Header, Query String, HTTP Request Method, Cookie, and Source IP Address.
      • Outbound Conditions: Response Status Code and Response Header.
    • Outbound actions: Add Header and Remove Header.

Limits

  • Basic ALB instances support only inbound forwarding rules. Standard ALB instances and higher support both inbound and outbound forwarding rules.
  • When you create a forwarding rule for a basic ALB instance, you can set the condition to Domain Name, Path, or HTTP Header, and set the action to Forward or Redirect. If you want to specify other conditions and actions, you must first upgrade the ALB instance to the standard edition. For more information, see Modify the configurations of an ALB instance.

Prerequisites

  • A standard ALB instance is created. For more information, see Create an ALB instance.
  • A server group is created and backend servers are added to the server group. For more information, see Manage server groups.

Create a forwarding rule

You can configure the default forwarding rule of a listener or add custom forwarding rules to meet your business requirements.

  1. Log on to the ALB console.
  2. In the top navigation bar, select the region.
  3. On the Instances page, click the ID of the ALB instance that you want to manage.
  4. Click the Listener tab, find the listener that you want to manage, and then click View/Modify Forwarding Rule in the Actions column.
  5. On the Forwarding Rules tab, select Inbound Forwarding Rules or Outbound Forwarding Rules, and then click Add New Rule.
  6. In the Add Forwarding Rules section, set the following parameters and click OK.
    Note
    • The logical operator among the actions in a forwarding rule is OR. If you specify multiple actions for a condition, requests that match one of the actions are forwarded.
    • The logical operator among different conditions is AND. If you specify multiple conditions in a forwarding rule, the action is performed when all conditions are met.
    • Inbound forwarding rules
      Parameter Description
      Name Enter a name for the forwarding rule.

      You can enter only one name for a forwarding rule.

      If (Matching All Conditions) If you specify one of the following conditions, you can click + Add Condition to add more conditions:
      • Domain Name: Enter one or more domain names. The domain name must be 3 to 128 characters in length. You can use asterisks (*) and question marks (?) as wildcards. You can enter a specific domain name, a wildcard domain name, or a regular expression. For more information about how to configure domain name-based forwarding rules, see Limits on domain name-based forwarding rules.

        Example: *.example.com.

      • Path: Enter one or more URL paths. You can enter a specific URL path or a regular expression. For more information about how to configure URL-based forwarding rules, see Limits on URL-based forwarding rules.

        For example, if the URL is www.example.com/test/test1?x=1&y=2, you can set the parameter to /test/*.

      • HTTP Header: Enter the name of an HTTP header in the Key field and the value of the HTTP header in the Value field.

        In this example, the key user-agent and the value *Mozilla/4.0* are entered.

      • Query String: Add key-value pairs of one or more query strings. The key must be 1 to 100 characters in length. The value must be 1 to 128 characters in length. The key and value can contain lowercase letters and other printable characters. You can use asterisks (*) and question marks (?) as wildcards. The key and value cannot contain space characters or the following special characters: # [ ] { } \ | < > &.

        For example, if the URL is www.example.com/test/test1?x=1&y=2, you can set the parameter to x:1 or y:2.

      • HTTP Request Method: Add one or more HTTP request methods.
      • Cookie: Add one or more cookies. The key must be 1 to 100 characters in length. The value must be 1 to 128 characters in length. The key and value can contain lowercase letters and other printable characters. You can use asterisks (*) and question marks (?) as wildcards. The key and value cannot contain space characters or the following special characters: # [ ] { } \ | < > &.

        In this example, the key key and the value value are entered.

      • Source IP Address: Add one or more IP addresses or CIDR blocks.
      Then If you specify one of the following actions, you can click + Add Action to add more actions:
      • Forward: Select a server group from the drop-down list. You can add multiple server groups and enable Session Persistence for Server Groups.
      • Redirect: Specify Protocol, Status Code, Domain Name, Port, Path, and Query.
        Note
      • Return Fixed Responses: Enter an HTTP status code in the Status Code field, select a Response Content Type, and then enter the Response Content.
      • Rewrite: Enter the Domain Name and Path to which requests are forwarded, and then enter a query string in the Query field. For more information about how to configure Path for the Rewrite action, see Upgraded settings for URL-based forwarding rules configured for rewrites and redirects.
      • Add Header: Enter the name and the value of the header that you want to add to the request. Newly added headers overwrite the existing headers in the request.
      • Remove Header: Enter the value of the header that you want to remove from the request.
      • Throttle Traffic: Enter the maximum number of requests per second. Valid values: 1 to 100000. If the requests per second reach the upper limit, new requests are dropped.
      • Mirror Traffic: Select a server group from the drop-down list.
      • CORS: If the response is returned from a URL (including protocol, domain name, and port) different from the one requested by the client, it indicates that Cross-Origin Resource Sharing (CORS) is enabled. Request types in the CORS mechanism: CORS requests and preflight requests.
        • Allowed Origins: Specify the origins that are allowed to access cross-region resources through a browser.
        • Allowed Methods: Specify the HTTP methods that the specified origins can use to access cross-origin resources. Valid values: GET, POST, PUT, DELETE, HEAD, OPTIONS, and PATCH.
        • Allowed Request Headers: Specify the headers that can be carried in CORS requests other than the built-in headers of browsers.
        • Allowed Response Headers: Specify the response headers that browsers and JavaScript can obtain.
        • Allow Credentials: Specify whether to allow credentials in CORS requests. Valid values: Enable and Disable. Default value: Enable.
        • Browser Cache Time: Specify the maximum period of time for which a preflight request that uses the OPTIONS method can be cached. Unit: seconds. Valid values: -1 to 172800.
    • Outbound forwarding rules
      Parameter Description
      Name Enter a name for the forwarding rule.

      You can enter only one name for a forwarding rule.

      Inbound Conditions (Optional) Select an inbound condition. You can click + Add Inbound Condition to add more inbound conditions. For more information about how to configure conditions in inbound forwarding rules, see Create a forwarding rule.
      Outbound Conditions Select an outbound condition. You can click + Add Outbound Condition to add more outbound conditions.
      • Response Status Code: Specify the HTTP status code carried in the response to the client. Valid values: 100 to 599.

        You can enter multiple values by range. Separate the values with commas (,). Example: 200-233,301.

      • Response Header: Specify the HTTP headers carried in the response. Enter the name of the HTTP header in the Key field and the value of the HTTP header in the Value field. You can specify multiple HTTP headers.
      Then Select an outbound action. You can click + Add Action to add more outbound actions.
      • Add Header: Enter the name and the value of the header that you want to add to the response. Newly added headers overwrite the existing headers.
      • Remove Header: Enter the value of the header that you want to remove from the response.
    • Create scripts by using AScript
      You can click + Add Script After Forwarding Rule Is Applied to add a script. For more information, see Configure scripts for forwarding rules.
      Note
      To use the AScript feature, make sure that the following requirements are met:
      1. A standard ALB instance or higher is used.
      2. By default, the AScript feature is disabled. To enable this feature, submit a ticket.

Modify a forwarding rule

  1. Log on to the ALB console.
  2. In the top navigation bar, select the region.
  3. On the Instances page, click the ID of the ALB instance that you want to manage.
  4. Click the Listener tab, find the listener that you want to manage, and then click View/Modify Forwarding Rule in the Actions column.
  5. On the Forwarding Rules tab, select Inbound Forwarding Rules or Outbound Forwarding Rules and then click the The Edit icon icon on the right side of the forwarding rule that you want to modify.
  6. Modify the configurations and click Save.

Modify the priority of a forwarding rule

Forwarding rules are evaluated in priority order. A lower value specifies a higher priority. You can modify the priority of a custom forwarding rule anytime. You cannot modify the priority of the default forwarding rule.

  1. Log on to the ALB console.
  2. In the top navigation bar, select the region.
  3. On the Instances page, click the ID of the ALB instance that you want to manage.
  4. Click the Listener tab, find the listener that you want to manage, and then click View/Modify Forwarding Rule in the Actions column.
  5. On the Forwarding Rules tab, select Inbound Forwarding Rules or Outbound Forwarding Rules. You can modify the priority of a forwarding rule by dragging and dropping the forwarding rule. Then, click Save Priority Changes.

Delete a forwarding rule

You can delete the custom forwarding rules of a listener anytime. The default forwarding rule cannot be deleted. If you delete a listener, all forwarding rules of the listener are deleted.

  1. Log on to the ALB console.
  2. In the top navigation bar, select the region.
  3. On the Instances page, click the ID of the ALB instance that you want to manage.
  4. Click the Listener tab, find the listener that you want to manage, and then click View/Modify Forwarding Rule in the Actions column.
  5. On the Forwarding Rules tab, select Inbound Forwarding Rules or Outbound Forwarding Rules and then click the Delete icon on the right side of the forwarding rule that you want to delete.
  6. In the message that appears, click OK.

References