问题
I use Amazon CloudFront to provide my frontend. My backend runs on Amazon EC2.
I assigned CloutFront a domain of its own and set the CNAME
entry. In my backend I added the new domain as allowed origin host. But now I get crossorigin
error messages. Why might that be?
回答1:
Make sure you have following setup:
- Make a curl directly on EC2 with Origin header and see if you get the CORS response. (For OPTIONs requests to)
- OPTIONS requests are allowed on CloudFront.
- Origin header Whitelisted on CloudFront.
- CloudFront bydefault forwards Origin header but it doesn't consider Origin header into the cache key which means if you first make a request www.example.com/test without Origin header, CloudFront caches it and for the next requests comes with Origin header, it serves it from Cache, make sure you clear the cache after making these changes.
5.Check if you're not getting 4xx response for the CORS request (Use Mozilla to test).
回答2:
Did you check the CORS support? for exmaple by forwarding the origin header? see https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/forward-custom-headers.html
Try calling the URL using
curl -H "origin: example.com" -v "https://examplec.com"
and check the output
see also https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/header-caching.html#header-caching-web-cors
来源:https://stackoverflow.com/questions/55357846/amazon-cloudfront-url-issue