使用 AWS re:Post 即表示您同意 AWS re:Post 使用條款

How to prevent Cloudfront to cache some types of files?

0

Hi,

Currently we only want to use Cloudfront to cache .EXE files. For all other types of files, we do NOT want to cache them. However, it seems that Cloudfront will cache all files. And one of my sitemap file at https://www.datanumen.com/sitemap_index.xml is also cached by Cloudfront as https://download.datanumen.com/sitemap_index.xml. How to prevent that?

已提問 8 個月前檢視次數 887 次
5 個答案
1

To disable caching completely, go to CloudFront console, click on the ID value of your CloudFront distribution and navigate to Behaviors section. Select and edit the default entry. Your CachePolicy is probably set to CachingOptimized. Change it to CachingDisabled and save.

To cache only .EXE file, click Create behavior to create a new entry. Use *.exe for Path pattern and set CachePolicy to CachingOptimized

Your configuration should be similar to screenshot below Enter image description here

Since your files are already cached, you may want to clear CloudFront cache as well. Go to Invalidations section and click Create invalidation

Edit: Your configuration looks correct. *.exe files are cached as per curl command below

% curl -I https://download.datanumen.com/downloads/file/daccr.exe
HTTP/2 200
...
vary: Accept-Encoding
x-cache: Hit from cloudfront
...

Others are not cached by CloudFront

% curl -I https://download.datanumen.com/
HTTP/2 200
...
x-cache: Miss from cloudfront

Any caching if any is by your origin (www.datanumen.com). It is using Cloudflare. Suggest you change your CloudFront origin to your EC2 (?) domain name.

AWS
專家
已回答 8 個月前
0

Hi,

I check my distribution and find it already have a behavior, so I think the most simple way is to edit it and change the pattern from * to *.exe, as below: Enter image description here

But I cannot make the change. Why?

已回答 8 個月前
  • Do not change path pattern for this (Default). Only change CachePolicy to CachingDisabled. Instead, create a new behavior with the same values except for path pattern (*.exe) and caching policy (CachingDisabled). I have updated my post with screenshot

0

Hi, Mike

I just follow your instructions, except that we have a custom cache policy with our own defined settings, below is my setting: Enter image description here And I have clear the cache as below: Enter image description here

But, when visiting https://download.datanumen.com (the domain used by Amazon Cloudfront), I will still see our company page. Am I doing something wrong?

已回答 8 個月前
  • Alan, when I go to the following URL (https://download.datanumen.com/sitemap_index.xml), I see that the object is not being cached. CloudFront is returning a 200 (not a 304), and the X-Cache header returns "Miss from cloudfront". It looks like caching is working as expected. What are you expecting to see?

0

Hello.

I think the following documents will be helpful.
https://repost.aws/knowledge-center/prevent-cloudfront-from-caching-files

I thought it would be a good idea to target only ".exe" by setting the cache key etc.
https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/controlling-the-cache-key.html

profile picture
專家
已回答 8 個月前
profile picture
專家
已審閱 8 個月前
0

I recommend following Mike L's instructions, that is exactly what you need to do.

To elaborate, CloudFront uses Behaviors, and you can think of them as rules. They have an order of execution, with 0 being first, 1 next, then 2, etc. Inside a Behavior you define what path you want to match on, in this case *.exe. You also can define other things, such as a caching policy, which origin to use, etc.

profile pictureAWS
已回答 8 個月前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南