CloudFront 中的 AWS Lambda@Edge 502 LambdaValidationError 用于重定向
AWS Lambda@Edge 502 LambdaValidationError in CloudFront for redirect
我有一个 lambda 函数,如果它有使用正则表达式生成的 uri,它会执行 301
重定向。
const redirects = {
xyz: '/contact',
abc: '/about',
};
const arrRegex = [
{ regex: /\xyz/\faq/\max, redirect: redirects.xyz },
{ regex: /\abc/\faq/\min, redirect: redirects.abc },
{ regex: /\lmn\?page=[1-9]/, redirectNotNeeded }
];
function redirect(request) {
let requests = request.uri;
let finalUrl = '';
arrRegex.some(reg => {
const re = new RegExp(reg.regex);
if (requests.match(re) && reg.redirectNotNeeded) {
finalUrl = request;
return true;
} else if (requests.match(re) && reg.redirect) {
finalUrl = reg.redirect;
return true;
} else {
return requests.match(re) && !reg.redirect;
}
});
return finalUrl;
}
exports.handler = (event, context, callback) => {
// find request URL
const request = event.Records[0].cf.request;
request.uri = redirect(request);
let prefixPath; // needed for 2nd condition
if (request.uri.match('.+/$')) {
request.uri += 'index.html';
callback(null, request);
} else if (prefixPath = request.uri.match('(.+)/index.html')) {
const response = {
status: '301',
statusDescription: 'Found',
headers: {
location: [{
key: 'Location', value: prefixPath[1] + '/',
}],
}
};
callback(null, response);
} else if (request.uri.match('/[^/.]+$')) {
const response = {
status: '301',
statusDescription: 'Found',
headers: {
location: [{
key: 'Location', value: request.uri + '/',
}],
}
};
callback(null, response);
} else {
callback(null, request);
}
};
在部署函数并将其添加到云端行为时,当我转到 url https:my-website/xyz/faq/max
时,即使我看到 301
的状态 https:my-website/xyz/faq/max
] 正确重新路由到 https:my-website/contact
后,显示 502 错误。 https:my-website
也显示相同的错误。在 chrome 状态下,我在 Response Header 下看到 LambdaValidationError from cloudfront
。我可以知道我在这里做错了什么来抛出这个错误吗?这些页面只是存储在使用 Amazon CloudFront 提供服务的 AWS S3 存储桶中的静态页面。
将 let finalUrl = '';
更改为 let finalUrl = request.uri;
帮助我摆脱了错误,因为我将其设置为空字符串,lambda 抛出 502 错误,因为它无法 re-direct请求中没有uri时的流量。
const redirects = {
xyz: '/contact',
abc: '/about',
};
const arrRegex = [
{ regex: /\xyz/\faq/\max, redirect: redirects.xyz },
{ regex: /\abc/\faq/\min, redirect: redirects.abc },
{ regex: /\lmn\?page=[1-9]/, redirectNotNeeded }
];
function redirect(request) {
let requests = request.uri;
let finalUrl = request.uri;
arrRegex.some(reg => {
const re = new RegExp(reg.regex);
if (requests.match(re) && reg.redirectNotNeeded) {
finalUrl = request;
return true;
} else if (requests.match(re) && reg.redirect) {
finalUrl = reg.redirect;
return true;
} else {
return requests.match(re) && !reg.redirect;
}
});
return finalUrl;
}
exports.handler = (event, context, callback) => {
// find request URL
const request = event.Records[0].cf.request;
request.uri = redirect(request);
let prefixPath; // needed for 2nd condition
if (request.uri.match('.+/$')) {
request.uri += 'index.html';
callback(null, request);
} else if (prefixPath = request.uri.match('(.+)/index.html')) {
const response = {
status: '301',
statusDescription: 'Found',
headers: {
location: [{
key: 'Location', value: prefixPath[1] + '/',
}],
}
};
callback(null, response);
} else if (request.uri.match('/[^/.]+$')) {
const response = {
status: '301',
statusDescription: 'Found',
headers: {
location: [{
key: 'Location', value: request.uri + '/',
}],
}
};
callback(null, response);
} else {
callback(null, request);
}
};
我有一个 lambda 函数,如果它有使用正则表达式生成的 uri,它会执行 301
重定向。
const redirects = {
xyz: '/contact',
abc: '/about',
};
const arrRegex = [
{ regex: /\xyz/\faq/\max, redirect: redirects.xyz },
{ regex: /\abc/\faq/\min, redirect: redirects.abc },
{ regex: /\lmn\?page=[1-9]/, redirectNotNeeded }
];
function redirect(request) {
let requests = request.uri;
let finalUrl = '';
arrRegex.some(reg => {
const re = new RegExp(reg.regex);
if (requests.match(re) && reg.redirectNotNeeded) {
finalUrl = request;
return true;
} else if (requests.match(re) && reg.redirect) {
finalUrl = reg.redirect;
return true;
} else {
return requests.match(re) && !reg.redirect;
}
});
return finalUrl;
}
exports.handler = (event, context, callback) => {
// find request URL
const request = event.Records[0].cf.request;
request.uri = redirect(request);
let prefixPath; // needed for 2nd condition
if (request.uri.match('.+/$')) {
request.uri += 'index.html';
callback(null, request);
} else if (prefixPath = request.uri.match('(.+)/index.html')) {
const response = {
status: '301',
statusDescription: 'Found',
headers: {
location: [{
key: 'Location', value: prefixPath[1] + '/',
}],
}
};
callback(null, response);
} else if (request.uri.match('/[^/.]+$')) {
const response = {
status: '301',
statusDescription: 'Found',
headers: {
location: [{
key: 'Location', value: request.uri + '/',
}],
}
};
callback(null, response);
} else {
callback(null, request);
}
};
在部署函数并将其添加到云端行为时,当我转到 url https:my-website/xyz/faq/max
时,即使我看到 301
的状态 https:my-website/xyz/faq/max
] 正确重新路由到 https:my-website/contact
后,显示 502 错误。 https:my-website
也显示相同的错误。在 chrome 状态下,我在 Response Header 下看到 LambdaValidationError from cloudfront
。我可以知道我在这里做错了什么来抛出这个错误吗?这些页面只是存储在使用 Amazon CloudFront 提供服务的 AWS S3 存储桶中的静态页面。
将 let finalUrl = '';
更改为 let finalUrl = request.uri;
帮助我摆脱了错误,因为我将其设置为空字符串,lambda 抛出 502 错误,因为它无法 re-direct请求中没有uri时的流量。
const redirects = {
xyz: '/contact',
abc: '/about',
};
const arrRegex = [
{ regex: /\xyz/\faq/\max, redirect: redirects.xyz },
{ regex: /\abc/\faq/\min, redirect: redirects.abc },
{ regex: /\lmn\?page=[1-9]/, redirectNotNeeded }
];
function redirect(request) {
let requests = request.uri;
let finalUrl = request.uri;
arrRegex.some(reg => {
const re = new RegExp(reg.regex);
if (requests.match(re) && reg.redirectNotNeeded) {
finalUrl = request;
return true;
} else if (requests.match(re) && reg.redirect) {
finalUrl = reg.redirect;
return true;
} else {
return requests.match(re) && !reg.redirect;
}
});
return finalUrl;
}
exports.handler = (event, context, callback) => {
// find request URL
const request = event.Records[0].cf.request;
request.uri = redirect(request);
let prefixPath; // needed for 2nd condition
if (request.uri.match('.+/$')) {
request.uri += 'index.html';
callback(null, request);
} else if (prefixPath = request.uri.match('(.+)/index.html')) {
const response = {
status: '301',
statusDescription: 'Found',
headers: {
location: [{
key: 'Location', value: prefixPath[1] + '/',
}],
}
};
callback(null, response);
} else if (request.uri.match('/[^/.]+$')) {
const response = {
status: '301',
statusDescription: 'Found',
headers: {
location: [{
key: 'Location', value: request.uri + '/',
}],
}
};
callback(null, response);
} else {
callback(null, request);
}
};