用 lambda@edge 重写云端源主机,怎么样?
rewrite cloudfront origin host with lambda@edge, how?
我看到有人在谈论如何根据各种信息重写 URI。但我想规范化所请求的域名。这是我尝试过的:
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
if (request.method.toUpperCase() != 'GET') {
callback(null, request);
return;
}
request.origin = {
custom: {
domainName: 'slashdot.org',
port: 443,
protocol: 'https',
path: request.uri
}
};
request.headers.host = {
"key": "Host",
"value": request.origin.custom.domainName
};
console.log("returning req:", request);
callback(null, request);
}
我希望这会拉起请求,然后云端会针对我的规范化域发出请求。 (对于示例和测试,我使用的是 slashdot,因为很明显它不是 my 内容)。
最终,我试图在不进行重定向的情况下规范化请求,而是在命中到达源之前重写请求。
哦,整洁。我终于想出了如何做到这一点。首先,一些限制:
- 对于 https,这仅适用于您证书下的域。所以我不能使用
mydomain.com
和 slashdot.org
除非我是 CmdrTaco. That's fine, my AWS ACM 证书包括三个域,其中一个是我的实际域。我将在下面称之为 actualdomain.com
。
- 这不能在 查看器请求 上完成,只能在 原始请求 上完成,因为
host
is read-only for the viewer request.
鉴于此,我使用了“Example: Using an Origin-Request Trigger to Change From an Amazon S3 Origin to a Custom Origin”并做了一些小的修改。这是完整的 Lambda 代码。
'use strict';
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
const destDomain = 'actualdomain.com';
/* Set custom origin fields*/
request.origin = {
custom: {
domainName: destDomain,
port: 443,
protocol: 'https',
path: '',
sslProtocols: ['TLSv1', 'TLSv1.1', 'TLSv1.2'],
readTimeout: 5,
keepaliveTimeout: 5,
customHeaders: {}
}
};
request.headers['host'] = [{ key: 'host', value: destDomain}];
callback(null, request);
};
因此,类似于文档中给出的示例,destDomain
是实际域,我的证书中的任何合法域都被代理到该目标,最终用户实际上看不到 actualdomain.com
.
我看到有人在谈论如何根据各种信息重写 URI。但我想规范化所请求的域名。这是我尝试过的:
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
if (request.method.toUpperCase() != 'GET') {
callback(null, request);
return;
}
request.origin = {
custom: {
domainName: 'slashdot.org',
port: 443,
protocol: 'https',
path: request.uri
}
};
request.headers.host = {
"key": "Host",
"value": request.origin.custom.domainName
};
console.log("returning req:", request);
callback(null, request);
}
我希望这会拉起请求,然后云端会针对我的规范化域发出请求。 (对于示例和测试,我使用的是 slashdot,因为很明显它不是 my 内容)。
最终,我试图在不进行重定向的情况下规范化请求,而是在命中到达源之前重写请求。
哦,整洁。我终于想出了如何做到这一点。首先,一些限制:
- 对于 https,这仅适用于您证书下的域。所以我不能使用
mydomain.com
和slashdot.org
除非我是 CmdrTaco. That's fine, my AWS ACM 证书包括三个域,其中一个是我的实际域。我将在下面称之为actualdomain.com
。 - 这不能在 查看器请求 上完成,只能在 原始请求 上完成,因为
host
is read-only for the viewer request.
鉴于此,我使用了“Example: Using an Origin-Request Trigger to Change From an Amazon S3 Origin to a Custom Origin”并做了一些小的修改。这是完整的 Lambda 代码。
'use strict';
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
const destDomain = 'actualdomain.com';
/* Set custom origin fields*/
request.origin = {
custom: {
domainName: destDomain,
port: 443,
protocol: 'https',
path: '',
sslProtocols: ['TLSv1', 'TLSv1.1', 'TLSv1.2'],
readTimeout: 5,
keepaliveTimeout: 5,
customHeaders: {}
}
};
request.headers['host'] = [{ key: 'host', value: destDomain}];
callback(null, request);
};
因此,类似于文档中给出的示例,destDomain
是实际域,我的证书中的任何合法域都被代理到该目标,最终用户实际上看不到 actualdomain.com
.