简体   繁体   English

Javascript React 单页应用程序 + Amazon S3:永久链接问题

[英]Javascript React Single Page Application + Amazon S3: Permalinks issue

I am using an S3 bucket as static web solution to host a single page React application.我使用 S3 存储桶作为静态 Web 解决方案来托管单页 React 应用程序。 The react application works fine when I hit the root domain s3-bucket.amazon.com and the HTML5 history api works fine every time I click on a link the new url looks fine: _http://s3-bucket.amazon.com/entities/entity_id_ The problem happens when I use permalinks to access the application.当我点击根域s3-bucket.amazon.com时,react 应用程序工作正常,每次点击链接时 HTML5 历史 API 工作正常,新 url 看起来很好:_http://s3-bucket.amazon.com/ entity/entity_id_ 当我使用永久链接访问应用程序时会出现问题。 Let's assume I am typing the same url (_http://s3-bucket.amazon.com/entities/entity_id_) in the browser I will get the following error from Amazon S3:假设我在浏览器中输入相同的 url (_http://s3-bucket.amazon.com/entities/entity_id_) 我将从 Amazon S3 收到以下错误:

404 Not Found
Code: NoSuchKey
Message: The specified key does not exist.
Key: users
RequestId: 3A4B65F49D07B42C
HostId: j2KCK4tvw9DVQzOHaViZPwT7+piVFfVT3lRs2pr2knhdKag7mROEfqPRLUHGeD/TZoFJBI4/maA=

Is it possible to make Amazon S3 to work nicely with permalinks and HTML5 history api?是否可以使 Amazon S3 与永久链接和 HTML5 历史记录 api 很好地工作? Maybe it can act as proxy?也许它可以充当代理?

Thank you谢谢

Solution using AWS CloudFront:使用 AWS CloudFront 的解决方案:

Step 1: Go to CloudFront步骤 1:转到 CloudFront

Click on distribution id单击分发 ID

Go to the Error page tab转到错误页面选项卡

Click on Create Custom Error Response单击创建自定义错误响应

Step 2: Fill the form as第 2 步:将表格填写为

HTTP Error Code: 404 HTTP 错误代码:404

TTL: 0 TTL:0

Custom Error Response: Yes自定义错误响应:是

Response Page Path: /index.html响应页面路径:/index.html

HTTP Response Code: 200 HTTP 响应代码:200

source: https://medium.com/@nikhilkapoor17/deployment-of-spa-without-location-strategy-61a190a11dfc来源: https : //medium.com/@nikhilkapoor17/deployment-of-spa-without-location-strategy-61a190a11dfc

Sadly S3 does not support the wildcard routing required for single page apps (basically you want everything after the host in the url to serve index.html, but preserve the route.遗憾的是,S3 不支持单页应用程序所需的通配符路由(基本上,您希望 url 中主机之后的所有内容都提供 index.html,但保留路由。

So www.blah.com/hello/world would actually serve www.blah.com/index.html but pass the route to the single page app.所以www.blah.com/hello/world实际上会为www.blah.com/index.html提供服务,但将路由传递给单页应用程序。

The good news is you can do this with a CDN such as Fastly (Varnish) in front of S3.好消息是您可以在 S3 前使用诸如 Fastly (Varnish) 之类的 CDN 来做到这一点。 Setting up a rule such as:设置规则,例如:

if(req.url.ext == "" ) {
    set req.url = "/index.html"; 
}

This will redirect all non asset requests (anything without a file extension) to index.html on the domain.这会将所有非资产请求(任何没有文件扩展名的请求)重定向到域上的 index.html。

I have no experience running SPA on Amazon S3, but this seems to be a problem of url-rewriting.我没有在 Amazon S3 上运行 SPA 的经验,但这似乎是 url 重写的问题。 It is one thing do have the history (html5) api rewrite your url when running your application / site.在运行您的应用程序/站点时,历史 (html5) api 重写您的 url 是一回事。 But allowing rewritten urls to be accessible when refreshing or cold-surfing to your site definitely needs url-rewriting on a server level.但是,当刷新或冷浏览您的站点时,允许访问重写的 url 肯定需要在服务器级别进行 url 重写。 I'm thinking web.confg (IIS), .htaccess (Apache) or nginx site configuration.我在考虑 web.confg (IIS)、.htaccess (Apache) 或 nginx 站点配置。

It seems the same question already got asked some time ago: https://serverfault.com/questions/650704/url-rewriting-in-amazon-s3似乎前段时间已经问过同样的问题: https : //serverfault.com/questions/650704/url-rewriting-in-amazon-s3

Specify the same name for the index and error files on "Static website hosting" properties.为“静态网站托管”属性上的索引和错误文件指定相同的名称。 See linked image 1 .请参阅链接图像1

Old question but simplest way would be to use hash routing.老问题,但最简单的方法是使用散列路由。 So instead of mydomain.com/somepage/subpage it would be mydomain.com/#/somepage/subpage所以不是 mydomain.com/somepage/subpage 而是 mydomain.com/#/somepage/subpage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM