The conditional origin feature uses the rules engine to filter user requests based on specified criteria, such as request headers, query string parameters, paths, or client IP addresses. CDN routes requests that match the criteria to a specific origin address. You can add multiple configurations to direct requests to different origins based on different conditions.
Prerequisites
Before you configure a conditional origin, you must create a Rule Condition in the rules engine.
Usage notes
Feature conflict: The conditional origin and advanced origin features are mutually exclusive. You can enable only one.
Wildcard support: In the rules engine, the URI match value supports the wildcard
*(matches any number of characters) and?(matches any single character). Path matching in the advanced origin feature supports only exact values, not wildcards.Rule priority and matching order: Conditional origins are evaluated based on the priority of the associated Rule Condition in the rules engine, not the order of the conditional origin configurations. When a request matches a rule, evaluation stops. Rule priority in the rules engine is determined by creation order: rules created earlier have higher priority. To adjust the matching priority, you must delete and recreate the rules in the desired order.
Multi-path configuration tip: When creating rules to "include" or "exclude" a path, you can enter multiple paths (in multiple lines or as multiple values) in the match value field of a single rule to simplify your configuration.
Rule limit: The number of Rule Conditions that a conditional origin can reference is subject to a global limit in the rules engine. For a single domain, the total number of references to Rule Conditions across all features, including conditional origin, advanced origin, and Referer-based hotlink protection, cannot exceed five. In complex multi-origin scenarios, exceeding this limit causes the configuration to fail. We recommend that you plan your rule usage carefully or use separate domains for acceleration if you have many origins.
Considerations for OSS origins
If an origin is an OSS bucket, you must add its address under Domain Names > Basics > Origin Information and set the origin type to OSS Domain. This enables CDN to authenticate correctly with OSS.
Multi-OSS origin scenarios: When you configure multiple OSS origins, you must configure a specific origin Host for each one. The Host value must exactly match the corresponding OSS bucket domain, such as
dev-3mir.oss-cn-guangzhou.aliyuncs.com.If the origin Host is not configured correctly or if you rely on the default origin Host, requests may be routed to the wrong bucket. This can cause 403 SignatureDoesNotMatch or 404 Not Found errors. You must explicitly bind every conditional origin rule, including match rules and fallback rules, to its corresponding origin and origin Host. When you configure multiple origins, we recommend setting equal weights for the basic origins and creating separate Rule Conditions for each conditional origin to ensure predictable routing.
Origin type comparison
Both conditional origins and advanced origins can reference Rule Conditions from the rules engine to create flexible origin routing policies. CDN automatically selects an origin if a request matches a Rule Condition:
Origin type | Trigger condition |
conditional origin | The request matches the Rule Condition referenced by the conditional origin. |
advanced origin | The request matches the Rule Condition referenced by the advanced origin. |
basic origin | The request does not match any conditional origin or advanced origin rule (default fallback). |
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 Origin Information section, click Expand next to Conditional Origin.
Click Add Conditional Origin.
Set the Rule Condition.
If this is your first time adding a configuration or you need to modify an existing Rule Condition, click Conditional Origin in the Rules Engine dialog box. This opens the rules engine where you can add or modify configurations. For more information, see rules engine.
If you have an existing Rule Condition, select it from the Rule Condition drop-down list.
In the Origin Address field, enter the origin address. The origin address can be an IP address, a domain name, an OSS bucket, or a Function Compute (FC) endpoint.
Click OK to complete the configuration.
Example: Routing /api/* requests to FC
The advanced origin feature supports only exact value matching and does not support wildcards or regular expressions. To implement path-based origin routing, use conditional origins with the rules engine. By combining the conditional origin feature with the rules engine and URL rewrite, you can route requests for the /api/* path to a Function Compute (FC) origin, while all other requests are routed by default to an OSS origin.
Create a rule in the rules engine
Navigate to Domain Names, find the target domain, and go to the Rules Engine page.
Click Add Rule and configure the rule condition as described in the following table:
Parameter
Value
Rule Name
Enter a custom name for easy identification, such as
api-to-fc.Match type
URI
Match operator
Include Any
Match value
Click Edit, enter
*/api/*, and press Enter to add it. Wildcards*and?are supported. A single rule supports up to 32 match values.Case sensitive
Case-insensitive (default)
ImportantDo not add a hostname condition. The hostname condition matches the request domain, not the origin. Adding a hostname condition may prevent the rule from matching the path after a URL rewrite.
Click Submit to save the rule.
Configure the conditional origin
In the navigation pane on the left, go to Domain Names > Basics. In the Origin Information section, click the expand icon next to Conditional Origin.
Click Add Conditional Origin and configure the parameters in the dialog box as described in the following table:
Parameter
Value
Rule Condition
From the drop-down list, select the rule that you created in Step 1, such as
api-to-fc.Origin Address
Enter the address of your Function Compute (FC) origin.
Click OK to save the configuration.
Configure the origin Host
Configure a specific origin Host for the FC origin to ensure that CDN sends origin fetch requests with the correct Host header.
In the left-side navigation pane of the Domain Names page, go to Origin Fetch > Configurations tab.
In the Specify Origin Host section, click Add.
In the dialog box, configure the following parameters and click OK.
Parameter
Description
Origin Type
Select Custom Origin. The origin addresses configured in conditional origins do not appear in the Basic origin address drop-down list. You must select Custom origin and manually enter the address.
Origin Address
Enter the FC origin address from Step 2.
Origin Host Type
Select the type of Host value to use. For FC origins, select Custom Origin Host and enter the domain name manually.
Origin Host
Enter the FC domain name (for example,
your-fc-function.cn-hangzhou.fcapp.run).For OSS origins, the value must exactly match the OSS bucket domain (for example,
dev-3mir.oss-cn-guangzhou.aliyuncs.com).Rule Condition
Optional. Select the Rule Condition created in step 1 (for example,
api-to-fc) to scope this origin Host to requests that match the rule.
You do not need to disable the default origin Host. When the default origin Host and the origin Host for a specific origin are configured for the same origin, the origin Host for the specific origin takes effect. For origins that do not have a specific origin Host configured, the default origin Host remains in effect. For more information, see origin Host.
Configure a URL rewrite (optional)
If you need to rewrite the /api/ path to the /print/api/ path on your FC origin, you must also use the URL rewrite feature in CDN:
In the rules engine, configure a URL rewrite rule to rewrite
/api/to/print/api/.Ensure that the URI matching condition in the rules engine from Step 1 covers the rewritten path. For example, use
*/api/*or match the specific path/print/api/*.
After you complete these configurations, CDN routes frontend requests to /api/* to the FC service, while routing other requests to the OSS origin by default.
Example: Referer-based routing
The following example shows how to route requests to different origins based on the Referer header. This is useful for hotlink protection or for controlling access based on the source of the request.
Create a referer rule condition
Log on to the CDN console, select the target domain, and navigate to Domain Names > Rules Engine.
Click Add Rule and configure the parameters as described in the following table. The rules engine does not have a dedicated Referer match type. You must use the Request Header match type to implement Referer-based matching:
Parameter
Value
Rule Name
Enter a descriptive name, such as
referer-routing.Match type
Request Header
Parameter name
RefererMatch operator
Include Any
Match value
example.com. This value specifies the referer domain. You can add multiple domains.Case sensitive
Case-insensitive
Click Submit to save the rule.
Configure the conditional origin
In the navigation pane on the left of the domain management page, go to the Basics tab. In the Origin Information section, click the expand icon next to Conditional Origin.
Click Add Conditional Origin and configure the parameters in the dialog box as described in the following table:
Parameter
Value
Rule Condition
From the drop-down list, select
referer-routing.Origin Address
Enter the address of the origin for this Referer source.
Click OK to save the configuration.
Example: Routing users from the Chinese mainland and outside the Chinese mainland to different origins
The following example shows how to use conditional origins with the rules engine to route users from Chinese mainland and outside Chinese mainland to different origin servers.
Log on to the CDN console. On the Domain Names page, find the target domain and click Manage in the Actions column. In the left navigation pane, click Rules Engine, and then click Add Rule.
Configure the Chinese mainland rule: In the rule content area, click the plus icon to add a match condition. Set Match type to IP Location, Match operator to Include Any and click Modify to select Chinese mainland from the drop-down list as the match value. Submit and save the rule.
Configure the overseas rule (fallback): Create another rule. Set Match type to IP Location, Match operator to Exclude Any, and set the match value to Chinese mainland. Submit and save the rule.
Configure the conditional origin: In the Origin Information section, associate each rule with its corresponding origin:
Associate the Chinese mainland rule with a domestic origin (for example, a domestic OSS bucket or a domestic server).
Associate the overseas rule with an overseas origin (for example, an overseas OSS bucket or an overseas server).
Configure the origin Host: Configure a dedicated Origin Host for each origin to ensure the Host value matches the corresponding origin domain.
In the left navigation pane of the domain management page, go to Origin Fetch > Configurations tab. In the Specify Origin Host section, click Add.
In the Specify Origin Host dialog box, configure the following parameters. Add one entry for each origin:
Parameter
Value
Origin Type
Custom origin. The origin addresses configured in conditional origins do not appear in the Basic origin address drop-down list. You must select Custom origin and manually enter the address. If the origin address is already added in the basic origin configuration (for example, as an OSS origin), you can also select Basic origin address and choose it from the drop-down list.
Origin Address
Enter the origin address associated with the conditional origin (for example, the domain of the domestic or overseas OSS bucket).
Origin Host Type
Custom origin Host
Origin Host
Enter the Host value that matches the origin domain (for example,
bucket.oss-cn-hangzhou.aliyuncs.com).Rule Condition
Select the rule condition that corresponds to this origin (for example, the Chinese mainland rule or the rule for outside the Chinese mainland).
Click OK to complete the configuration.
You do not need to disable the default origin Host. When the default origin Host and the origin Host for a specific origin are configured for the same origin, the origin Host for the specific origin takes effect. For origins that do not have a specific origin Host configured, the default origin Host remains in effect.
Recommendations
For scenarios where users outside the Chinese mainland experience high latency or unstable connections when accessing Chinese mainland origins, we recommend using OSS transfer acceleration to optimize the origin-fetch link. Configure as follows:
For the Chinese mainland conditional origin, set the origin to the OSS standard public endpoint (for example,
bucket.oss-cn-hangzhou.aliyuncs.com).For the overseas conditional origin, set the origin to the OSS Transfer Acceleration endpoint (for example,
bucket.oss-accelerate.aliyuncs.com).
OSS uses globally distributed acceleration nodes to optimize cross-border transfer links and can significantly reduce origin-fetch latency. For more information, see OSS Transfer Acceleration.