【问题标题】:How to serve static web content from S3 backed by multiple buckets from different regions如何从来自不同区域的多个存储桶支持的 S3 提供静态 Web 内容
【发布时间】:2019-02-09 15:33:35
【问题描述】:

我正在尝试从 S3 存储桶提供静态 Web 内容(HTML、CSS 和 JS 文件)。我知道我可以转到存储桶的属性选项卡并从Static website hosting 框中选择项目Use this bucket to host a website。而且我确信这一步仍然是我正在寻找的解决方案的一部分,但它不会是全部。

这是我想要完成的:

将相同的内容部署到多个区域并根据可用性和/或延迟向客户端提供服务。

至于 API 网关,我知道该怎么做。我应该在所有区域中创建相同的 API 网关(以及底层 lambda 函数)和自定义域名。然后在 Route 53(CNAME 类型)上创建相同的域并选择延迟作为路由策略。还可以为记录集设置健康检查,以便定期检查 API 网关和 lambda 函数的可用性。

现在我想对 S3 存储桶和我的静态内容执行相同的操作。即我想将相同的内容部署到不同的区域,并以某种方式使 Route 53 将请求路由到 最近的可用 存储桶。以前,我使用的是 CloudFront,但在我看来,在此设置中,我只能引入一个存储桶。

有人知道如何从多个存储桶中提供静态内容吗?如果您要推荐 CandFront,请告诉我您打算如何使用多个存储桶。

【问题讨论】:

  • 不要重新发明轮子,使用 CloudFront

标签: amazon-web-services amazon-s3 amazon-cloudfront


【解决方案1】:

您可以生成a certificate,设置CloudFront distribution 以从您的存储桶中获取内容,然后使用Route53 将您的域指向您的分配。您可以获得免费的 https,还可以添加多个 S3 存储桶作为分配的来源。

来自 AWS 文档:

After you configure CloudFront to deliver your content, here's what happens when users request your objects:

1. A user accesses your website or application and requests one or more objects, such as an image file and an HTML file.

2. DNS routes the request to the CloudFront edge location that can best serve the request—typically the nearest CloudFront edge location in terms of latency—and routes the request to that edge location.

3. In the edge location, CloudFront checks its cache for the requested files. If the files are in the cache, CloudFront returns them to the user. If the files are not in the cache, it does the following:

3a. CloudFront compares the request with the specifications in your distribution and forwards the request for the files to the applicable origin server for the corresponding file type—for example, to your Amazon S3 bucket for image files and to your HTTP server for the HTML files.

3b. The origin servers send the files back to the CloudFront edge location.

3c. As soon as the first byte arrives from the origin, CloudFront begins to forward the files to the user. CloudFront also adds the files to the cache in the edge location for the next time someone requests those files.

P.D.请记住,这仅适用于静态内容!

【讨论】:

  • 谢谢。我刚刚看到创建 CloudFront 实例后可以引入多个Origins。但是,我仍然不确定具有多个起源的含义是什么。这是否意味着如果其中一个出现故障,则将使用另一个?你能指点我一些参考文档吗?
  • CloudFront 没有区域,这里有一些文档:docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/…
  • 你们有什么特殊的地区要求吗?我不会担心 S3/Cloudfront 会宕机。以这种方式托管您的静态内容/网站是可靠的;配置区域端点是优化/其他特殊用例。
【解决方案2】:

这可以通过 CloudFront 使用 Lambda@Edge 根据来自 Route 53 的答案更改来源。

请参阅此博客以获取执行此操作的示例 Lambda@Edge 代码 - https://aws.amazon.com/blogs/apn/using-amazon-cloudfront-with-multi-region-amazon-s3-origins/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-07
    • 2019-05-05
    • 2021-08-11
    • 2021-02-16
    • 2012-04-18
    • 2017-09-25
    • 1970-01-01
    • 2015-11-30
    相关资源
    最近更新 更多