[英]Cloudfront returns 403 forbidden when refreshing page
I have uploaded a react app to AWS S3 and am using static website hosting. 我已将React应用上传到AWS S3,并且正在使用静态网站托管。 I then linked a cloudfront distribution to the s3 bucket. 然后,我将一个Cloudfront发行版链接到s3存储桶。
I am able to navigate to the site and it functions properly except when I navigate to a new page my-domain/new-page. 我可以导航到该站点,并且可以正常运行,除非导航到新页面my-domain / new-page。 At first it succeeds but if I try and load the page directly or refresh I get a 403 forbidden error. 一开始它成功,但是如果我尝试直接加载页面或刷新,则会收到403禁止错误。
Using react with S3 and CloudFront, I had this or a similar issue where loading the initial index page and then linking to other pages worked just fine (push state changes), but if I refreshed the page or linked directly to the page, it came up as "Access Denied," status 403. 使用React与S3和CloudFront时,我遇到了一个或类似的问题,即加载初始索引页面,然后链接到其他页面也很好(推送状态更改),但是如果刷新页面或直接链接到页面,它就来了并显示为“访问被拒绝”状态403。
The solution I found was to add custom error pages for 403 and 404 errors to the CloudFront configuration. 我发现的解决方案是将403和404错误的自定义错误页面添加到CloudFront配置中。
S3 responds 403 when an object doesn't exist as would be the case with pages in client side routing. 当对象不存在时,S3响应403,就像客户端路由中的页面一样。
I have noticed this happens when you change the object and look for the object immediately. 我注意到当您更改对象并立即查找对象时会发生这种情况。 To resolve this issue, we used to cache the object for 5 mins, so CloudFront will serve cached version when available. 为了解决此问题,我们过去将对象缓存了5分钟,因此CloudFront将在可用时提供缓存的版本。
This also relates if you are changing the object and applying the ACL along with the object. 如果要更改对象并将ACL与对象一起应用,则这也有关系。 Ensure you have public read as policy for the bucket, so you can reduce outage time. 确保您已将存储桶公开阅读作为策略,这样可以减少中断时间。
{
"Version":"2012-10-17",
"Statement":[
{
"Sid":"AddPerm",
"Effect":"Allow",
"Principal": "*",
"Action":["s3:GetObject"],
"Resource":["arn:aws:s3:::examplebucket/*"]
}
]
}
Enabling cache in CloudFront also solves the issue. 在CloudFront中启用缓存也可以解决此问题。
Hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.