当前位置: 首页 > 知识库问答 >
问题:

在S3+Cloudfront上以同一URL托管多个SPA web应用程序

巫马淳
2023-03-14

我有两个静态web应用程序(create-react-apps)目前在两个独立的S3桶中。这两个bucket都配置为公共读取+静态web托管,访问它们的S3托管URL可以正确显示站点。

Bucket 1 - First App:
   index.html
   static/js/main.js

Bucket 2 - Second App:
   /secondapp/
       index.html
       static/js/main.js

我为此设置了一个CloudFront--默认的Cloudfront源正确地加载FirstApp,这样www.mywebsite.com在默认情况下加载index.html。

对于SecondApp,我已经设置了一个缓存行为,以便路径模式SecondApp/*指向SecondApp bucket URL。

这两个应用程序都被配置为通过react-router-dom使用html5推送状态。

我希望的行为是,访问以下内容将显示正确的站点/桶:

www.mywebsite.com-当前工作

共有1个答案

喻高寒
2023-03-14

在研究了这个问题之后,我能够使用lambda@edge(https://aws.amazon.com/lambda/edge/)解决这个问题

通过部署一个简单的javascript函数将特定路径路由到所需的s3 bucket,我们能够实现类似Nginx的路由设置。该函数位于Cloudfront CDN上的lambda@edge上,这意味着您可以指定何时触发该函数。对我们来说,它是在“原产地请求”上

我的设置如下:

    null

var path = require('path');

exports.handler = (event, context, callback) => {
  // Extract the request from the CloudFront event that is sent to Lambda@Edge
  var request = event.Records[0].cf.request;

  const parsedPath = path.parse(request.uri);

  // If there is no extension present, attempt to rewrite url
  if (parsedPath.ext === '') {
    // Extract the URI from the request
    var olduri = request.uri;

    // Match any '/' that occurs at the end of a URI. Replace it with a default index
    var newuri = olduri.replace(/second-app.*/, 'second-app/index.html');

    // Replace the received URI with the URI that includes the index page
    request.uri = newuri;
  }
  // If an extension was not present, we are trying to load static access, so allow the request to proceed
  // Return to CloudFront
  return callback(null, request);
};
    null
 类似资料:
  • 问题内容: 我想要达到的目标: 我有一个托管mydomain.com的Nginx Web服务器。当有人将我的domain.com键入其客户端时,我希望我的服务器从中提供index.html。他们键入mydomain.com/flaskapp1时,他们应该看到flaskapp1。当他们键入mydomain.com/flaskapp2时,他们应该看到flaskapp2。 我已经成功地使用此处的教程ht

  • 问题内容: 我需要使用SSL和管理缓存在AWS S3上部署React App。什么是必需的步骤,我可能会遇到哪些问题? 问题答案: 为什么这样 它可以非常快,“无服务器”并且非常便宜。通过CloudFront全局端点(边缘位置),应用程序可以非常快速且高可靠性地运行。通过设置另一个源来源,CloudFront可以充当API的反向代理,从而消除跨区域(CORS)问题并加快遥远位置的API调用。可以将

  • 问题内容: 我是Spring的新手,我想知道是否可以在同一应用程序中使用多个事务管理器? 我有两个数据访问层-一个用于两个数据库。我想知道,你如何在一个层使用一个事务管理器,而在另一层使用另一个事务管理器。我不需要在两个数据库之间都执行事务。但是我确实需要分别在每个数据库上执行事务。我创建了一个图像来帮助概述我的问题: 这是我的应用程序上下文配置: 这是使用此配置的示例: 因此,对于帐户存储库,我

  • 我的S3 url是:http://my-bucket-name.S3.ap-east-1.amazonaws.com/assets/local/css/app.css(返回文件) CloudFront链接到S3,其URL为https://id.CloudFront.net/assets/local/css/app.css(这将返回IllegalLocationConstraintException

  • 但是所有的答案和评论对链接帖子的回应也没有太大帮助