1

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

1 回答 1

1

看起来没有返回错误页面,因为 CloudFront 获取自定义错误页面时正在执行上述函数。

为确保在从 S3 存储桶请求自定义错误页面时不执行该函数,可以从未配置 Lambda 函数的不同缓存行为提供自定义错误页面。

例如,可以在没有任何 Lambda 的情况下配置具有路径模式/error-pages/*的缓存行为,并且可以为来自源的 404 状态代码的自定义错误页面设置/error-pages/404.html 。

于 2018-08-11T21:54:15.943 回答