我正在使用standard-redirects-for-cloudfront Lambda @ Edge函数来处理
“内部”从/ foo /重定向到/foo/index.html和“外部”
从/foo/index.html重定向到/ foo /。
'use strict';
/*
Copyright 2017 DigitalSailors e.K.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.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);
}
}
服务自定义错误页面时出现问题。
当前,我在S3存储桶中有对象/404.html(也配置为S3静态网站的错误页面,无论如何都不应请求它,因为它应该是Cloudfront的责任)。
在Cloudfront中,我将/404.html设置为404状态的自定义错误页面。
我应该如何设置它以返回错误页面?
marcanuy@scarone:~/Development/website$ curl -I https://example.com/wrong-page
HTTP/2 301
server: CloudFront
location: /wrong-page/
x-cache: Miss from cloudfront
marcanuy@scarone:~/Development/website$ curl -I https://example.com/wrong-page.html
HTTP/2 403
content-type: application/xml
server: AmazonS3
x-cache: Error from cloudfront
marcanuy@scarone:~/Development/website$ curl -I https://example.com/404
HTTP/2 301
server: CloudFront
location: /404/
x-cache: Miss from cloudfront
marcanuy@scarone:~/Development/website$ curl -I https://example.com/404.html
HTTP/2 200
content-type: text/html
last-modified: Fri, 03 Aug 2018 03:38:19 GMT
etag: "4ecbbfd9d1eb384afc897df3f29a8865"
accept-ranges: bytes
server: AmazonS3
x-cache: Miss from cloudfront
最佳答案
似乎未返回错误页面,因为当CloudFront提取自定义错误页面时,正在执行上述功能。
为了确保在从S3存储桶请求自定义错误页面时不执行该功能,可以从未配置Lambda函数的其他缓存行为提供自定义错误页面。
例如,可以配置路径模式为/ error-pages / *的缓存行为,而无需任何Lambda,并且可以针对源位置上的404状态代码为自定义错误页面设置/error-pages/404.html。