Amazon web services AWS ELB重写路径并更改其间的路径

Amazon web services AWS ELB重写路径并更改其间的路径,amazon-web-services,amazon-elb,Amazon Web Services,Amazon Elb,我在AmazonWebServices(特别是应用程序负载平衡器)上使用ELB(弹性负载平衡器) 问题是: 我想创建一个规则来重写路径,但需要在路径中插入一个字符串 当一个请求传入到: example.org/api/foo/* 我需要将其重定向到: example.org/api/v1/foo/* 请求的其余部分需要保持不变。 问题是路径的原始值无法更改。原因之一是将所有可能的路径添加为规则。但是当扩展api时,它会非常不舒服。正如您已经发现的那样,ELB不支持重写 一些选择: 1) 在ALB

我在AmazonWebServices(特别是应用程序负载平衡器)上使用ELB(弹性负载平衡器)

问题是: 我想创建一个规则来重写路径,但需要在路径中插入一个字符串

当一个请求传入到:

example.org/api/foo/*

我需要将其重定向到:

example.org/api/v1/foo/*

请求的其余部分需要保持不变。
问题是路径的原始值无法更改。原因之一是将所有可能的路径添加为规则。但是当扩展api时,它会非常不舒服。

正如您已经发现的那样,ELB不支持重写

一些选择:

1) 在ALB和您的应用程序之间实现一个Web服务器,例如nginx,它可以实现重写规则


2) 基于路径的重写可以通过使用
Route53->CloudFront->Lambda->ALB
来实现,而不是直接使用
Route53->ALB
,如图所示。

目前一个好的选择是利用
Lambda@Edge
以实现URL重写

Lambda@Edge
是Amazon
CloudFront
的一项功能,它允许您在更接近用户的地方全局运行代码。A
Lambda@Edge
功能在请求发送到负载平衡器之前触发

下面是一个通过
Lambda@Edge
Node.js中的函数

顺便说一句,由于AWS边缘位置和AWS优化的专用网络路径等原因,通常情况下,或
ELB

对于某些用例,前面的
CloudFront
的另一个好处是,通过在
CloudFront
发行版中配置多个来源和多个行为,静态内容(例如HTML、JavaScript、CSS和图像文件等)和动态API可以共享同一个域和SSL证书,有关详细信息,请参阅


因此,使用
Lambda@Edge
对于URL重写,以及
CloudFront带来的附加好处

我有点怀疑您是否真的想“重定向”路径——听起来您想重写路径。重定向告诉客户端在另一个URL重试。@Michael sqlbot说得对。我的意思是路径重写,但我不知道这个词。我查了一下,ELB/ALB目前不支持路径重写。
'use strict';
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;

    // 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(/\/$/, '\/index.html');
    
    // Log the URI as received by CloudFront and the new URI to be used to fetch from origin
    console.log("Old URI: " + olduri);
    console.log("New URI: " + newuri);
    
    // Replace the received URI with the URI that includes the index page
    request.uri = newuri;
    
    // Return to CloudFront
    return callback(null, request);

};