react router doesn't work in aws s3 bucket

前端 未结 12 1490
深忆病人
深忆病人 2020-11-28 08:08

I deployed my React website build/ folder into an AWS S3 bucket.

If I go to www.mywebsite.com, it works and if I click on a ta

12条回答
  •  遥遥无期
    2020-11-28 08:27

    This question already has several great answers. Although the question is now old, I faced this problem today, so I feel that perhaps this answer can help.

    S3 has two kinds of end points, and if you are facing this error, you have probably selected the S3 bucket directly as the endpoint in the Origin Domain Name field for your CloudFront Distribution.

    This would look something like: bucket-name.s3.amazonaws.com and is a perfectly valid endpoint. In fact, it would seem that AWS does expect this as default behaviour; this entry will be in the drop-down list you have when you are creating your CloudFront distribution.

    However, doing this and then setting error pages may or may not solve your problem.

    S3 has, however, a dedicated Website Endpoint. This is accessible from your S3 Bucket > Properties > Static Website Hosting. (You will see the link on the top).

    You should use this link instead of the original auto-populated link that comes up in CloudFront. You can put this in while creating your distribution, or after creation, you can edit from the tab Origins and Origins Groups, and then invalidating caches.

    This link will look like: bucket-name.s3-website.region-name.amazonaws.com.

    Once this propagates and changes, this should then fix your problem.

    tl;dr: Don't use the default S3 endpoint in CloudFront. Use the S3 website endpoint instead. Your origin domain should look like this: my-application.s3-website.us-east-1.amazonaws.com and not like my-application.s3.amazonaws.com.

    More Information about website endpoints is available in the AWS Documentation here.

提交回复
热议问题