Reputation: 4412
I have my angular2 webapp hosted in s3. it has cloudfront as well. Its connecting to nodejs as backend which is in different machine in AWS EBS.
My web application works fine.But when i refresh any page , its throwing 404 Not Found with the below message.
Code: NoSuchKey
Message: The specified key does not exist.
Key: user/adduser/edit
RequestId: CA306FFABB30FB75
HostId: WAp7/BZMHH0UpE3z2dJLJT4gqEwv7u/LKbFTUuHH86lRyReLXvODLgTuTw+3emnTJ3jDj2VpiYo=
Can you help to resolve this?
Upvotes: 8
Views: 4365
Reputation: 21
I was having the exact same issue with my S3 website endpoint. The issue arises when you use some internal routing in your angular/react app which changes the URL. So, now the S3 tries to find the new file that is in your URL but fails to do so because in the build folder, there is just a single index.html
file which handles the routing on its own.
Let's understand with my example. Let's say the S3 endpoint is: http://bucketname.s3-website.region.amazonaws.com.
So, when I was trying to access http://bucketname.s3-website.region.amazonaws.com or http://bucketname.s3-website.region.amazonaws.com/index.html, everything was working fine, even on refreshing the page. Now, my website has a default route '/dashboard'. When the page loads on the root route '/', it gets redirected to '/dashboard' which works fine. But when I refresh the page, the URL changes to http://bucketname.s3-website.region.amazonaws.com/dashboard. So, now the S3 tries to find this file called 'dashboard' in the bucket but it fails to do so hence the error "The specified key does not exist".
The solution is simple and it is to return an error page on every such error. This error page should be set to "index.html". So now when I access http://bucketname.s3-website.region.amazonaws.com/dashboard, S3 knows that it will throw an error, so it returns the index.html page which by itself handles the routing according to your angular/react app and it shows the dashboard page properly.
Steps to set this error page:
Now everything should work fine! Do support by upvoting if you found this useful and let me know if there can be any improvements or updates in this answer.
Upvotes: 2
Reputation: 9356
I was getting the same error but using GCP (Google Cloud Platform) storage buckets instead of Amazon S3. As @janier mentions configuring my app's main index page and error page settings to index.html
resolved the issue.
In GCP go to your storage bucket in question and use the right side menu to access the Website Configuration as seen below:
In the following modal set your index and error pages to index.html
Then simply refresh your app and it should fixed!
Upvotes: 0
Reputation: 21
I was having this error that looks very similar to yours:
error 404: no such key
on refreshing S3 website webpage.
Try adding "index.html" in the S3 bucket configurations:
This solution fixed my problem and I am confident that it will also fix yours.
Upvotes: 2
Reputation: 4412
I am not sure if its the correct thing to do.But it fixed my issue. I configured s3 404 redirection url to be my index.html.ie pointed my 404 redirection to index.html itself. It worked.
Upvotes: 20