AScript

Updated at: 2025-03-02 20:57

AScript is a programmable script feature that offers powerful customization capabilities, enabling you to perform secondary development on forwarding rules when the standard configurations in the ALB console do not meet your business requirements.

What is AScript?

AScript provides easy-to-learn syntax and many functions that allow you to create custom ALB configurations.

image

AScript provides encapsulation-free functions, built-in variables, and the if-else statement. You can use variables and functions to create custom forwarding rules. This way, your business can quickly adapt to changes.

For details about the AScript syntax, see AScript syntax.

Use scenarios

  • Hotlink protection

    You can use AScript to customize authentication algorithms, User-Agent (UA) blacklists, and Referer whitelists. AScript allows you to customize the authentication logic and authenticate requests based on request parameters, cookies, or algorithms. This helps you protect your resources against attacks.

  • Access control based on whitelists or blacklists

    You can use AScript to configure an IP blacklist or whitelist to control access from clients.

  • Request and response header customization

    You can use AScript to modify variables, such as request and response headers, based on your business requirements.

  • Request rewrites and redirects

    You can use AScript to rewrite request URIs and file extensions, add URI prefixes, and perform 302 redirects. This way, you can rewrite requests or redirect them to the desired resources.

    A common use case is that your website supports different languages. With Ascript, requests destined for the website can be redirected to the URLs that point to the content written in the requested language. For example, requests that require English or German are redirected from the China site to the English or German site, respectively.

Billing

For information about how the number of rule evaluations affects the Load Balancer Capacity Unit (LCU) fee, see ALB billing rules.

How it works

Working principles

Same as standard forwarding rules, AScript is used to define how requests are processed by ALB.

When a request reaches ALB, ALB processes the request based on the forwarding rules configured in the ALB console. AScript can execute scripts to process the request before or after forwarding rules are applied.

Scripts are executed based on the following model:

  • AScript executes scripts to perform various operations. Operations are triggered only when the conditions in scripts are met.

  • You can specify the position where a script is executed and the priority of the script in a request processing pipeline.

  • Scripts are managed based on listeners.

Execution positions

You can configure scripts to be executed before or after an inbound forwarding rule is applied, or before an outbound forwarding rule is applied. The operations that can be performed vary based on the execution position:

  • Before or after an inbound forwarding rule: renaming frequently used files, converting file extensions to lowercase letters, adding URI prefixes, and rewriting file extensions

  • Before an outbound forwarding rule: renaming frequently used files

Execution status

Parameters defining the execution status of a script are described as follows. You can adjust the scripts and forwarding rules you configured based on the information.

  • Status ID: indicates the execution status of a script.

    The following table describes the valid values of the status ID.

    Status ID

    Description

    Status ID

    Description

    -1

    The default value.

    1

    Indicates the script is not executed.

    2

    Indicates the condition is met.

    This ID is returned when if condition {} is specified in the script and the condition is met.

    3

    Indicates the condition is not met.

    This ID is returned when if condition {} is specified in the script but the condition is not met, or if condition {} is not specified.

    4

    Indicates script execution fails.

    Failure causes:

    • 400: not found inline func %s

    • 401: not found argument %d in %s

    • 402: mismatch string type of arg %d in %s

    • 403: mismatch number type of arg %d in %s

    • 404: mismatch table type of arg %d in %s

    • 405: mismatch boolean type of arg %d in %s

    • 406: mismatch function type of arg %d in %s

    • 407: exceed the exec cputime limit %d-%d us

    • 408: exceed the api call limit %s %d-%d times

    • 409: exceed the max loops limit %d in m3u8_rewrite

    • 410: exceed the max loops limit %d in foreach

    • 499: unknown reason

  • Execution time:

    • Unit: microseconds.

    • Default value: -1

    • Execution time displayed on the web interface:

      • Tier 1: 0 to 100 µs

      • Tier 2: 100 to 500 µs

      • Tier 3: 500 to 1000 µs

      • Tier 4: 1000 to 5000 µs

      • Tier 5: 5000 to 20000 µs

      • Tier 6: 20000 to 50000 µs

      • Tier 7: above 50000 µs

  • Execution suspended:

    Default value: -1.

  • On this page (1)
  • What is AScript?
  • Use scenarios
  • Billing
  • How it works
  • Working principles
  • Execution positions
  • Execution status
Feedback
phone Contact Us