Lambda@Edge 从 s3 回退到带有 cloudfront 的自定义源,无需硬编码域
我有带有源组 [s3,自定义] 的 CloudFront 分发,
我希望从 s3 提供内容,但当给定某个标头时 - 回退到自定义源。 我有一个在源请求
上运行的 lambda@edge 函数,并且我已经检查了标头 - 现在我需要将请求重定向到自定义源。
问题:
- 据我了解,Lambd@edge 应该使用后备原点运行两次,如此处所述 https://docs.aws.amazon.com/AmazonCloudFront/latest/ DeveloperGuide/high_availability_origin_failover.html 因此,如果源是 s3 并从 CloudFront 回退到自定义源,我想返回 {status: 404} - 我应该怎么做? event.origin 是否包含一次 s3 和一次自定义?或两者兼而有之?
当我返回 404 时,用户会收到 404 响应,并且不会执行回退。
if (!request.origin.s3) {
callback(null, { status: '404' });
return;
}
- 为了执行回退,我按照此处所述覆盖原点 https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-examples.html#lambda-examples-content-based-custom-origin-request-trigger
request.origin = {
custom: {
domainName: 'www.example.com',
port: 443,
protocol: 'https',
path: '',
sslProtocols: ['TLSv1'],
readTimeout: 10,
keepaliveTimeout: 5,
customHeaders: {},
},
};
我可以获取域名以某种方式来自 CloudFront 分配?
I have CloudFront distribution with origin group [s3, custom]
I want to have the content served from s3, but when given a certain header - fallback to the custom origin.
I have a lambda@edge function that runs on origin request
and I've checked the headers - now I need to redirect the request to the custom origin.
Questions:
- As I understand, the Lambd@edge should run twice with a fallback origin as described here
https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/high_availability_origin_failover.html
So I want to return {status: 404} if the origin is s3 and fallback from CloudFront to the custom origin - how should I do that? does event.origin contain once s3 and once custom? or both?
When I return 404, the user gets a 404 response and no fallback is performed.
if (!request.origin.s3) {
callback(null, { status: '404' });
return;
}
- To perform the fallback, I override the origin as described here
https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-examples.html#lambda-examples-content-based-custom-origin-request-trigger
request.origin = {
custom: {
domainName: 'www.example.com',
port: 443,
protocol: 'https',
path: '',
sslProtocols: ['TLSv1'],
readTimeout: 10,
keepaliveTimeout: 5,
customHeaders: {},
},
};
Can I get the domain name somehow from the CloudFront distribution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论