How can I exclude URLs within Amazon Cloudfront?

11,191

At the risk of helping you solve the wrong problem, I should point out that if you configure CloudFront to forward query strings to the origin server, then the response will be cached against the entire URI -- that is, against the path + query-string -- not against the path, only, so...

/dynamic-page?r=1
/dynamic-page?r=2
/dynamic-page?r=2&foo=bar

...would be three different "pages" as far as CloudFront is concerned. It would never serve a request from the cache unless the query string were identical.

If you configure CloudFront to forward query strings to your origin, CloudFront will include the query string portion of the URL when caching the object. [...]

This is true even if your origin always returns the same [content] regardless of the query string.

http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/QueryStringParameters.html

So, it shouldn't really be necessary to explicitly and deliberately avoid caching on this page. The correct behavior should be automatic, if CloudFront is configured to forward the query string.

Additionally, of course, if your origin server sets Cache-Control: private, no-cache, no-store or similar in the response headers, neither CloudFront nor the browser should cache the response.

But if you're very insistent that CloudFront be explicitly configured not to cache this page, create a new cache behavior, with the path pattern matching /do-not-cache*, and configure CloudFront to forward all of the request headers to the origin, which disables caching for page requests matching that path pattern.

Share:
11,191
user3452136
Author by

user3452136

Updated on June 24, 2022

Comments

  • user3452136
    user3452136 almost 2 years

    I plan on using Amazon Cloudfront CDN, and there is a URL that I need to exclude. It's a dynamic URL (contains a querystring).

    For example, I want to store/cache every page on my site, except:

    http://www.mypage.com/do-not-cache/?query1=true&query2=5
    

    EDIT - Is this what invalidating 'objects' does? If so, please provide an example.

    Thanks