代码之家  ›  专栏  ›  技术社区  ›  marcanuy

如何在使用lambda@edge frunction将网站从cloudfront服务到私有s3 bucket时显示自定义404错误页

  •  0
  • marcanuy  · 技术社区  · 6 年前

    我正在使用 standard-redirects-for-cloudfront lambda@edge函数处理

    “internal”从/foo/重定向到/foo/index.html和“external” 从/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状态的自定义错误页。

    如何设置它以返回错误页?

    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
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Abhishek Shukla    6 年前

    似乎没有返回错误页,因为当cloudfront获取自定义错误页时正在执行上述函数。

    为了确保在从s3 bucket请求自定义错误页时不执行该函数,可以从未配置lambda函数的不同缓存行为提供自定义错误页。

    例如,具有路径模式的缓存行为 /错误页/* 可以在不使用任何lambda和 /错误页/404.html 可以设置404状态代码的自定义错误页。

    推荐文章