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_reto match IP addresses and URI paths. Usereturn trueto allow the request, and useexit(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:
Check if a WAF whitelist is configured. A WAF whitelist might bypass the CDN authentication logic and allow requests through directly.
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.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-aor 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 | 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 |
|
| Not applicable | $scheme |
Request method | method | The request method used by the client request, such as GET or PUT. | Not applicable |
|
| Not applicable | $request_method |
URI (path) | uri | The path in the client request URL, excluding any request parameters. For example: | Not applicable |
| The wildcards |
| $raw_uri or $uri |
File name | basename | The name of the file requested by the client. For example: name1. | Not applicable |
| The wildcards |
| - |
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: | Not applicable |
| The wildcards |
| - |
Hostname | hostname | The hostname in the client request. Matching order: host in the request URL > host in the Host request header. | Not applicable |
| The host from the client request. You can enter multiple values. |
| $host or $http_host |
Client IP address | clientip | The IP address of the client. IPv4 (for example, |
Note For more information about POP connection IP and XFF IP, see . |
| 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. |
Note For more information about POP connection IP and XFF IP, see . |
|
| Not applicable | - |
Internet service provider (ISP) | geolocation | The ISP to which the client IP address belongs. |
Note For more information about POP connection IP and XFF IP, see . |
| 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. |
Note For more information about POP connection IP and XFF IP, see . |
| 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. |
| The wildcards |
| $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. |
| You can enter multiple values. |
| $http_{name} |
Cookie | cookie | The cookie in the client request. | Enter the cookie name. |
| The wildcards |
| $cookie_{name} |
User-Agent | useragent | The User-Agent in the request header. | Not applicable |
| You can select a value from the drop-down list or enter a User-Agent value, such as |
| $http_user_agent |
Range bucket | range | Matches a specified percentage of client requests. | Not applicable |
| 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 |
| 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 |
| 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 requesthttps://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.txtfiles, enter.txtrather thantxt; 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 containingdatain any subpath..??: matches all two-character file extensions, such as.jsand.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.
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:
|
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. |
|
| Matches zero or more characters. |
|
Features that support rule conditions
Category | Feature |
Basic configuration | |
Cache settings | |
Origin settings | |
Access control | |
Performance optimization | |
Video settings | |
Traffic 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
-
Log on to the CDN console.
-
In the left navigation pane, click Domain Names.
-
On the Domain Names page, find the target domain name and click Manage in the Actions column.
In the left navigation bar for the specified domain name, click Rules Engine.
Click Add Rule.
On the Add Rule page, set Rule Name and Rule Content.
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:
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.
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:
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.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:
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,.jspas the match values.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.
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.