All Products
Search
Document Center

CDN:Rules engine

Last Updated:Sep 18, 2026

The rules engine provides a graphical user interface to configure conditional rules. These rules analyze user request parameters to determine if a specific configuration applies, giving you flexible and precise control over the application of CDN configuration policies.

Background

The Alibaba Cloud CDN console provides basic features, such as configuring cache expiration and rewriting origin-fetch parameters, which meet most general requirements. However, for special requirements, such as fetching requests whose path contains /example from a specified origin address, you must use the rules engine to implement custom configurations. In addition, Alibaba Cloud CDN provides the EdgeScript feature, which supports highly flexible customization.

Capabilities

Basic features

Basic features and rules engine

EdgeScript

Feature implementation

General-purpose configurations

Lets you create conditional filtering rules using a graphical interface. It supports multiple match types, such as URI, Header, Cookie, and Query String, and AND/OR logical combinations.

Supports highly flexible customization and is suitable for advanced scenarios that require custom scripting.

Scenarios

General-purpose scenarios

Advanced custom scenarios

Fully custom scenarios

Ease of use (technical skills required)

High

Medium

Low

Configuration flexibility

Low

Medium

High

Complex logic and EdgeScript

The rule engine supports basic AND/OR logical combinations, making it suitable for most conditional filtering scenarios. However, the following scenarios can help you decide whether to use EdgeScript:

  • Complex regular expression matching: The regular expression operator in the rule engine is disabled by default. You must submit a ticket to request that it be enabled. If you need to use regular expressions frequently, EdgeScript natively supports regular expression matching, which is more convenient.

  • Precise multi-dimensional combinations: Configuring complex evaluations that combine multiple criteria, such as matching both the client IP and User-Agent with regular expressions (for example, blocking requests from a specific IP range where the User-Agent contains a specific keyword), is cumbersome in the rule engine. EdgeScript lets you flexibly combine multiple conditions using script logic.

  • Granular multi-directory IP access control: EdgeScript lets you grant different IP address ranges access to different directories. For example, you can allow one IP address range to access all directories, allow a specific IP address to access only one directory, and deny all other IP addresses. Use match_re to match IP addresses and URI paths. Use return true to allow the request, and use exit(403) to deny it.

Limitations

  • You can create a maximum of 50 rule conditions per domain name.

  • Each rule condition can contain a maximum of 20 sub-rules.

  • You cannot use regular expression match or regular expression mismatch operators when configuring rule conditions in the console or using OpenAPI. However, you can view existing configurations that use these operators. To use these operators, use ESA.

  • A rule condition can be referenced a maximum of five times across all features within a single domain name.

  • You can nest rule conditions up to three levels deep, and each level can have its own independent logical relationships.

  • If a feature, such as setting the cache expiration time or modifying outgoing request headers, references a rule condition, the condition's priority determines the execution order, not the feature's configuration order.

  • Among the preceding limits, the maximums of 50 rule conditions, 20 sub-rules, and 3 levels of nesting are hard limits that cannot be increased upon request. The maximum total number of references to rule conditions across all features within a single accelerated domain name, which is 5 by default, can be increased upon request.

    • To increase the total number of times that rule conditions can be referenced for a single domain name (five by default), submit a ticket. Provide the following information in the ticket: the accelerated domain name for which you want to increase the limit, the target number of references, and a description of your business scenario.

    • When adding another accelerated domain name, submit a separate ticket to increase the number of rule-condition references for that domain. The process is the same as for the first request.

  • Each condition can have a maximum of 32 match values for match types such as client IP, URI, file extension, filename, and User-Agent. If you have a large client IP allowlist, consolidate multiple IP addresses into CIDR blocks (for example, 120.209.XXX.X/24) to configure the IP addresses and conserve match-value capacity.

  • You can configure a maximum of 32 User-Agent match values. Values beyond this limit are ignored. To match a large number of User-Agents, use a wildcard (*) to consolidate similar User-Agent values. For example, *Chrome* matches all Chrome browser versions), or use EdgeScript for more flexible matching logic.

Feature priority and execution logic

When you configure the rule engine, consider the following feature priorities and execution logic:

  • Referer hotlink protection takes precedence over the rule engine: When a request includes a Referer header, the system first evaluates it against the Referer blacklist and whitelist. If the request matches the whitelist, it is allowed to proceed, bypassing the rule engine. If the request does not match the whitelist, the system blocks it immediately. If the Referer header is empty, the request bypasses this logic and is evaluated by the rule engine.

  • Execution order for features using rule conditions: When features such as Cache Expiration or Modify Outbound Request Header use rule conditions, the system executes them based on the priority of their associated rule conditions, not the configuration order of the features. For example, if Cache Expiration and Back-to-Origin Parameter Rewrite use rule conditions with different priorities, the system executes them in order of rule condition priority, from highest to lowest.

  • Troubleshoot conflicts between URL authentication and caching: If you encounter unexpected behavior after configuring URL authentication, such as requests being accessible without authentication parameters or valid links being incorrectly blocked, check the following in order:

    1. Check if a WAF whitelist is configured. A WAF whitelist might bypass the CDN authentication logic and allow requests through directly.

    2. Verify that the Ignore URL Parameters feature is not stripping authentication parameters, such as auth_key) are filtered out by the ignore URL parameters setting, which skips authentication validation for cache hits.

    3. Confirm that the Cache Key Rule does not ignore the authentication parameter. If it does, requests with different authentication parameters might receive the same cached content, effectively bypassing authentication.

Rule condition syntax

A rule condition combines one or more conditional expressions by using logical operators. The following sections describe the syntax.

Logical operators

Logical operators evaluate conditions at the same level, including nested condition sets. The supported operators are and and or.

  • and (AND): A logical AND operator. A match succeeds only if all conditions are true.

  • or (OR): A logical OR operator. A match succeeds if any condition is true. For example, configuring multiple rule conditions for the same response header lets you add the same response header under different conditions. Example: When the URI contains /path-a or the URI contains /path-b , add the response header.

Parameters of a conditional expression

A conditional expression, the most granular unit of a rule, includes the following parameters:

Parameter

Corresponding configuration parameter in the condition function for domain name configuration

Description

Required

Conditional match

match

Specifies the conditional match expression.

Yes

Logical operator

logic

Specifies the logical operator for the conditional match expression. Valid values are and and or.

Yes

Criteria

criteria

Specifies the array of conditional expressions to be evaluated.

Yes

Match type

MatchType

Specifies the type of information in a client request to match.

Yes

Match object

MatchObject

Further refines the match type. For example, a client IP address can be specified as a POP connection IP or an XFF IP.

No

Match operator

MatchOperator

Specifies the comparison to perform.

Yes

Match value

MatchValue

The value to compare against data from the client request.

Yes

Negate condition

negate

Specifies whether to negate the result of the conditional expression. Valid values are true and false.

Yes

Case sensitivity

caseSensitive

Specifies whether the match value is case-sensitive.

No

Rule condition name

name

Specifies the name of the rule condition.

Yes

Status

status

Specifies the status of the rule condition.

Yes

Conditional expression configuration

Match type

Corresponding configuration parameter in the condition function for domain name configuration

Description

Match object

Match operator

Match value

Case sensitivity

Corresponding nginx/tengine variable

Protocol

scheme

The protocol used by the client request, such as HTTP or HTTPS.

Not applicable

  • equal to

  • not equal to

  • http

  • https

Not applicable

$scheme

Request method

method

The request method used by the client request, such as GET or PUT.

Not applicable

  • equal to

  • not equal to

  • get

  • put

  • post

  • delete

  • head

Not applicable

$request_method

URI (path)

uri

The path in the client request URL, excluding any request parameters. For example: /favicon.ico.

Not applicable

  • contains any

  • does not contain any

The wildcards ? and * are supported. For example, enter /*/my_path/*. Multiple values are supported.

  • Case-sensitive

  • Case-insensitive

$raw_uri or $uri

File name

basename

The name of the file requested by the client. For example: name1.

Not applicable

  • contains any

  • does not contain any

The wildcards ? and * are supported. You can enter multiple values.

  • Case-sensitive

  • Case-insensitive

-

File extension

extension

The file extension of the file requested by the client. The system identifies the extension as the substring from the last dot (.) to the end of the filename. For example: .mp4.

Not applicable

  • contains any

  • does not contain any

The wildcards ? and * are supported. You can enter multiple values.

  • Case-sensitive

  • Case-insensitive

-

Hostname

hostname

The hostname in the client request. Matching order: host in the request URL > host in the Host request header.

Not applicable

  • contains any

  • does not contain any

The host from the client request. You can enter multiple values.

  • Case-sensitive

  • Case-insensitive

$host or $http_host

Client IP address

clientip

The IP address of the client. IPv4 (for example, 1.1.X.X), IPv6 (for example, 240e:95c:3004:2:3:0:0:XXX), and CIDR blocks (for example, 20.209.XXX.XXX/31) are supported.

  • POP connection IP

  • XFF IP

Note

For more information about POP connection IP and XFF IP, see .

  • contains any

  • does not contain any

IPv6 addresses, such as 240e:XXX:3004:2:3:0:0:3f7, and CIDR blocks, such as 120.209.XXX.XXX/31, are supported. You can enter multiple values.

Not applicable

$remote_addr

Client IP version

clientipVer

The IP version of the client address: IPv4 or IPv6.

  • POP connection IP

  • XFF IP

Note

For more information about POP connection IP and XFF IP, see .

  • equal to

  • not equal to

  • v4

  • v6

Not applicable

-

Internet service provider (ISP)

geolocation

The ISP to which the client IP address belongs.

  • POP connection IP

  • XFF IP

Note

For more information about POP connection IP and XFF IP, see .

  • contains any

  • does not contain any

You can select an ISP from the drop-down list or enter characters to filter the options. Fuzzy search by ID or name is supported. You can enter multiple values.

Not applicable

$ip_isp_id

IP geolocation

geolocation

The geographical location of the client IP address.

  • POP connection IP

  • XFF IP

Note

For more information about POP connection IP and XFF IP, see .

  • contains any

  • does not contain any

You can select a location from the drop-down list or enter characters to filter the options. Fuzzy search by ID or name is supported. You can enter multiple values.

Not applicable

$ip_country_id

Request parameter

querystring

A parameter in the request URL.

Enter the parameter name.

  • exists

  • not exists

  • contains any

  • does not contain any

  • greater than

  • greater than or equal to

  • less than

  • less than or equal to

The wildcards ? and * are supported. You can enter multiple values.

  • Case-sensitive

  • Case-insensitive

$arg_{name}

Request header

header

A header in the client request.

You can enter a parameter name or select a parameter from the drop-down list.

  • exists

  • not exists

  • contains any

  • does not contain any

  • greater than

  • greater than or equal to

  • less than

  • less than or equal to

You can enter multiple values.

  • Case-sensitive

  • Case-insensitive

$http_{name}

Cookie

cookie

The cookie in the client request.

Enter the cookie name.

  • exists

  • not exists

  • contains any

  • does not contain any

  • greater than

  • greater than or equal to

  • less than

  • less than or equal to

The wildcards ? and * are supported. You can enter multiple values.

  • Case-sensitive

  • Case-insensitive

$cookie_{name}

User-Agent

useragent

The User-Agent in the request header.

Not applicable

  • contains any

  • does not contain any

You can select a value from the drop-down list or enter a User-Agent value, such as *Chrome/25*. The wildcards ? and * are supported. You can enter multiple values. A maximum of 32 User-Agent values can be configured. If this limit is exceeded, the configuration does not take effect.

  • Case-sensitive

  • Case-insensitive

$http_user_agent

Range bucket

range

Matches a specified percentage of client requests.

Not applicable

  • equal to

  • not equal to

Enter a percentage value.

Not applicable

-

Time

time

The time when the client request occurs. The time is in UTC+8. For example, 09:10~14:22.

Not applicable

  • contains any

  • does not contain any

Enter a time range, such as 09:10~14:22, which represents the period from 09:10 to 14:22.

Not applicable

-

Nginx var

ngxvar

Use Nginx variables if the preceding variables do not meet your requirements. For a list of supported variables, see the official Nginx documentation.

You can select a variable from the drop-down list or enter a variable name. Concatenation is supported, such as $region:$isp.

  • exists

  • not exists

  • contains any

  • does not contain any

  • greater than

  • greater than or equal to

  • less than

  • less than or equal to

You can enter multiple values.

Not applicable

${name}

Common configuration notes for conditional expressions

  • URI (path) match starting point: For URI matching, the value is the path portion that starts with the first / in the path after the domain name and excludes the domain name and request parameters. For example, for the request https://example.com/path/file.html?key=value, the URI value that is actually matched is /path/file.html. The match value must start with / .

  • File extension match format: When you configure a file extension match, the match value must include a dot (.). For example, to match .txt files, enter .txt rather than txt; otherwise, the match may fail.

  • Wildcard usage examples: URI and file extension matches support the wildcards ? (matches any single character) and * (matches any number of characters). Common examples:

    • /*.pdf: matches all PDF files in the root directory.

    • /api/*/data: matches /api/ paths containing data in any subpath.

    • .??: matches all two-character file extensions, such as .js and .ts.

IP address verification mode

The rules engine provides two IP address verification modes. The selected mode affects how CDN POPs identify the client IP address:

  • POP connection IP: This mode matches the IP address that a client uses to connect to a CDN POP. If the traffic between the client and the CDN POP passes through a proxy server, the POP connection IP is the IP address of the proxy server.

  • XFF IP: This mode matches the leftmost IP address in the x-forwarded-for request header. Whether or not a proxy server is used between the client and the CDN POP, the XFF IP is always the client's real IP address.

The choice of verification mode depends on whether a client request passes through a proxy server before it reaches a CDN POP.

Note that the location on a CDN POP where a feature that references a rule condition takes effect also affects which IP address verification mode you must select. For origin-fetch features that take effect on L2 POPs, the L1 POP that the user request passes through is equivalent to an intermediate proxy server.

Example: Assume the client's real IP address is 10.10.10.10, and the proxy IP address is 192.168.0.1.

  • Without a proxy server:

    • The x-forwarded-for request header value is 10.10.10.10.

    • The client's real IP address (the leftmost IP in the x-forwarded-for request header) = the IP address used to establish the connection between the client and the CDN POP = 10.10.10.10.

  • With a proxy server:

    • The x-forwarded-for request header value is 10.10.10.10,192.168.0.1.

    • The client's real IP address (the leftmost IP in the x-forwarded-for request header) = 10.10.10.10.

    • The IP address used to establish the connection between the client and the CDN POP = the IP address of the proxy server = 192.168.0.1.

    • The client's real IP address (the leftmost IP in the x-forwarded-for request header) ≠ the IP address used to establish the connection between the client and the CDN POP.

In specific regions, a small number of Internet service providers (ISPs) may assign private IP addresses to end users. As a result, the CDN node receives the user's private IP address.

Note

Private IP addresses fall within the following three ranges:

  • Class A private IP addresses: 10.0.0.0–10.255.255.255. Subnet mask: 10.0.0.0/8.

  • Class B private IP addresses: 172.16.0.0–172.31.255.255. Subnet mask: 172.16.0.0/12.

  • Class C private IP addresses: 192.168.0.0–192.168.255.255. Subnet mask: 192.168.0.0/16.

Match operators (matchOperator)

Operator

Corresponding configuration parameter in the condition function for domain name configuration

Description

equal to

The matchOperator is set to equals.

The condition is met only when the variable exactly equals or does not equal the specified match value.

not equal to

The matchOperator is set to equals and the negate parameter is set to true.

exists

The matchOperator is set to exists.

The condition is met depending on whether the specified variable exists in the request.

not exists

The matchOperator is set to exists and the negate parameter is set to true.

contains any

The matchOperator is set to contains.

The condition is met if the variable contains (or does not contain) any of the specified match values. A maximum of 32 match values are supported.

Two types of containment matching are supported:

  • Exact match: Enter the match value directly. For example, to match a, the variable must equal a.

  • Wildcard match: You can use * as a wildcard. Supported patterns include a*, *a, and *a*. These match abc, bca, and bcabc.

does not contain any

The matchOperator is set to contains and the negate parameter is set to true.

greater than

The matchOperator is set to gt.

That is, >

less than

The matchOperator is set to lt.

That is, <

greater than or equal to

The matchOperator is set to ge.

That is, >=

less than or equal to

The matchOperator is set to le.

That is, <=

regular expression match

The matchOperator is set to regex.

Matches the variable against a regular expression.

Note

If you configure rules in the console or by using OpenAPI, you cannot use these regular expression operators. However, you can view existing configurations. To use regular expression-related match operators,

regular expression mismatch

The matchOperator is set to regex and the negate parameter is set to true.

Wildcards

Wildcard

Description

Path matching example

?

Matches any single character.

/img/?.png matches resources whose file name is a single character, such as /img/a.png and /img/b.png.

*

Matches zero or more characters.

/api/* matches all paths under /api/, such as /api/v1/users and /api/v2/products. /static/*.css matches all CSS files in the /static/ directory.

Features that support rule conditions

Category

Feature

Basic configuration

Conditional origin

Cache settings

Cache expiration

Modify outbound response headers

URL rewrite

Custom cache key

Origin settings

Modify outgoing request headers

Modify incoming response headers

Parameter rewrite

Specify an origin-pull HOST for an origin server

Access control

Configure a Referer blacklist or whitelist

Configure URL signing

Configure an IP blacklist or whitelist

Configure a User-Agent blacklist or whitelist

Performance optimization

Ignore parameters

Video settings

Configure range origin fetch

Traffic throttling

Single-request throttling

Configuration management

  • Where to view execution actions: The Rules Engine page defines only rule conditions. It does not configure execution actions directly. You must view and manage specific execution actions, such as cache expiration, URL rewriting, and throttling, on the page of the feature that references the rule. For example, if a cache expiration configuration references a rule condition, you must view and modify it in Cache Settings > Cache Expiration.

  • Remote Authentication does not support rule conditions: The remote authentication feature cannot reference rule conditions. You cannot use the rules engine to control the scope in which remote authentication takes effect.

  • Authentication timeout limit: The Remote Authentication feature's Timeout can be set to a maximum of 3000 milliseconds (3 seconds). The default value is 500 milliseconds. 3000 milliseconds is the maximum value that the system supports.

Advanced use cases

You can implement the following advanced configurations with the Rule Engine:

  • Differentiated rate limiting: To rate-limit some requests but not others (for example, rate-limit URLs with authentication parameters but not other URLs), or to configure a fallback rate limit, create rule conditions that distinguish request characteristics in the Rules Engine module. Then, in Rate Limiting for a Single Request, reference the different rule conditions and set a different rate limit for each.

  • IP-based canary origin routing: To implement a canary release based on client IP addresses or route requests from specific IPs to different origins, create an IP matching condition in the rules engine. Then, reference that rule condition under "Conditional origin" and specify different origin addresses.

  • Mixed-origin architecture recommendation: If you need to use both WAF and OSS as origins, do not directly configure multiple primary origins. Instead, use the rules engine to create matching conditions based on URL paths, then dynamically specify the origin address for each path under "Conditional origin" to avoid origin conflicts between multiple origins.

Procedure

  1. Log on to the CDN console.

  2. In the left navigation pane, click Domain Names.

  3. On the Domain Names page, find the target domain name and click Manage in the Actions column.

  4. In the left navigation bar for the specified domain name, click Rules Engine.

  5. Click Add Rule.

  6. On the Add Rule page, set Rule Name and Rule Content.

  7. Click Submit to complete the configuration.

Typical configuration scenarios

The following sections describe how to implement three common configuration scenarios.

Scenario 1: IP allowlist access control and non-allowlist redirects

Requirement: Allow only specified IP addresses to access the website and automatically redirect all other IP addresses to a maintenance page.

Configuration approach:

  1. In the Rules Engine module, create a rule condition. Set the match type to Client IP address, set the match operator to does not contain any, and enter the allowlisted IP addresses as the match values.

  2. Under "Cache configuration" > "Access URL rewrite", reference this rule condition and redirect matching requests (that is, requests from non-allowlist IP addresses) to the specified maintenance-page URL, such as an HTML page hosted in OSS.

Notes:

  • If requests from non-allowlist IP addresses return 403 instead of being redirected, check whether multiple OSS origins cause an origin conflict. We recommend that each CDN domain name map to only one primary OSS origin.

  • Ensure that the maintenance-page URL is publicly accessible and is not blocked by other access-control rules.

  • Default behavior: When an IP allowlist rule is associated with a URI path matching condition, the IP allowlist check applies only to requests matching that URI path; requests that do not match it are allowed by default. To apply IP access control to all paths, use EdgeScript to write custom logic.

Scenario 2: Referer protection and file-extension control

Requirement: Restrict Referer access for files with specified extensions, while allowing requests with an empty Referer for files with those extensions.

Configuration approach:

  1. In the Rules Engine module, create a rule condition. Set the match type to File extension, set the match operator to does not contain any, and enter the extensions that you want to exempt, such as .pdf, as the match values.

  2. Under "Access control" > "Referer hotlink protection", reference this rule condition. Requests for extensions other than the specified ones are restricted by the Referer allowlist or blocklist, while requests with an empty Referer for the specified extensions are exempted.

Scenario 3: Block access to non-image and non-video files

Requirement: An OSS bucket is made available through CDN acceleration. Access to non-media files such as .html through crafted malicious URLs must be blocked.

Configuration approach:

  1. In the Rules Engine module, create a rule condition. Set the match type to File extension, set the match operator to contains any, and enter .html,.htm,.php,.asp,.jsp as the match values.

  2. Add a rule condition. Set Match Type to "Client IP", Match Operator to "Does not contain any", and Match Value to the allowlist IP addresses.

  3. Under "Access control", reference this rule condition and return 403 for matching requests: requests from non-allowlist IP addresses for files that are not specified image or video formats.

Note (upload restrictions):

The CDN rules engine limits access only; it cannot control uploads. To limit file types during upload, use the OSS PostObject API and configure a Policy to restrict file types. For example, setting starts-with $Content-Type image/ rejects uploads of non-image files such as .html. The OSS PutObject API does not check file types and cannot block non-media files during upload.

FAQ

Do rules configured for the Host header when fetching from a specified origin count toward the advanced conditional-rule limit?

Yes. Any rule applied to a domain name counts toward the advanced conditional rule limit. You can create up to 50 rule conditions per domain name, and each condition can contain up to 20 sub-conditions.

Where are execution actions after configuring the rules engine?

The rules engine itself defines conditions and takes effect only when combined with other features, such as cache expiration, origin request parameter rewrite, and URL rewrite. Execution actions are reflected in the configuration of the specific associated feature, where you can view or manage referenced rules. For example, first create a rule condition to define the matching logic, then reference that rule in cache expiration so that the cache policy applies only to matching requests.

With URL authentication enabled, how can I use the rules engine to match request parameters and control video downloads?

When URL authentication (A authentication) is enabled, you can use the rules engine to match request parameters and control behavior. We recommend configuring a "URL contains" condition in the rules engine to match a specified business parameter name or value, such as download=1.

Note

Do not use authentication parameters as match criteria to avoid affecting normal authentication validation. However, other business parameters, such as download, can be matched normally.

Ensure that the generated URL includes a parameter consistent with the rule, such as http://.../video.mp4?auth_key=...&download=1, to trigger the corresponding response header configuration and control downloads.

How do I configure the rules engine to avoid caching after matching a specified path?

For different matching requirements, use either of the following configuration methods:

  • For file extensions: In cache configuration, set cache expiration to 0 for extensions that should not be cached. Set a suitable cache duration, such as 4 hours, for extensions that should be cached, and select "Do not ignore origin", "Do not cache headers", and "Do not follow origin cache-control".

  • For path matches: Because the CDN console does not support regular-expression matching of complete URLs, configure URI path matching through the rules engine. Set Match Type to "URI (path)", Match Operator to "Contains any" or "Does not contain any", and use the following wildcards in Match Value: ? and *. For example, enter /*/no-cache/* to match /api/no-cache/data and similar paths.

Does path matching start from the first / after the domain name?

Yes. For a non-regular-expression path match in the rules engine, the matched content is the path portion starting from the first / after the domain name. For https://example.com/a.html, the actual matched path is /a.html.

CDN does not natively support regular-expression matches for complete URLs that include a protocol and domain name, such as ^https?://([^/]+)/path.

Why does a response header not take effect the first time I configure filename matching?

When configuring filename matching in the rules engine, note that "Filename (basename)" and "File extension (extension)" are two distinct match types:

  • Filename (basename): Matches the filename portion excluding the file extension. For example, script (excluding .js).

  • File extension (extension): Matches the filename suffix. For example, .js and .css.

If the filename match includes the file extension, such as script.js, it cannot be matched correctly. Remove the file extension and enter only the filename portion, such as script, for the configuration to take effect.