【问题标题】:how to make AWS api gateway accept http instead of https如何使 AWS api 网关接受 http 而不是 https
【发布时间】:2017-08-31 08:39:43
【问题描述】:

我有一个由 API Gateway 代理的 Lambda 函数。但是,API Gateway 只公开 https 而不是 http。我到处搜索,但看起来 API Gateway 无法接受 http。

所以我的问题是如何将 http 客户端调用转换为 https 并发送到 api 网关?我问是因为我的客户只能拨打 http 电话,而且他们不会改变。

【问题讨论】:

  • 除非您想创建自己的 HTTP->HTTPS 或 HTTP->Lambda 代理,否则 API Gateway 不是正确的解决方案。

标签: amazon-web-services aws-lambda aws-api-gateway


【解决方案1】:

您可以通过以下设置将 Cloudfront 分发版放在 API Gateway 前面:

  • 源协议策略:仅限 HTTPS
  • 查看器协议策略:HTTP 和 HTTPS
  • 转发标头:无
  • 对象缓存:使用原始缓存标头

【讨论】:

    【解决方案2】:

    CloudFront 是一种快速解决方案,因为与实例化 Elastic Beanstalk 相比,它的设置要容易得多。

    我花了几个小时试图做到这一点,所以只是分享一些好的文章和一个更温和的提醒:

    1. 这个Smartcam project 使用Amazon API GatewayCloudFront,它详细描述了设置过程。
    2. 亚马逊文档中给出的official example 演示了如何使用查询字符串参数(例如/search?keyword=mars&...)、JSON 有效负载创建 GET 和 POST 方法strong> 并加上 路径参数。必读!
    3. 个人 2 美分:
      • 如果您使用查询字符串参数,请确保编辑行为,然后在查询字符串转发和缓存下选择forward all, cache all。@987654324 @
      • 或者,阅读this doc 了解另外两种可能的配置。
      • 如果您已经将 Origin Path 设置为 stage 变量(默认情况下:/prod),那么当您调用 CloudFront 域时,请跳过 /prod ,简单地说:xxxx.cloudfront.net/resource1/resource2?param1=...&param2=...

    【讨论】:

    • 是的,它有效。仅当您在 api 网关中使用自动为您创建云端分发的自定义域时,您将无法在此自动创建的分发中进行此类自定义更改。
    【解决方案3】:

    我最近与 AWS 代表通了 4 个小时的电话,讨论了我们在生产阶段遇到的类似问题。我的情况类似,我们无法在 APIGateway 中进行任何更改来修复它(代表尝试了各种技巧,但似乎没有任何效果)。所以我们的结论是启动一个 EC2 实例作为 APIGateway 的代理服务器并转发所有流量。有一些额外的工作,例如转移域名,但总的来说它工作得很好。在您的情况下,由于您只需要重定向 HTTP 流量,因此一个简单的 ElasticBeanstalk 代理应用程序可能就足够了(EB 默认使用 HTTP 并且位于 Nginx 代理服务器后面)。

    【讨论】:

      【解决方案4】:

      您可以为 HTTP 到 HTTPS 重定向创建 CloudFront 分配。

      我建议您先为您的域获取 SSL 证书,在ACM (Certificate Manager),区域必须是 us-east-1。

      CloudFront 中,单击Create Distribution,然后选择Web 以创建Web 分发。

      以下是您可以使用的所有设置,以及一些额外的说明。请注意,我在此示例中使用 v0 作为 API 网关阶段。

      # ORIGIN SETTINGS
      # ---
      
      # Origin Domain Name - Paste the domain name of your API Gateway > Stages > v0 but without the path at the end
      Origin Domain Name: https://<getway-id>.execute-api.eu-central-1.amazonaws.com
      
      # Origin Path - The name of your API Gateway stage
      Origin Path: /v0
      
      # Origin ID - The ID for this origin
      # By default it will be defined as `Custom-<getway-id>.execute-api.eu-central-1.amazonaws.com/v0`
      # I replace `Custom` by `v0` just to quickly recognise it in the list later on.
      Origin ID: v0-<getway-id>.execute-api.eu-central-1.amazonaws.com/v0
      
      # Minimum Origin SSL Protocol - Choose the minimum SSL protocol for CloudFront to use when it establishes an HTTPS connection to your origin.
      Minimum Origin SSL Protocol: TLSv1.2
      
      # Origin Protocol Policy - HTTPS since that is all that API Gateway supports. So with HTTPS CloudFront to connects to your origin only over HTTPS.
      Origin Protocol Policy: HTTPS
      
      # DEFAULT CACHE BEHAVIOR SETTINGS
      # ---
      
      # Viewer Protocol Policy - CloudFront allowed protocol to access your web content
      Viewer Protocol Policy: Redirect HTTP to HTTPS
      
      # Allowed HTTP Methods - HTTP methods you want to allow for this cache behavior
      # Select at least GET, HEAD, OPTIONS
      Allowed HTTP Methods: GET, HEAD, OPTIONS, PUT, POST, PATCH, DELETE
      
      # Compress Objects Automatically - No since we are not serving files via CloudFront, we are just returning redirects
      Compress Objects Automatically: No
      
      
      # DISTRIBUTION SETTINGS
      # ---
      
      # Price Class
      # There is no extra charge for using extra locations, so you may leave it on `Use All Edge Locations`. The total request count is aggregated across all locations for the free tier.
      Price Class: Use All Edge Locations
      
      # Alternate Domain Names (CNAMEs) - The domain names of your websites
      Alternate Domain Names (CNAMEs): www.example.com
      
      # SSL Certificate - The `Custom SSL Certificate` MUST be a certificate obtained in us-east-1
      # So, you may go to ACM (certificate manager) and request a certificate there
      # https://console.aws.amazon.com/acm/home?region=us-east-1
      SSL Certificate: Custom SSL Certificate > www.example.com
      
      # Custom SSL Client Support - Leave to default value
      Custom SSL Client Support: Clients that Support Server Name Indication (SNI)
      
      # Security Policy - Leave to default value
      Security Policy: TLSv1.2_2018
      

      部署您的 CloudFront 分配后,转到 Route 53 并在您的托管区域中选择您的域名。

      创建记录集 > A 或选择现有的 A 记录。然后在 Record edition 控制台中:

      Alias 设置为是。然后设置 Alias Target 使其指向您的 CloudFront 分配(如果这是您之前定义的,而不是您的 API 网关)。它会出现在一个下拉列表中,就像www.example.com (&lt;cloudfront-id&gt;.cloudfront.net)

      【讨论】:

        猜你喜欢
        • 2017-06-19
        • 2019-06-06
        • 1970-01-01
        • 1970-01-01
        • 2017-06-01
        • 2019-02-09
        • 2020-06-17
        • 1970-01-01
        • 2012-11-16
        相关资源
        最近更新 更多