Cloudfront issue

0

I have pushed the new images to my server. When I make a request to CloudFront, it fails to fetch the image from the origin because it is not cached before, and it throws multiple 301 redirects. However, after clearing the cache in CloudFront, I can see the new image. But again, CloudFront sends a request to the origin and gets the response. When I make a request for the first time, it fails, but after clearing the cache, it works. What could be the reason?

Thanks in advance!

asked 2 months ago180 views
1 Answer
0

The issue you are facing is likely due to the way CloudFront handles the initial request for an object that is not yet cached in the Edge Locations.

Here's what typically happens when you request an object from CloudFront for the first time:

1 - CloudFront receives the request and checks if the object is cached in the Edge Location closest to the client.
2 - If the object is not cached, CloudFront sends a request to the origin server (your server) to fetch the object.
3 - During this initial request to the origin server, CloudFront follows any redirects (301 or 302) that the origin server may respond with.
4 - Once CloudFront receives the final response from the origin server (after following any redirects), it caches the object in the Edge Location.
5 - CloudFront then serves the cached object to the client.\

The issue you are facing is likely caused by the 301 redirects that your origin server is sending during the initial request from CloudFront. Since CloudFront follows these redirects, it may be causing a failure or timeout before it can retrieve and cache the object.

If your origin returns a redirect status code (for example, 301 or 307), CloudFront doesn’t follow the redirect. CloudFront passes along the 301 or 307 response to the viewer, who can follow the redirect by sending a new request.

I recommend you to check How CloudFront processes HTTP 3xx status codes from your origin.

profile pictureAWS
answered a month ago
profile picture
EXPERT
reviewed a month ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions