All Products
Search
Document Center

CDN:Configure CDN cache expiration time

Last Updated:Sep 10, 2026

Cache expiration rules control how long resources are cached on CDN points of presence (POPs), which lets you balance content freshness, access performance, and origin fetch costs. This topic describes how cache rules work, how to configure and verify them, and how to troubleshoot caching issues, and it provides best practices for production environments.

How it works

When a request reaches a CDN point of presence (POP), the system decides whether to serve a cached copy or fetch the latest content from the origin server. The following steps are evaluated in priority order, and a lower number indicates a higher priority.

  1. Origin no-cache directives — CDN does not cache a resource if the origin server responds with Pragma: no-cache, Cache-Control: no-cache, Cache-Control: no-store, or Cache-Control: max-age=0. To force caching for these resources, select Ignore Origin No-Cache Header when you configure the cache expiration rule.

  2. Cache rules configured in the console — Cache rules configured in the CDN console take precedence, unless the origin server explicitly sends a no-cache directive as described in step 1. When multiple console cache rules match a request, the rule with the higher weight takes precedence, and rules with the same weight are resolved by creation order. Once a request matches a cache rule, no other rules are evaluated.

  3. Origin response headers — CDN honors the origin server's HTTP response headers if a request does not match any CDN console rule, or if the matched rule has Honor Origin TTL enabled. Header priority, from highest to lowest, is as follows: Cache-Control > Expires > Last-Modified > ETag.

  4. Default no-cache policy — If a request does not match any cache rule in the CDN console and the origin server does not return cache response headers such as Cache-Control, CDN applies a no-cache policy.

  5. CDN applies cache policies only to requests for which the origin server returns a status code of 200, 203, 206, 300, 301, 308, or 410. To configure the expiration time of status codes such as 404, use the Cache Settings > Status Code TTL page.

The following table describes the priority logic when multiple console cache rules match a request:

Scenario

Priority logic

Example

Different weights

The rule with the higher weight (1-99) takes precedence.

Rule A (directory /image/, weight 50) and Rule B (file extension .jpg, weight 90) both match image/a.jpg. Rule B takes effect because it has a higher weight.

Same weight

The earliest created rule takes precedence.

You configure a directory rule (/static/) and a file extension rule (.js) for a domain name, both with a weight of 60. If the directory rule was created before the file extension rule, a request for /static/app.js matches the directory rule.

The following table describes how each origin response header is handled:

Response header

CDN handling

Notes and examples

Cache-Control

Uses s-maxage (CDN cache duration) first, and then max-age.

Example: s-maxage=86400, max-age=3600

Expires

Specifies the expiration time. This header is used only if no Cache-Control header is present.

Example: Expires: Wed, 21 Oct 2025 07:28:00 GMT

Last-Modified

The Last-Modified header is a timestamp that indicates when the resource was last modified. The cache duration is calculated as follows: (current time - Last-Modified) × 0.1. The calculated duration is then capped between a minimum of 10 seconds and a maximum of 3,600 seconds.

Example: Last-Modified: Wed, 21 Oct 2023 07:28:00 GMT

ETag

An ETag is a unique identifier generated by the server for a specific version of a resource, typically a hash or version number. By default, a resource with an ETag is cached for 10 seconds.

Example: ETag: "abc123"

Cache rule matching logic

CDN cache expiration rules support two rule types with different matching behaviors:

  • Directory: Uses path prefix matching. For example, configuring /static/ matches all resources under that directory (such as /static/image/1.jpg and /static/css/style.css). Configuring / matches all paths. The directory path must start with a forward slash (/). Each rule supports only one directory.

  • File Extension: Uses an exact match on the extension. Enter extensions without dots, and separate multiple extensions with commas (for example, jpg,css,js). Extensions support alphanumeric characters only, with no restriction on specific extension types. The following types can all be configured:

    • Common static resource extensions: jpg, png, gif, css, js, html

    • Font file extensions: ttf, otf, woff, woff2, eot

    • Dynamic page extensions: php, aspx, jsp

    • Any other alphanumeric extension

Note the following about cache rule matching:

  • When multiple cache rules match the same request (for example, a directory rule and a file extension rule), the rule with the higher Weight takes precedence. Rules with equal weights are resolved by creation order (earlier rule wins). For details, see How it works.

  • If you associate a rule condition (configured in the Rule Engine) with a cache rule, multiple conditions use AND logic; the request must satisfy all conditions to match.

  • If you set a blanket no-cache rule for all dynamic extensions (such as aspx), make sure it does not affect static resources you intend to cache. To cache font files, add a dedicated rule with extensions such as ttf,otf,woff,woff2,eot.

Procedure

Note

The business type you select when you create an accelerated domain name (such as Image and Small File, Large File Download, or On-demand Video/Audio Streaming) does not preset fixed cache durations for specific file extensions or paths. The actual caching behavior is determined by the cache rules you configure in the console. After you create the domain name, configure cache rules based on your resource types and update frequency.

Console (recommended)

  1. In the Alibaba Cloud CDN console, go to the Domain Names page and click Manage next to the target domain name.

  2. On the Cache > Cache Expiration page, click Create Rule to configure a cache rule. The following table describes the parameters.

Parameter

Description

Default/example

Type

Specify the scope of the rule by Directory or File Extension. Directory: sets a uniform cache rule for all resources under a path. File Extension: sets a uniform cache rule for files of a specific type.

Directory, File Extension

Object

Enter a value based on the selected type. Directory: must start with a forward slash (/), for example, /static/. A single forward slash (/) matches all paths. You can add only one directory at a time. File Extension: enter one or more file extensions, separated by commas, for example, jpg,png,css. The entries are case-sensitive and do not support the pipe character or other symbols.

/static/, jpg,png,css

Expire In

The cache duration for resources on POPs. The maximum duration is three years.

Rarely updated static resources (such as images and installers): set a duration of 1 month or longer. Frequently updated static resources (such as JS and CSS files): set a shorter duration, such as 1 to 7 days. Dynamic content (such as PHP and JSP pages): set the duration to 0 seconds (no cache).

0 seconds to 3 years

Honor Origin TTL

This is disabled by default. If enabled, the cache policy of the origin server takes precedence and overrides this rule.

Off

Ignore Origin No-Cache Header

If enabled, CDN ignores the following no-cache directives from the origin server: Cache-Control: no-store, no-cache, or max-age=0, and Pragma: no-cache. Resources are cached according to the console rule.

Off

Follow POP Cache Policy

If enabled, CDN returns its effective cache policy, such as max-age=3600, to the client in the response header.

Off

Force Revalidation

This setting is effective only when the expiration time is set to 0 seconds. Disabled (default, equivalent to the no-store cache policy): the POP does not cache the file, and every request must be forwarded to the origin server to fetch the content. Enabled (equivalent to the no-cache cache policy): the POP caches the file, but every request must be revalidated with the origin server by using the 304 mechanism. This is useful for scenarios that require real-time validation but also need to reduce bandwidth pressure on the origin server.

Off

Weight

The priority of the rule. Valid values: 1 to 99. A higher value indicates a higher priority.

Recommended: Set a high weight for specific paths or file extensions and a low weight for the root directory (/) to achieve fine-grained control.

1 to 99

Rule Condition

Further refine the scope of the rule based on request parameters such as headers and URL parameters. This is not used by default. To configure conditions, use the Rule Engine. When rule conditions are referenced, they are matched based on the priority of the associated conditions, not on the order in which the cache rule was configured.

Do not use

For details about how each rule type matches a request, see Cache rule matching logic.

API

Call the BatchSetCdnDomainConfig API operation to configure multiple domain names in batches. For more information about how to configure parameters for other features, see Domain name configuration functions.

Important

New or modified cache rules apply only to resources that are cached after the change. Resources that are already cached continue to use the previous cache policy until they expire.

Apply rule changes immediately

To apply new rules network-wide immediately, you must manually clear the existing cache. If you change a rule, purge the existing cache by using the Purge and prefetch resources feature. If you add a new rule, prefetch the resource by using the prefetch resource feature.

Verification

After you complete the configuration, use the curl command or browser developer tools to inspect the HTTP response headers of a resource and verify that caching works as expected. For details about when a rule change takes effect, see Apply rule changes immediately.

Method 1: Use the curl command

Run the following command in your terminal to test the configuration.

curl -I "https://your.domain.com/path/to/file.jpg"

In the response headers, check X-Cache: HIT means the request was served from the CDN cache, and MISS means a cache miss that triggers an origin fetch. For details about each header, see Interpret key response headers.

Method 2: Use browser developer tools

Open your browser's developer tools (F12) and go to the Network tab. Access the resource URL, select the request, and check the X-Cache header in Response Headers to determine whether the resource was served from the CDN cache.

Interpret key response headers

X-Cache

Indicates whether the request hit the CDN cache.

  • HIT: The request hit the cache.

  • MISS: The request missed the cache and the resource was fetched from the origin server.

Cache-Control

After you enable Follow POP Cache Policy, CDN returns the effective cache policy on the POP to the client, such as max-age=3600.

X-Swift-SaveTime

  • The time when the resource first entered the CDN POP at the L1 layer that the client directly accesses.

  • The value uses the GMT time format. Example: Sat, 19 Apr 2025 08:58:31 GMT.

Ali-Swift-Global-Savetime

  • The time when the resource first entered a CDN POP. Depending on the cache architecture of the site, this may be an L2 POP or another cache layer.

  • The value is a Unix timestamp. For example, 1745053111 indicates 2025-04-19 16:58:31.

X-Swift-CacheTime

The configured cache duration of the resource on the CDN POP, in seconds. This value is an upper limit rather than a guarantee. Ali-Swift-Global-Savetime is a Unix timestamp and X-Swift-SaveTime is a GMT time value.

  • X-Swift-CacheTime = Ali-Swift-Global-Savetime + the cache expiration configured in CDN - X-Swift-SaveTime.

  • X-Swift-CacheTime may not be exactly equal to the configured cache expiration. The following situations may occur:

    • It equals the configured cache expiration, such as 3,600 seconds.

    • It is slightly shorter than the configured cache expiration. For example, the configured expiration is 300 seconds but X-Swift-CacheTime is 295 seconds. This can occur because an L1 POP experiences high latency when fetching the resource from an L2 POP, or because the clocks on the L1 and L2 POPs are not synchronized.

    • It is negative because the cache expiration was changed and, when the client accesses the resource, the cache on the L1 POP has expired while that on the L2 POP has not. For example, the expiration was changed from 3,600 seconds to 300 seconds. If the client accesses the resource again 600 seconds after its first access, the response contains X-Swift-CacheTime: -300. Purge the cache to make the new expiration time take effect.

  • If a resource is infrequently accessed on a POP, it may be evicted by more frequently accessed resources before it expires. Therefore, its actual cache duration may be shorter than the configured value.

Best practices

  • Use versioned filenames (recommended) — When you update a static resource such as style.css, use a new filename with a version or hash, such as style-v2.css or style-a1b2c3d.css, and update the reference in your HTML. This method ensures that users receive the latest content immediately without requiring a manual CDN cache purge and is the recommended way to update cached content.

  • Effectively use the browser cache — Enable Follow POP Cache Policy to reduce repeated requests to CDN, improve loading speed, and save CDN traffic.

  • Avoid excessively short cache durations — A short cache duration causes CDN to perform frequent origin fetches, which negates the benefits of acceleration and increases your origin server's traffic and costs.

  • Be cautious with long cache durations — A long cache duration can prevent clients from receiving timely content updates. For content that needs frequent updates, make sure to purge the cache or use versioned filenames.

  • Game industry and small file scenarios — For small file resources in the game industry (such as configuration files and asset packages) that are updated infrequently (for example, weekly or biweekly), set the cache expiration time to 15 days or match it to the actual resource update cycle. This balances update speed and acceleration performance. A cache duration that is too short leads to frequent origin fetches and reduces acceleration benefits; one that is too long may serve stale content to players. (Recommended) Use thepurge and prefetch feature to proactively refresh the cache when content is updated.

  • robots.txt and sitemap.xml — These files directly affect search engine crawling. Configure their cache duration separately to prevent them from being matched by a broad extension rule that caches them for too long, which would delay updates. (Recommended) To target only these two files, select Directory as the type, enter the specific file path (such as /robots.txt) in the Object field, and then set a high weight to override broad extension rules. If you prefer to configure by file extension, set a cache duration of 1 hour to 1 day for the txt,xml extensions. An extension rule applies to every resource with those extensions, not only to robots.txt and sitemap.xml. For frequently updated sitemaps, set the cache duration to no more than 1 hour.

Troubleshooting

For how to troubleshoot caching not taking effect, cache misses, a low cache hit rate, and content not being updated, see Troubleshoot cache issues.

Appendix: HTTP cache control mechanisms

This section provides protocol background. For the corresponding cache behavior on POPs, see How it works. The HTTP protocol uses three types of headers for cache control:

  1. Expiration time validation

    When a client requests a resource from a server, both parties agree on an expiration time for the resource. Before this time, the cached copy is considered valid. After this time, the cached copy becomes invalid.

    In HTTP, the following headers are commonly used to control cache expiration:

    Header

    Protocol version

    Description

    Example

    Type

    Pragma

    HTTP/1.0

    Indicates whether the content should be cached. Its value is typically no-cache, meaning the file should not be cached. It is often used for compatibility with servers that support only the HTTP/1.0 protocol.

    Pragma: no-cache

    Request/Response

    Expires

    HTTP/1.0

    The Expires response header specifies the date and time when the cached content becomes stale. If an invalid date is used, such as 0, it means the resource has already expired.

    Expires: Wed, 21 Oct 2022 07:28:00 GMT

    Response

    Cache-Control

    HTTP/1.1

    The Cache-Control response header provides flexible cache control through various directives and is the primary header that modern clients, like browsers, use for this purpose.

    The following three examples indicate that the file should not be cached: Cache-Control: no-cache; Cache-Control: no-store; Cache-Control: max-age=0. Example of a 1-hour cache validity period: Cache-Control: max-age=3600.

    Request/Response

  2. Resource tag validation

    The server includes a resource tag (such as an ETag) in its initial response. When the client requests the same resource again, it sends this tag back to the server for validation. If the resource is unchanged, the server responds with an HTTP status code of 304, allowing the client to use its cached copy. If the resource has changed, the server sends the new content.

    In HTTP, the following headers are commonly used to control cache versions:

    Header

    Protocol version

    Description

    Example

    Type

    Last-Modified

    HTTP/1.0

    Indicates the last modification time of the resource.

    Last-Modified: Wed, 21 Oct 2015 07:28:00 GMT

    Response

    ETag

    HTTP/1.1

    Provides a unique identifier for a specific version of a resource. Comparing ETags can determine if a resource has changed. If it has not, the origin server does not need to send a full response.

    ETag: "33a64df551425fcc55e4d42a148795d9f25f89d4"

    Response

  3. Content negotiation

    Caching software uses a keyword to index objects cached on disk. In HTTP/1.0, the resource URL is used as the keyword. However, different representations of a resource can exist at the same URL. To distinguish them, more information is needed from the client, such as the Accept-Language and Accept-Charset headers. To support content negotiation, HTTP/1.1 introduced the Vary header in the response message. This header lists which request headers are required for content negotiation.

    Content negotiation typically uses the HTTP Vary header to differentiate between different cached copies, allowing different clients to receive different cached copies when requesting the same resource:

Header

Protocol version

Description

Example

Type

Vary

HTTP/1.1

Common examples: The server specifies Vary: Accept-Encoding to inform the receiver (e.g., a CDN POP) that it must cache two versions of the resource: compressed and uncompressed. When a client requests the same resource from CDN, older browsers can receive the uncompressed resource to avoid compatibility issues, while newer browsers can receive the compressed resource to reduce data transfer traffic. The server specifies Vary: User-Agent to identify the type of the browser sending the request. This informs the receiver (e.g., a CDN POP) to cache different versions of the resource based on the type of the browser.

Vary: Accept-Encoding; Vary: Accept-Encoding,User-Agent

Response