更新时间:2021-12-07 06:24:29
After researching this issue, I was able to resolve it using lambda@edge (https://aws.amazon.com/lambda/edge/)
通过部署简单的javascript函数将特定路径路由到所需的s3存储桶,我们可以实现类似nginx的路由设置. 该函数位于我们的Cloudfront CDN上的lambda @ edge上,这意味着您可以指定何时被触发.对我们来说,它是在原产地请求"上
By deploying a simple javascript function to route specific paths to the desired s3 bucket, we are able to achieve an nginx-like routing setup. The function sits on lambda@edge on our Cloudfront CDN, meaning you can specify when it is triggered. For us, it's on "Origin Request"
我的设置如下:
arn:aws:lambda:us-east-1:12345667890:function:my-function-name
)的地址arn:aws:lambda:us-east-1:12345667890:function:my-function-name
)这是我使用的lambda函数的示例.
Here is an example of the lambda function I used.
var path = require('path');
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;
const parsedPath = path.parse(request.uri);
// If there is no extension present, attempt to rewrite url
if (parsedPath.ext === '') {
// 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(/second-app.*/, 'second-app/index.html');
// Replace the received URI with the URI that includes the index page
request.uri = newuri;
}
// If an extension was not present, we are trying to load static access, so allow the request to proceed
// Return to CloudFront
return callback(null, request);
};
这些是我用于此解决方案的资源:
These are the resources I used for this solution: