If you are using a CMS or custom CDN implementation that is not included in the CDN no-cache rules, you may desire to apply a rule to prevent the CDN from caching specific files or paths. This article will walk through the process of setting up a custom rule to do just this.
If you have not already, please check this guide to get started.
Creating the Rule
- Navigate to Sites and select the CDN Site you would like to create the rule for
- Navigate to your EdgeRules tab
- Select Add Delivery Rule
- In the IF section, select the URL Option, with the Matches modifier, and paste in
*://edge.address/
followed by the directory or file you would like to exclude from cache. (edge.address can be replaced with your Edge Address or by one your Delivery Domains) - In the THEN section select the Do Not Cache rule to to remove the
cache-control
header and instruct the CDN to avoid caching the assets.
Adding the AND HTTP Method option is optional in this case. But in the above example, this will apply the rule only to POST requests to this website's administrative page.
Verifying the Rule
Since this rule modifies a public response header to the CDN Response, verify the new EdgeRule by checking the headers from any CDN asset using browser inspect tools or cURL requests.
For browser tools, right-click anywhere on the website and select inspect or inspect element and navigate to the network tab. If no request is seen, refresh the page with this tab open to view them as they come in.
Check the request for the matching file or directory to ensure they are excluded from cache.
Before implementing the EdgeRule, there is a cache-control
header specifying the TTL for the file, and the CDN has cached the file, as indicated by the .c
at the end of x-hw
response.
After implementing the EdgeRule, the cache-control
header is removed, and the CDN will no longer cache the file, indicated by the .p
at the end of the .p at the end of the
x-hw
response.
When performing a cURL test, the option -I should be used to see the headers appropriately. Below is an example of a curl, where the x-hw
header shows the asset was not cached.
curl -I https://c7j8f9a6.stackpathcdn.com/js/jquery/jquery.js?ver=1.12.4
HTTP/2 200
date: Thu, 20 Dec 2018 17:06:27 GMT
content-length: 97184
content-type: application/javascript
last-modified: Tue, 21 Aug 2018 19:29:33 GMT
server: Apache
etag: "17ba0-573f70ba995a3"
accept-ranges: bytes
vary: Accept-Encoding
access-control-allow-origin: *
x-hw: 1545325587.cds087.da2.hn,1545325587.cds030.da2.sc,1545325587.cds030.da2.p
If any questions arise or any assistance can be further provided, feel free to drop us a line at hi@stackpath.com , we'll be happy to help you out.