Introduction
The Cache Expiry Time option of both CDN Static and CDN Static Push services allows you to control cache system on our CDN edge servers. The Cache Expiry Time option determines for how long your content should be stored in the CDN cache.
Note that when your content expires in the CDN cache then (if requested) it is automatically pulled from your Origin Domain/Origin Storage again and the old CDN cache (if still exists) is replaced with the newly pulled content.
You have two options where you can set cache expiry time.
- In your application
- In CDN service settings
Please note that the option 1 overrides the option 2.
We recommend to set cache expiry time to far future to increase your cache hit ratio and to improve the CDN performance.
Setting cache expiry time in your application
You can set cache expiry time in your web server configuration.
This configuration is usually on a file type basis.
An example how to set cache expiry time for static assets in Apache .htaccess
# 30 DAYS - Static assets (images, CSS, JS, etc.) <FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$"> Header set Cache-Control "max-age=2592000, public" </FilesMatch> # 1 DAY - Text-based files (XML, TXT) <FilesMatch "\.(xml|txt)$"> Header set Cache-Control "max-age=86400, public, must-revalidate" </FilesMatch> # NO CACHE - Prevent caching for HTML pages <FilesMatch "\.(html|htm)$"> Header set Cache-Control "no-store, no-cache, must-revalidate, private" </FilesMatch>
An example how to set cache expiry fro HLS streaming time in Apache .htaccess
# HLS Playlist files (.m3u8) - No caching (important for live streaming) <FilesMatch "\.m3u8$"> Header set Cache-Control "no-cache, no-store, must-revalidate" </FilesMatch> # HLS Segment files (.ts) - Cache for 30 minutes <FilesMatch "\.ts$"> Header set Cache-Control "max-age=1800, public, must-revalidate" </FilesMatch>
An example of how to set cache expiry time for static assets in Nginx
# 30 DAYS - Static assets (images, CSS, JS, etc.) location ~* \.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$ { expires 30d; add_header Cache-Control "public"; } # 1 DAY - Text-based files (XML, TXT) location ~* \.(xml|txt)$ { expires 1d; add_header Cache-Control "public, must-revalidate"; } # NO CACHE - Prevent caching for HTML pages location ~* \.(html|htm)$ { expires off; add_header Cache-Control "no-store, no-cache, must-revalidate, private"; }
An example of how to set cache expiry for HLS streaming in Nginx
# HLS Playlist files (.m3u8) - No caching (important for live streaming) location ~* \.m3u8$ { expires off; add_header Cache-Control "no-cache, no-store, must-revalidate"; } # HLS Segment files (.ts) - Cache for 30 minutes location ~* \.ts$ { expires 30m; add_header Cache-Control "public, must-revalidate"; }
Setting cache expiry time in CDN service settings
You can set the Cache Expiry Time option on the Services/New Service and Services/Settings pages.
Setting cache expiry time in this way applies to all content of the particular CDN service.
The If-Modified-Since validation
The If-Modified-Since validation increases CDN service performance by increasing its cache-hit ratio.
How it works
If a value (time) of the HTTP header Last-Modified in the CDN cache is the same as on the origin server then the corresponding content (CDN and origin) is considered to be identical.
Example
Assume that a file expires in the CDN cache. Assume also that the file still exists in the CDN cache and that it is requested by a visitor. If the file passes the If-Modified-Since validation then it is not pulled from the Origin Domain/Origin Storage again but the CDN edge server will stay using the old cache.
Notes
After changing origin HTTP headers you might need to purge your content from the CDN cache as it is cached with the old HTTP headers.
What next?
Read about the following topics.