问题
I have a CloudFront distribution which is pointing to an S3 bucket holding my website's static content. This is done using S3 REST API endpoints, so the origin's domain name and path looks like this:
bucket-name.s3.amazonaws.com
So far, this is working and I know this because when I open the browser and enter https://xxxxxxxxxxxxx.cloudfront.net/index.html
, it downloads the html file. But this is not all that I'm after. My website is an SPA (single page application) written in React. This means not all the resources mentioned in the client-side code can actually be found on the bucket. Those resources that are not physically available in the bucket are virtual resources and are supposed to be handled by the client-side code.
For this to work, if the user enters the address of a virtual URL into the browser, the server first needs to verify whether that resource exists or not. If it does not, the server should return index.html
file instead of 404 error. This can be set up in the CloudFront's Error Pages.
So I created a Custome Error Response for my CloudFront distribution handling all the 404 errors, returning the index.html
file with 200 ok status. But this does not work.
When enter the URL https://xxxxxxxxxxxxx.cloudfront.net/virtual-url
, it returns:
<Error>
<Code>AccessDenied</Code>
<Message>Access Denied</Message>
<RequestId>39E3967AA5F16693</RequestId>
<HostId>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</HostId>
</Error>
Even the naked URL https://xxxxxxxxxxxxx.cloudfront.net
returns the exact same error.
So my question is, how can I use Error Pages of CloudFront when the distribution is using an S3 REST API endpoint?
P.S. When I was using the S3's static website endpoint (bucket-name.s3-website.us-east-1.amazonaws.com
), the same mechanism worked.
来源:https://stackoverflow.com/questions/54411610/setting-up-error-pages-for-cloudfront-when-it-is-using-an-s3-rest-api-endpoints