Configuring resource caching
In the CDN resource settings, you can enable content caching to temporarily store the copies of files loaded from origins on CDN servers.
If CDN server caching is enabled for a resource, files are copied from origins to the server cache in the following cases:
- File the user has requested from the CDN resource is not yet cached on the responding sever.
- Lifetime of the file’s copy cached on the CDN server has expired, and the file stored on the origin has changed (otherwise, the lifetime is prolonged for the same duration).
- You prefetched files from origins to the CDN server cache in the CDN resource settings.
Note
Caching settings affect the percentage of requests CDN servers can process from the cache (cache hits). Cloud CDN does not guarantee any particular percentage of such requests. If the CDN server cannot respond from the cache, it requests content from the origin. Consider possible traffic increases when assessing the origin's acceptable load and operating costs.
To configure the caching parameters of a resource:
-
In the management console
, select the folder where your resource is located. -
Navigate
to Cloud CDN. -
Click the resource name.
-
Navigate to the Caching tab.
-
In the top-right corner, click
Edit. -
Select and configure the caching options:
-
Under CDN:
- Enable CDN caching.
- Select the setting type:
Same as originorCustom settings. - Select the cache lifetime from the list.
- Optionally, for the
Custom settingssetting type, set the cache lifetime for the required HTTP response codes.
-
Under Browser:
- Enable Browser caching.
-
-
Optionally, under Additional settings:
-
Select the option to ignore Cookies.
-
In the Query parameter caching field, select a mode:
Do not cache: Ignore query parameters in the cache key.Cache all: Consider all query parameters.Cache all except: Consider all query parameters except those specified.Cache only: Consider only specified query parameters.
Specify query parameters for the
Cache all exceptandCache onlymodes. To add a field, click Add query parameter. You can specify a maximum of ten parameters. The name may be up to 30 characters long. Use Latin letters, digits, and the_character.
-
-
Click Save.
If you do not have the Yandex Cloud CLI yet, install and initialize it.
The folder used by default is the one specified when creating the CLI profile. To change the default folder, use the yc config set folder-id <folder_ID> command. You can also specify a different folder for any command using --folder-name or --folder-id.
If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.
-
View the description of the CLI command to update a resource:
yc cdn resource update --help -
Get a list of all resources in the default folder:
yc cdn resource list --format yamlResult:
id: s0me1dkfjq******** folder_id: s0mef01der7p******** cname: testexample.com created_at: "2022-01-19T09:23:57.921365Z" updated_at: "2022-01-19T10:55:30.305141Z" active: true options: edge_cache_settings: enabled: true default value: "345600" cache_http_headers: enabled: true value: - content-type - content-length - connection - server - date - test stale: enabled: true value: - error - updating allowed_http_methods: value: - GET - POST - HEAD - OPTIONS origin_group_id: "89783" origin_group_name: My origins group origin_protocol: HTTP ssl_certificate: type: DONT_USE status: READY -
To change the cache lifetime, run:
yc cdn resource update <resource_ID> \ --cache-expiration-time <time> \ --browser-cache-expiration-time <time> \ --ignore-cookie true \ --ignore-query-stringWhere:
--cache-expiration-time: Cache lifetime in seconds.--browser-cache-expiration-time: Browser cache lifetime in seconds.--ignore-cookie: Iftrue, ignores cookies.--ignore-query-string: Ignores query parameters.
Using the CLI, you can only choose to evaluate or ignore all query parameters. To evaluate specific parameters or all parameters except the specified ones, use the management console or API.
For more information about the
yc cdn resource updatecommand, see the CLI reference.
If you do not have Terraform yet, install it and configure the Yandex Cloud provider.
To manage infrastructure using Terraform under a service account or user accounts (a Yandex account, a federated account, or a local user), authenticate using the appropriate method.
-
In the configuration file, describe the properties of the
yandex_cdn_resourceCDN resource you want to create:resource "yandex_cdn_resource" "my_resource" { cname = "cdn1.yandex-example.ru" active = false origin_protocol = "https" secondary_hostnames = ["cdn-example-1.yandex.ru", "cdn-example-2.yandex.ru"] origin_group_id = yandex_cdn_origin_group.my_group.id options { edge_cache_settings = "345600" browser_cache_settings = "1800" ignore_cookie = true ignore_query_params = false } }Where:
cname: Primary domain name used for content distribution. This is a required setting.active: Flag indicating content availability to end users.True: CDN content will be available to clients. This is an optional setting. The default value istrue.origin_protocol: Protocol for origins. This is an optional setting. The default value ishttp.secondary_hostnames: Additional domain names. This is an optional setting.origin_group_id: Origin group ID. This is a required setting. Use the ID from the description of the origin group in theyandex_cdn_origin_groupresource.- The
optionssection contains additional parameters of CDN resources:browser_cache_settings: Browser cache lifetime in seconds. This is an optional setting. The default value is0.edge_cache_settings: Cache lifetime for response codes in seconds. This is an optional setting. The default value is345600.ignore_query_params: Ignore query parameters. This is an optional setting. The default value isfalse.ignore_cookie: Ignore cookies. This is an optional setting. The default value isfalse.
Using Terraform, you can only choose to evaluate or ignore all query parameters. To evaluate specific parameters or all parameters except the specified ones, use the management console or API.
For more on the properties of
yandex_cdn_resourcein Terraform, see this provider guide. -
In the command line, navigate to the directory with the Terraform configuration file.
-
Check the configuration using this command:
terraform validateIf the configuration is valid, you will get this message:
Success! The configuration is valid. -
Run this command:
terraform planYou will see a list of resources and their properties. No changes will be made at this step. Terraform will show any errors detected in the configuration.
-
Apply the configuration changes:
terraform apply -
Type
yesand press Enter to confirm the changes.You can check the CDN resource update in the management console
or using this CLI command:yc cdn resource list
Use the update REST API method for the Resource resource or the ResourceService/Update gRPC API call.
To configure caching of query parameters, provide only one of the fields in the options.queryParamsOptions object and set enabled: true for it:
ignoreQueryString: Ifvalue: true, ignore query parameters in the cache key; ifvalue: false, consider all query parameters.queryParamsWhitelist: Consider only query parameters listed invalue.queryParamsBlacklist: Consider all query parameters except those listed invalue.
In the gRPC API, these fields correspond to ignore_query_string, query_params_whitelist, and query_params_blacklist in the options.query_params_options object.
It may take up to 15 minutes for the new settings of the existing resource to apply to the CDN servers. After that, we recommend purging the resource cache.