3

AWS Cloudfront document says:

If you set the TTL for a particular origin to 0, CloudFront will still cache the content from that origin. It will then make a GET request with an If-Modified-Since header, thereby giving the origin a chance to signal that CloudFront can continue to use the cached content if it hasn't changed at the origin

I need to configure my Dynamic Content. I have already set TTL to 0.. I want every request to go to Origin always. Is there a way I avoid this additional GET request with an If-Modified-Since header ! Why this extra request everytime !

Deepak Singhal
  • 10,568
  • 11
  • 59
  • 98

2 Answers2

4

Is there a way I avoid this additional GET request

It sounds as if you are misinterpreting the what you are reading. Unfortunately, you didn't cite the source, so it's difficult to go back and pick up more context; however, this is not referring to an "extra" request.

It will then make a GET request with an If-Modified-Since header

This refers to each time the object is subsequently requested by a browser. CloudFront sends the next request with If-Modified-Since: so that your origin server has the option of returning a 304 Not Modified response... it doesn't send two requests to the origin in response to one request from a browser.

If your content is always dynamic, return Cache-Control: private, no-cache, no-store and set Minimum TTL to 0.

http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Expiration.html#ExpirationDownloadDist

Michael - sqlbot
  • 169,571
  • 25
  • 353
  • 427
  • Yes content is always dynamic..I don't think setting anything on response like Cache-control etc.. will change things.. Suppose browser makes request which goes to CF ..now CF makes first If-Modified-Since request and then makes another request.. So, I feel there has to be some config on CF where I mention; just always go to origin – Deepak Singhal Oct 16 '16 at 12:27
  • 1
    You may think that, but it is still true. The documentation, at the link provided, is also very clear. If Minimum TTL is set to 0 *and* the origin server sends `Cache-Control: no-cache`, then CloudFront will **"respect the header"** -- and not cache the content. If it does not cache the content, then sending an `If-Modified-Since` request would be impossible and subsequent requests will simply go to the origin. – Michael - sqlbot Oct 16 '16 at 15:50
  • 1
    I also suspect you misunderstand how `If-Modified-Since` works. If CloudFront sends a request for a page `If-Modified-Since` then the origin has two choices: respond `200 OK` along with the new content, or respond `304 Not Modified` with *no content* since this is directing the cache to use the old content. Either way, only one request occurs. There is no second request. – Michael - sqlbot Oct 16 '16 at 15:53
  • Firstly thanks Michael for looking into it.. Both of these last two comments makes sense; but don't you think they are bit of contradicting each other. Are you sure If-Modified-Since which is NOT GET request ; can get 200 as the response ! Also, as I can see you are MySQL expert; can u take a quick look at stackoverflow.com/questions/40055423 – Deepak Singhal Oct 16 '16 at 16:31
  • It **is still** a `GET` request. `If-Modified-Since` is an attribute of a `GET` (or `HEAD`) request. *"The `If-Modified-Since` header field makes a `GET` or `HEAD` request method conditional on the selected representation's modification date being more recent than the date provided in the field-value. Transfer of the selected representation's data is avoided if that data has not changed."* -- [RFC-7323](https://tools.ietf.org/html/rfc7232#section-3.3) -- Otherwise, of course, transfer is not avoided, because such as request is otherwise treated as a normal `GET` (or `HEAD`) request. – Michael - sqlbot Oct 16 '16 at 18:46
  • Thanks Michael for your time :) – Deepak Singhal Oct 17 '16 at 02:26
2

This is the answer I got from AWS:

However, if you forward all headers for that particular origin, the request will go to the origin every time without the If-Modified-Since header mentioned [1]. Please view the excerpt from the link below for further detail:

“Forward all headers to your origin Important If you configure CloudFront to forward all headers to your origin, CloudFront doesn't cache the objects associated with this cache behavior. Instead, it sends every request to the origin.”

Deepak Singhal
  • 10,568
  • 11
  • 59
  • 98