如何使用 Heroku / Amazon cloudfront / route 53 重定向

Posted

技术标签:

【中文标题】如何使用 Heroku / Amazon cloudfront / route 53 重定向【英文标题】:How to redirect with Heroku / Amazon cloudfront / route 53 【发布时间】:2018-04-01 10:41:33 【问题描述】:

我在 heroku 上托管单页应用程序并使用 amazon cloudfront,路由 53。现在我想在不接触源代码的情况下将一些内部路由重定向到其他路由。

例如

http://example.com/foo -> http://example.com/bar

是否可以使用一些云端或路由 53 配置?

【问题讨论】:

【参考方案1】:

您可以通过多种方式做到这一点。

Lambda@Edge:

您可以为查看者请求创建一个 lambda 边缘函数并执行重定向。

'use strict';
exports.handler = (event, context, callback) => 
    /*
     * Generate HTTP redirect response with 302 status code and Location header.
     */
    const response = 
        status: '302',
        statusDescription: 'Found',
        headers: 
            location: [
                key: 'Location',
                value: 'http://docs.aws.amazon.com/lambda/latest/dg/lambda-edge.html',
            ],
        ,
    ;
    callback(null, response);
;

参考: http://docs.aws.amazon.com/lambda/latest/dg/lambda-edge.html

API-网关:

创建一个 http 代理并执行重定向到所需的 url。 您还需要创建源并将来自云端的行为关联到此 api-gateway 端点。

带有 Lambda 的 API 网关:

通过 ANY 集成将 url 传递给 API-Gateway,然后到达 Lambda,您可以返回相同的响应。

'use strict';

exports.handler = function(event, context, callback) 
    var response = 
        statusCode: 301,
        headers: 
            "Location" : "https://example.com"
        ,
        body: null
    ;
    callback(null, response);
;

希望对你有帮助。

【讨论】:

以上是关于如何使用 Heroku / Amazon cloudfront / route 53 重定向的主要内容,如果未能解决你的问题,请参考以下文章