Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static website hosted in s3: pages return 404 after refresh

With this bucket policy:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PublicReadGetObject",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::mypage.com/*"
        }
    ]
}

I am deploying a website that is build by reactJS v16+ (react-router-dom v5), and when I open the url, it works perfectly, e.g. from mypage.com, the that goes to /list works fine. Url is loaded as mypage.com/list and the page content is displayed.

However on any child page that is not homepage, for example mypage.com/list, once I refresh the page with browser, it returns S3's 404:

404 Not Found

    Code: NoSuchKey
    Message: The specified key does not exist.
    Key: list

I don't even know how to debug this... any idea?

like image 678
jamesdeath123 Avatar asked May 28 '19 15:05

jamesdeath123


People also ask

Can Amazon S3 run a static website?

After you create a bucket, you can enable static website hosting for your bucket. You can create a new bucket or use an existing bucket. Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ .

Can you host dynamic websites on S3 what about Static websites?

For hosting a dynamic website on AWS, you need to use EC2 product. S3 is only used for storage and static website hosting. Other than EC2, you can also use Lightsail, which is basically a VPS. For hosting on EC2, you will need to launch an empty and install LAMP or any PHP based stack you have on the server.

Is it free to host static website on S3?

This is the little gem I learned from a course on AWS Developer Associate Certificate. Normally, hosting a website on normal web hosting will cost at least $5 / month, but AWS S3 offers the 1-year free tier of 5GB storage and 15GB bandwidth.

Why does Amazon S3 return 404 error?

Amazon S3 generally returns 404 errors if the requested object is missing from the bucket. Before users make GET or HEAD requests for an object, make sure that the object is created and available in the S3 bucket. To check if an object is available in a bucket, you can review the contents of the bucket from the Amazon S3 console.

Can I use Amazon S3 to host a static website?

You can use Amazon S3 to host a static website. On a static website, individual webpages include static content. They might also contain client-side scripts.

How do I fix a 404 nosuchkey error in AWS S3?

If the requested object was available in the S3 bucket for some time and you receive a 404 NoSuchKey error again, then check the following: Confirm that the request matches the object name exactly, including the capitalization of the object name. Requests for S3 objects are case sensitive.

How to fix 404 error in ReactJS?

In that case, a 404 error message is returned to the user. The web server returns a 404 error message when we directly hit a sub URL of a production ReactJS application. A simple solution is to route all requests to the index.html file. Rest the React router will handle this. Update your server configuration based on the web server running.


2 Answers

As @Panther mentioned in the comment, the right way to go is to add an error fallback to index.html in the S3 bucket's static web hosting configuration.

The reason of this is when a URL is hit (either by manual input in the browser or a refresh), it sends a request to /list to the S3's root server (the one managed by AWS) before it hits our bucket. That S3 server have no idea if this is a reactjs app or not, so it goes into the bucket to look for the /list in the root of my bucket, which doesn't exist, so it returns the 404 error.

However by adding the error fallback, now when it gets 404, it redirects the request to index.html, where the react app defined and loaded into. In this case, the /list will go through the normal flow to reach the right router that handles page rendering, problem solved.

like image 61
jamesdeath123 Avatar answered Oct 18 '22 21:10

jamesdeath123


So, I had the same experience today, and the answer by @jamesdeath123 helped me too know the cause. In my case I am using create-react-app, so I was finding a way to generate the error.html to be used in s3 bucket. I realized though, that all I need to do is to set the error page to index.html as well.

like image 22
devbear Avatar answered Oct 18 '22 21:10

devbear