我正在使用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