My Situation
I have a web api hosted in an EC2 instance. I am trying to configure a cloudfront instance "infront" of that EC2 instance.
However, I have not been able to get my cloudfront to forward requests to the EC2 instance. I get hit with an error response like this:
Access to XMLHttpRequest at 'https://api.example.com' from origin 'https://example.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No access-control-Allow-Origin header is present on the requested resource
However, if I change my DNS to point https://api.example.com to EC2 instance's IP address, it works.
What I have done so far
- Configured to use correct SSL certificate (for a different problem earlier)
- Configured my CF distribution's behaviors to Whitelist Headers: "Origin"
- Configure my CF distribution's behaviors to "All" - (which disables caching)
- Invalidated cloudfront cache
What I am trying to do
I came across this AWS doc titled "Configuring CloudFront to Respect CORS Settings".
However, it only says "Custom origins – Forward the Origin header along with any other headers required by your origin."
But... How do I do that? How do I forward origin header along with any other headers required? The docs doesn't specify or link to another docs to do it.
I have spent 4 hours or so now and it's extremely frustrating because Cloudfront takes ~30 minutes to deploy.
Origin
,Access-Control-Request-Headers
, andAccess-Control-Request-Method
. But if forwarding all headers didn't reasult in satisfactory behavior (you said you tried that earlier), then forwarding that subset isn't going to work, either, of course. Reconfigure to forward all headers, do an invalidation of/*
and start testing again after everything settles. Observe your server logs. Observe request and response headers. – Michael - sqlbot