Setting up Yandex Cloud CDN location rules
Warning
The location rule functionality is only available through the Yandex Cloud CDN provider.
Location rules are a mechanism used to selectively redefine CDN resource settings for requests whose paths match a specific template.
Let’s assume that content caching is enabled in the CDN resource settings, while cache lifetime is set to four days. This is enough for most types of content, but for some specific files you have to reduce cache lifetime to a few seconds. Location rules will help you with such cases.
Learn more about location rules
To set up a location rule:
Creating a location rule
-
In the management console
, select the folder where your resource is located. -
Go to Cloud CDN.
-
Click the CDN resource name.
-
Select the
Location rules tab. -
In the top-right corner, click Add rule.
-
Under Overview, configure the main location rule settings:
-
Name.
The rule name may contain lowercase Latin letters, numbers, underscores, hyphens, and spaces, and may be up to 50 characters long.
-
Rule weight: Rule triggering priority. The lower a rule’s weight, the higher its priority in the user resource configuration. If multiple rules have the same weight, they will be applied in a random order.
For example, let’s assume you set the following location rules:
Rule Weight Regular expression Cache lifetime A 1^/path/.+1 hourB 2\.txt$4 hoursWith this configuration, the
/path/file.txtrequest will match rule A, which has a lower weight and a higher priority than rule B, so the file will be cached for one hour.To cache the
/path/file.txtfile for four hours, rule B must have a lower weight than rule A.
-
-
Under Condition, set Regular expression, i.e., the URI path pattern against which requests are checked. If there is a match, the settings specific to that location rule will apply.
Tip
To explicitly indicate the start or end of the string to match in a regular expression, use the
^and$anchor characters. -
Specify the CDN resource settings you want to redefine using a location rule:
- CDN caching: Temporary storage of file copies from origins. For more information, see Configuring resource caching.
- Redirect requests: Redirecting content requests to a different path. This is useful when rearranging files and folders on an origin. You can also use redirection if you want to keep search engine rankings of your content when moving your resource to a new address. For more information, see Configuring request redirection.
- Access via secure token: Restricting direct access to content via pre-signed URLs with the limited validity period. For more information, see Setting up access via a secure token.
- IP-based access: Restricting access to content using an allowing or blocking policy. For more information, see Updating the basic settings of a resource.
Note
The settings of HTTP headers and methods, CORS, content compression and segmentation, as well as advanced content caching options, become available after you create a location rule. For more details, see Setting additional parameters.
-
Click Create.
Use the create REST API method for the ResourceRules resource or the ResourceRulesService/Create gRPC API call.
Configuring advanced settings
After creating a rule, you can configure its advanced settings:
-
In the management console
, select the folder where your resource is located. -
Go to Cloud CDN.
-
Click the CDN resource name.
-
Select the
Location rules tab. -
Click the location rule name.
-
Configure advanced rule settings:
- To set up advanced content caching options, go to the
Caching tab and click Edit. For more information, see Configuring resource caching. - To configure HTTP request and response headers, CORS for responses to clients, and allowed HTTP methods for client requests, go to the
HTTP headers and methods tab and click Edit. For more information, see: - To configure file compression and segmentation, go to the
Content tab and click Edit. For more information, see:
Edit the relevant settings and click Save.
- To set up advanced content caching options, go to the
Use the update REST API method for the ResourceRules resource or the ResourceRulesService/Update gRPC API call.