Lambda 边缘来源请求 502
Lambda Edge Origin Request 502
我正在调用 Cloudfront 的原始请求中的 lambda 边缘,出现此错误,我正在尝试将已完成的单页应用程序的元标记更改为 React:
验证错误:Lambda 函数在 header 中返回了一个无效条目 object,header 必须有一个值字段,header: cache-control 有一个或多个条目没有值字段。
这是我的 lambda 边缘函数
const path = require('path');
const https = require('https');
const zlib = require('zlib');
let originalUri;
const downloadContent = (url, callback) => {
https.get(url, (res) => {
let response;
let body = '';
if (res.headers['content-encoding'] === 'gzip') {
response = res.pipe(zlib.createGunzip());
} else {
response = res;
}
response.on('data', (chunk) => {
body += chunk;
});
response.on('end', () => {
callback(true, body, res.headers);
});
}).on('error', (e) => callback(false, e));
};
const fetchMetaData = (url, callback) => {
downloadContent(url, (isOk, result, headers) => {
if (!isOk) {
console.log('Error fetching meta data:', result);
callback(false);
} else {
const metaData = JSON.parse(result);
let metaTags = '';
console.log('metaData whate:', metaData);
if (metaData) {
if (metaData.title) {
metaTags += '<title>' + metaData.title + '</title>';
metaTags += '<meta property=\"og:title\" content=\"' + metaData.title + '\" />';
}
if (metaData.description) {
metaTags += '<meta name=\"description\" content=\"' + metaData.description + '\" />';
metaTags += '<meta property=\"og:description\" content=\"' + metaData.description + '\" />';
}
if (metaData.images) {
for (let i = 0; i < metaData.images.length; i++) {
const image = metaData.images[i];
metaTags += '<meta property=\"og:image\" content=\"' + image + '\" />';
}
}
}
metaTags += '<meta property=\"og:url\" content=\"' + originalUri + '" />';
metaTags += '<meta property=\"og:type\" content=\"website\" />';
callback(true, metaTags, headers);
}
});
};
const fetchIndexHtmlAndCreateCloudFrontResponse = (url, metaTags, metaHeaders, callback) => {
downloadContent(url, (isOk, result, headers) => {
if (!isOk) {
callback(false);
} else {
var finalBody = result.replace(/(<title>title<\/title>)/gi, metaTags);
const responseHeaders = {
'content-type': [{key:'Content-Type', value: 'text/html'}],
'content-encoding' : [{key:'Content-Encoding', value: 'gzip'}],
'accept-ranges': [{key:'Accept-Ranges', value: 'bytes'}]
};
let eTag = '';
if (metaHeaders) {
const metaEtag = metaHeaders['etag'];
if (metaEtag) {
eTag = metaEtag.replace(/"/g, '');
}
}
if (headers) {
const lastModified = headers['last-modified'];
const cacheControl = headers['cache-control'];
const contentETag = headers['etag'];
if (lastModified) {
responseHeaders['last-modified'] = [{key:'Last-Modified', value: lastModified}]
}
if (lastModified) {
responseHeaders['cache-control'] = [{key:'Cache-Control', value: cacheControl}]
}
if (contentETag) {
eTag += contentETag.replace(/"/g, '');;
}
}
if (eTag !== '') {
responseHeaders['etag'] = [{key:'ETag', value: eTag}]
}
const newResponse = {
status: '200',
statusDescription: 'OK',
headers: responseHeaders,
body: finalBody,
};
callback(true, newResponse);
}
});
};
exports.handler = (event, context, callback) => {
const { request, response, config } = event.Records[0].cf;
originalUri = request.uri;
const parsedPath = path.parse(originalUri);
if (parsedPath.ext === '' && parsedPath.dir === '/collections') {
console.log('parsedPath:', parsedPath.base);
request.uri = '/index.html';
let metaUrl = 'https://a2mfja.execute-api.eu-west-1.amazonaws.com/testenv/metatags';
fetchMetaData(metaUrl, (isOk, metaTags, metaHeaders) => {
if (!isOk) {
return callback(null, request); // Return same request so CloudFront can process as usual.
} else {
const contentUrl = 'https://d3lyhnumbmrole.cloudfront.net/index.html';
fetchIndexHtmlAndCreateCloudFrontResponse(contentUrl, metaTags, metaHeaders, (isOk, newResponse) => {
if (!isOk) {
return callback(null, request);
} else {
//newResponse.headers = request.headers;
return callback(null, newResponse);
}
});
}
});
} else {
return callback(null, request);
}
};
错误在这里,cacheControl 在 headers:
中未定义
if (lastModified) {
responseHeaders['cache-control'] = [{key:'Cache-Control', value: cacheControl}]
}
我正在调用 Cloudfront 的原始请求中的 lambda 边缘,出现此错误,我正在尝试将已完成的单页应用程序的元标记更改为 React:
验证错误:Lambda 函数在 header 中返回了一个无效条目 object,header 必须有一个值字段,header: cache-control 有一个或多个条目没有值字段。
这是我的 lambda 边缘函数
const path = require('path');
const https = require('https');
const zlib = require('zlib');
let originalUri;
const downloadContent = (url, callback) => {
https.get(url, (res) => {
let response;
let body = '';
if (res.headers['content-encoding'] === 'gzip') {
response = res.pipe(zlib.createGunzip());
} else {
response = res;
}
response.on('data', (chunk) => {
body += chunk;
});
response.on('end', () => {
callback(true, body, res.headers);
});
}).on('error', (e) => callback(false, e));
};
const fetchMetaData = (url, callback) => {
downloadContent(url, (isOk, result, headers) => {
if (!isOk) {
console.log('Error fetching meta data:', result);
callback(false);
} else {
const metaData = JSON.parse(result);
let metaTags = '';
console.log('metaData whate:', metaData);
if (metaData) {
if (metaData.title) {
metaTags += '<title>' + metaData.title + '</title>';
metaTags += '<meta property=\"og:title\" content=\"' + metaData.title + '\" />';
}
if (metaData.description) {
metaTags += '<meta name=\"description\" content=\"' + metaData.description + '\" />';
metaTags += '<meta property=\"og:description\" content=\"' + metaData.description + '\" />';
}
if (metaData.images) {
for (let i = 0; i < metaData.images.length; i++) {
const image = metaData.images[i];
metaTags += '<meta property=\"og:image\" content=\"' + image + '\" />';
}
}
}
metaTags += '<meta property=\"og:url\" content=\"' + originalUri + '" />';
metaTags += '<meta property=\"og:type\" content=\"website\" />';
callback(true, metaTags, headers);
}
});
};
const fetchIndexHtmlAndCreateCloudFrontResponse = (url, metaTags, metaHeaders, callback) => {
downloadContent(url, (isOk, result, headers) => {
if (!isOk) {
callback(false);
} else {
var finalBody = result.replace(/(<title>title<\/title>)/gi, metaTags);
const responseHeaders = {
'content-type': [{key:'Content-Type', value: 'text/html'}],
'content-encoding' : [{key:'Content-Encoding', value: 'gzip'}],
'accept-ranges': [{key:'Accept-Ranges', value: 'bytes'}]
};
let eTag = '';
if (metaHeaders) {
const metaEtag = metaHeaders['etag'];
if (metaEtag) {
eTag = metaEtag.replace(/"/g, '');
}
}
if (headers) {
const lastModified = headers['last-modified'];
const cacheControl = headers['cache-control'];
const contentETag = headers['etag'];
if (lastModified) {
responseHeaders['last-modified'] = [{key:'Last-Modified', value: lastModified}]
}
if (lastModified) {
responseHeaders['cache-control'] = [{key:'Cache-Control', value: cacheControl}]
}
if (contentETag) {
eTag += contentETag.replace(/"/g, '');;
}
}
if (eTag !== '') {
responseHeaders['etag'] = [{key:'ETag', value: eTag}]
}
const newResponse = {
status: '200',
statusDescription: 'OK',
headers: responseHeaders,
body: finalBody,
};
callback(true, newResponse);
}
});
};
exports.handler = (event, context, callback) => {
const { request, response, config } = event.Records[0].cf;
originalUri = request.uri;
const parsedPath = path.parse(originalUri);
if (parsedPath.ext === '' && parsedPath.dir === '/collections') {
console.log('parsedPath:', parsedPath.base);
request.uri = '/index.html';
let metaUrl = 'https://a2mfja.execute-api.eu-west-1.amazonaws.com/testenv/metatags';
fetchMetaData(metaUrl, (isOk, metaTags, metaHeaders) => {
if (!isOk) {
return callback(null, request); // Return same request so CloudFront can process as usual.
} else {
const contentUrl = 'https://d3lyhnumbmrole.cloudfront.net/index.html';
fetchIndexHtmlAndCreateCloudFrontResponse(contentUrl, metaTags, metaHeaders, (isOk, newResponse) => {
if (!isOk) {
return callback(null, request);
} else {
//newResponse.headers = request.headers;
return callback(null, newResponse);
}
});
}
});
} else {
return callback(null, request);
}
};
错误在这里,cacheControl 在 headers:
中未定义 if (lastModified) {
responseHeaders['cache-control'] = [{key:'Cache-Control', value: cacheControl}]
}