San*_*jay 2 amazon-s3 amazon-web-services amazon-cloudfront url-redirection
是否有一种方法可以使用CloudFront m.foobar.com根据User Agent标题将用户重定向到Web应用程序的移动版本?
我确实使用标题使用用户的设备类型来读取标题缓存CloudFront-Is-Mobile-Viewer。但是,如果我使用自定义来源来提供资产(ELB或EC2实例),则只能将其列入白名单。在这种情况下,我可以编辑服务器配置以处理重定向。
但是,我现在正在使用S3为我的应用程序提供服务,并且希望在CloudFront / S3生态系统中使用一个解决方案。
编辑:
对于S3发行版,我不能访问CloudFront-Is-Mobile-Viewer和其他CF标头。

任何帮助,指针将不胜感激!
背景资料:http : //docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/header-caching.html https://aws.amazon.com/blogs/aws/enhanced-cloudfront-customization/
这是我要解决的方法。
Lambda @ Edge函数
'use strict';
exports.handler = (event, context, callback) => {
/*
* If mobile, redirect to mobile domain
*/
const isMobileHeader = 'CloudFront-Is-Mobile-Viewer'
const request = event.Records[0].cf.request;
const headers = request.headers;
let response = event.Records[0].cf.response;
if (headers[isMobileHeader.toLowerCase()] && headers[isMobileHeader.toLowerCase()] == "true") {
response = {
status: '302',
statusDescription: 'Found',
headers: {
location: [{
key: 'Location',
value: 'http://m.foobar.com',
}],
},
};
callback(null, response);
};
Run Code Online (Sandbox Code Playgroud)
CloudFront发行
Behaviours:
Default:
Cache Based on Selected Request Headers: Whitelist
Whitelist Headers:
- CloudFront-Is-Mobile-Viewer
Lambda Function Associations:
Event Type: Viewer Response
Lambda Function ARN: [ARN of function from Lambda@Edge Function]
Run Code Online (Sandbox Code Playgroud)
进一步阅读
编辑1
正如Sanjay所指出的,结果证明了S3 Origins 仅限于一组用于缓存的标头。
我的建议是,使用S3静态网站托管从S3来源更改为自定义来源,然后将其定位为自定义来源。
S3存储桶配置
S3 Bucket:
Properties:
Static Website Hosting: Use this bucket to host a website
Run Code Online (Sandbox Code Playgroud)
请注意在此页面上给定的端点名称,下一步将需要它。
CloudFront更新
Origins:
Create Origin:
Origin Domain Name: [Endpoint from above]
Origin ID: Custom-S3StaticHosting
Behaviours:
Default:
Origin: Custom-S3StaticHosting
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1646 次 |
| 最近记录: |