首页 文章

在S3和Cloudfront根页面上托管的静态站点路由到www.example.com/index.html,而不是www.example.com

提问于
浏览
0

我有一个托管在Amazon S3和Cloudfront上的静态站点 . 目前,当我访问我的网站www.example.com时,我得到了403.只有当我访问www.example.com/index.html时我才真正访问我的网站 . 我希望的行为是,当我访问www.example.com时,我会看到我在访问www.example.com/index.html时看到的内容 .

我已经设置了一个可以称为example.com的存储桶,其中包含我网站的所有信息 . 我还有另一个桶(www.example.com)重定向到example.com .

我的域指向Cloudfront,我在其中设置了Cloudfront域集 . 我认为这就是问题所在 . 我必须从这个域转到/index.html才能真正看到该网站 .

如何进行此设置,以便在访问www.example.com时,我会看到www.example.com/index.html上目前的内容?

我已经将index.html设置为我的存储桶的索引文档 .

3 回答

  • 0

    当您为静态网站托管(使用或不使用CloudFront)设置S3存储桶时,可以使用索引文档配置存储桶 . 当客户端发出目录请求时,索引文档将发送到客户端 .

    例如,您希望在客户端转到 www.example.com 时提供 www.example.com/index.html .

    为此,请将 index.html 设置为存储桶的索引文档 .

    https://docs.aws.amazon.com/AmazonS3/latest/dev/HostingWebsiteOnS3Setup.html

    参见步骤1,子步骤3(b) .

  • 0

    在CloudFront常规配置设置中,确保将默认根对象设置为index.html . 作为最佳实践,您应使用原始访问标识符来确保您的对象仅通过CloudFront提供 .

    还要确保将您的源设置正确设置到S3存储桶和包含您站点的文件夹(如果适用) .

  • 0

    CloudFront的默认根对象仅适用于您的域的根目录 . 因此,对 https://example.com 的请求将从S3提供 /index.html ,但对 https://example.com/about-us 的请求将仅为404(或403,具体取决于权限) .

    如果要从任何目录提供 index.html ,则需要为原始请求部署Lambda @ Edge函数 . 按照this tutorial,你需要的函数体(Node.js 8.10)是:

    exports.handler = (event, context, callback) => {
      const { request }  = event.Records[0].cf;
      const parts        = request.uri.split('/');
      const lastPart     = parts[parts.length - 1];
      const hasExtension = lastPart.includes('.');
    
      if (!hasExtension) {
        const newURI = request.uri.replace(/\/?$/, '/index.html');
        request.uri = newURI;
      }
    
      return callback(null, request);
    };
    

相关问题