1
votes

My idea is to use cloudfront to cache one entire site that is in EC2. But I am having a lot of trouble to do that. If I set the cloudfront option Forward Headers to none, the default index.html from apache is cached (probably because the CF is caching the LB URL). If I set Forward Headers to whitelist and add host in Whitelist Headers the site displays normally but I can see in the Response Header:

X-Amz-Cf-Id:Ij8TsEU2hPehG53Op6LX1zFDmZfYWBOVFn8el8ApicCQYdYEi69HsQ==
X-Cache:Miss from cloudfront

Here is the current scenario:

  1. Route 53 with A alias pointing to CloudFront (for my naked domain and www)
  2. CloudFront with Origin Domain Name = load balancer DNS name, also the cname are configured to naked domain and www
  3. Load Balancer with EC2
  4. EC2 with several sites (I just want use CF in one of them)

Could anyone help me how to configure it correctly?

Update 1 Just a quick observation, the site is in WordPress. But, I don't think that is necessary to use any cdn module (like W3 Total Cache) because I want that the entire site be cached.

1
X-Cache:Miss from cloudfront is exactly what you should expect to see, the first time a resource is requested through Cloudfront. It means Cloudfront didn't have a cached copy of the page. Refresh, and you should start to see "hit" instead of "miss." Have you tried that?Michael - sqlbot
Hello Michael, unfortunately no... I did a couple of page reload after configure and now (after 9 hours) I still getting "X-Cache:Miss from cloudfront". The curios here, is that if I set the Forward Headers to None, the default index.html is cachedThomas
In the Behavior section of your CF distribution, what do you have Object Caching set to? If it's set to "Use Origin Cache Headers" then you need to be returning a Cache-Control header for each request. If you're not doing that then CF will effectively expire every request immediately. You can test this by setting Object Caching to Customize and specifying a TTL.Bruce P
@BruceP, you are right. Now it's working fine. Could you write an answer just to register in stackoverflow?Thomas

1 Answers

0
votes

Take a look at the Behavior section of your CloudFront distribution. There's a setting there for Object Caching. If it's set to "Use Origin Cache Headers" then you need to be returning a Cache-Control header for each request. If your application isn't setting a Cache-Control header then set Object Caching to "Customize" and in the "Minimum TTL" field set an appropriate TTL for your objects.

If you are not setting your own Cache-Control header and you leave this setting to the default of "Use Origin Cache Headers" then you'll essentially be defeating the purpose of a CDN since each request made through the CDN will effectively expire immediately so every request through the CDN will still fetch from the origin.