Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Amazon s3 在一个CloudFront发行版和一个S3 bucket源中托管多个单页应用时,如何配置CloudFront 403重定向_Amazon S3_Single Page Application_Amazon Cloudfront - Fatal编程技术网

Amazon s3 在一个CloudFront发行版和一个S3 bucket源中托管多个单页应用时,如何配置CloudFront 403重定向

Amazon s3 在一个CloudFront发行版和一个S3 bucket源中托管多个单页应用时,如何配置CloudFront 403重定向,amazon-s3,single-page-application,amazon-cloudfront,Amazon S3,Single Page Application,Amazon Cloudfront,我的目标是为Git中的每个分支托管一个版本的网站。我有以下设置,似乎基本上按照预期工作: S3存储桶,每个构建都有一个目录 GitHub操作作业,该作业在文件更改时构建应用程序,并将代码部署到S3 bucket中的相应目录 位于S3存储桶顶部的网站CloudFront发行版。它使用通配符证书和Lambda@Edge查看器请求函数,用于将请求的子域转换为URI路径(->dev.company.com/branch/index.html)。我需要这样做,因为开发人员编写的应用程序内部带有绝对URI的

我的目标是为Git中的每个分支托管一个版本的网站。我有以下设置,似乎基本上按照预期工作:

  • S3存储桶,每个构建都有一个目录
  • GitHub操作作业,该作业在文件更改时构建应用程序,并将代码部署到S3 bucket中的相应目录
  • 位于S3存储桶顶部的网站CloudFront发行版。它使用通配符证书和Lambda@Edge查看器请求函数,用于将请求的子域转换为URI路径(->dev.company.com/branch/index.html)。我需要这样做,因为开发人员编写的应用程序内部带有绝对URI的重定向

  • 通常,对于单页应用程序,我会将自定义403响应设置为直接指向/index.html,因为我在不同的位置有多个index.html页面,我不知道如何将其指向相应的index.html。感觉我需要一个选项来重定向到%SITE\u URL%/index.html

    这是你的幸运日

    这个项目的目的是提供一种运行URI/URL重写功能的机制Lambda@Edge. 在CloudFront后面部署网站的情况下,现在可以在CDN边缘重写URL,避免在后端服务器上浪费CPU周期

    规则格式与mod_rewrite使用的格式类似

    <REGEX> <OPTIONAL REPLACEMENT> [<FLAGS>]
    
    上面的内容应该有助于制作类似示例所示的内容:

    ^/oldpath/(\\d*)/(.*)$ /newpath/$2/$1 [L]
    

    或者,一种更快、更容易、更可读和可维护的方法是自己编写重定向。我根据这篇优秀的文章为您定制了此代码:

    ^/oldpath/(\\d*)/(.*)$ /newpath/$2/$1 [L]
    
      'use strict';
        exports.handler = (event, context, callback) => {
            
            // Get request from CloudFront event  
            var request = event.Records[0].cf.request;
        
            // Extract the URI from the request
            var requestUrl = request.uri;
        
            // Rewrite the Subdomain to a Route to redirect to a different Branch
            var n = requestUrl.indexOf(".");  
            const protocolLen = 7;   //"https://"
            var branch = requestUrl.substring(protocolLen + 1, n - 1);  
            redirectUrl = requestUrl.substring(0, protocolLen) + requestUrl.substring(protocolLen + branch.length + 1) + branch + "/index.html";  
    
            // Replace the received URI with the URI that includes the index page
            request.uri = redirectUrl;
            
            // Return to CloudFront
            return callback(null, request);
        };