简体   繁体   English

刷新页面时Cloudfront返回403禁止

[英]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,就像客户端路由中的页面一样。

Cloud Front自定义错误的屏幕截图

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.

相关问题 S3 CloudFront React SPA由于标签而返回403,但无法设置403错误页面 - S3 CloudFront React SPA returns 403 due to hashtag but cannot setup 403 error page 部署到Netlify时刷新页面将返回空白页面 - Refreshing page when deployed to Netlify returns empty page axios 请求令牌在刷新页面时返回 null - axios request token returns null when refreshing page 如何使用 Spring Security 自定义 403 Forbidden/Access Denied Page - How to customize the 403 Forbidden/Access Denied Page using Spring Security 更改s3存储桶文件时出现403禁止错误 - 403 forbidden error when changing s3 bucket files 为什么在获取 API 数据时会出现 403 禁止? - Why get 403 forbidden when get API data? 使用带有 Apache 的 React Router 时禁止出现错误 403 - Error 403 forbidden when using React Router with Apache 代码:403,消息:“禁止”,响应:单击峰会表单按钮时 - code: 403, message: 'Forbidden', response: when click summit form button 进行API调用时反应给我403禁止的错误 - React giving me 403 forbidden error when making an API call 使用 React(Django 后端)进行 axios 调用时出现 403 禁止请求 - 403 Forbidden request when making an axios call with React (Django backend)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM