【问题标题】:How to enforce HTTPS traffic to Google App Engine with custom domain?如何使用自定义域强制对 Google App Engine 的 HTTPS 流量?
【发布时间】:2018-07-22 23:37:53
【问题描述】:

我在 Google Domains (www.example.com) 上有一个网站,它由 Gcloud 托管。我按照此处列出的说明设置 SSL 和 https:https://cloud.google.com/appengine/docs/standard/python/securing-custom-domains-with-ssl

基本上,我只是跑了gcloud beta app domain-mappings update example.com --certificate-management='AUTOMATIC'

现在我确实可以访问https://example.comhttps://www.example.com。但我也可以访问这些域的不安全 http 版本。

如何将我的 Google 域设置为始终使用 https?如果有人键入http://example.com,我希望它转到 https 站点。

记录: 我的裸域(example.com)有 4 条 A 记录和 4 条 AAAA 记录。

我的 www.example.com 域有 1 条别名=www 的 CNAME 记录。

【问题讨论】:

  • 请不要将您的解决方案添加到您的问题中,它应该作为答案发布。
  • 我建议您将问题的标题更改为:How to force HTTPS traffic to Google App Engine with custom domain?

标签: google-app-engine ssl google-domains


【解决方案1】:

您是否尝试在app.yaml 的处理程序中设置secure: always

handlers:
- url: /youraccount/.*
  script: accounts.app
  login: required
  secure: always

总是

请求与此处理程序匹配但不使用的 URL HTTPS 会自动重定向到相同的 HTTPS URL 小路。为重定向保留查询参数

https://cloud.google.com/appengine/docs/standard/python/config/appref#handlers_element

【讨论】:

  • 感谢您的解决方案。似乎它会起作用,但我有 handlers: - url: /.* secure: always 并且在部署时我得到“未知的 url 处理程序类型”。我已经检查并且我的 app.yaml 上的间距是有效的。我的网站是一个用流星 js 制作的静态网站,如果这很重要的话。
  • 我的应用 yaml 顶部也有这个:env: flex runtime: custom threadsafe: true automatic_scaling: max_num_instances: 1
  • 对不起,垃圾邮件 cmets,但事实证明,无论 gcloud 解析 app.yaml,script 部分是必需的,所以我只是将 script: UNUSED 放在该字段中。
  • 如果您有script: UNUSED,您的应用程序如何工作?这指定了哪个文件具有处理这些请求的 web 应用程序。你在 app-engine flex 上吗?如果是这样,我认为handlers 部分被忽略,您必须查看http 标头X-Forwarded-Proto 并手动执行重定向cloud.google.com/appengine/docs/flexible/python/…
  • 感谢您的建议,它让我走上了正确的道路。我做了meteor add gadicohen:headers,然后我的 before 钩子具有以下逻辑:如果我们不在本地主机上,并且 x-forward-proto 是 http,那么只需将当前 url 中的 http 替换为 https 并转到该页面。这有点草率,但它有效。 if (headers.get('x-forwarded-host') !== "localhost:3000") { if (headers.get('x-forwarded-proto') === "http") { window.location = window.location.href.replace('http', 'https') } }
【解决方案2】:

secure: always 在所有标准环境中仍然有效,但在所有灵活环境see documentation herehere for Node.js 中,安全选项已弃用

如果您在当前环境中需要此功能,建议的解决方案需要更改您的应用程序代码。使用自定义 HTTP 标头 X-Forwarded-Proto 将 HTTP 流量重定向到 HTTPS,或使用 HTTP Strict Transport Security response header

【讨论】:

  • 这仅适用于 Python 应用程序吗?我的应用程序是使用 Meteor js 用 javascript 编写的。这是一个静态网站。
  • 它也适用于 Node.js 应用程序。我已经更新了答案。建议的解决方案将是相同的。 (我刚刚阅读了 GAEfan 的答案,根据您的路由和代码,这可能会更容易)。但是,既然您提到它是一个静态 JavaScript 网站,也许 Firebase 托管会更合适?请参阅firebase.google.com/docs/hosting/custom-domain,其中解释了自定义域和 SSL 的组合。
【解决方案3】:

不确定您使用的是哪种后端语言,但您可以通过检查请求标头然后重定向来强制使用 ssl。示例:

if request.environ.get('HTTPS') == 'off':
    return redirect('https://www.example.com' + request.environ.get('PATH_INFO'), 301)

【讨论】:

    【解决方案4】:

    Alex 的回答(参见 cmets)让我走上了正确的道路。

    首先meteor add gadicohen:headers 添加headers 信息。

    在我的路由器逻辑(Meteor 上的 Iron Router)中,我检查了 x-forwarded-proto 是否为 http。如果是这样,请将 http 替换为 https 并转至该 URL。我确保我不在本地主机上,以便我可以开发网站

    Router.onBeforeAction(function () {
        <some other logic>
        // Redirect http to https
        if (headers.get('x-forwarded-host') !== "localhost:3000") {
            if (headers.get('x-forwarded-proto') === "http") {
                window.location = window.location.href.replace('http', 'https')
            }
        }
    });
    

    【讨论】:

      【解决方案5】:

      对于使用默认 Tomcat 的 Java 和 Spring Boot,以下设置适用于 Google App Engine flex:

      server.tomcat.remoteip.remote-ip-header=x-forwarded-for
      server.tomcat.remoteip.protocol-header=x-forwarded-proto
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-07
        • 2012-06-10
        • 2017-04-08
        • 1970-01-01
        • 1970-01-01
        • 2020-10-17
        相关资源
        最近更新 更多