繁体   English   中英

使用S3存储桶通过单个Cloudfront分发服务多个网站

[英]Use S3 bucket to serve multiple websites using single cloudfront distribution

我有一个S3存储桶,其中包含两个文件夹my-experiencetest-experience ,这两个文件夹均包含一个网站。 我想使用CloudFront分发来从这些文件夹提供网站。 我想实现这一目标:

我想使用index.html的默认根对象创建S3存储桶的分发,然后,当用户转到awd729398dh3d.cloudfront.net/test-experience我想从test-experience文件夹中服务我的网站,当他转到awd729398dh3d.cloudfront.net/my-experience我想从my-experience文件夹提供网站服务。

我做了什么:

我创建了一个S3存储桶,然后在存储桶中创建了文件夹。 根是空的。

我为存储桶创建了一个Cloudfront发行版,并将默认的根对象设置为index.html。

现在,当我转到cloudfront链接时,我什么也没得到。 我也尝试过创造不同的起源。

默认根对象仅在请求 URL时适用,在您的情况下,当请求awd729398dh3d.cloudfront.netawd729398dh3d.cloudfront.net/awd729398dh3d.cloudfront.net/

如果要将awd729398dh3d.cloudfront.net/my-experience重定向到awd729398dh3d.cloudfront.net/my-experience/index.htmlawd729398dh3d.cloudfront.net/test-experience重定向到awd729398dh3d.cloudfront.net/test-experience/index.html您将需要使用原始请求Lambda @ Edge函数重写URL:

exports.handler = (event, context, callback) => {
  const { request } = event.Records[0].cf;

  // Rewrite uri if needed
  if (/\/my-experience\/?$/.test(request.uri)) request.uri = '/my-experience/index.html';
  if (/\/test-experience\/?$/.test(request.uri)) request.uri = '/test-experience/index.html';

  callback(null, request);
};

暂无
暂无

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

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