如何在 NodeJs/Angular App Google App Engine 中强制使用 SSL

How to force SSL in NodeJs/Angular App Google App Engine

我想在 Google App Engine 上启动 Web 应用程序。

这是一个带有前端和后端的 NodeJs/Angular 应用程序,我按照本指南将其部署为单独的服务:https://medium.com/@rgoyard/how-to-deploy-a-single-page-application-and-its-backend-to-google-app-engine-353ff93bd38c 使用 client.yml 部署前端,api.yml后端和 dispatch.yml 用于将所有请求路由到 /api 到后端服务。

我还向应用程序添加了一个自定义子域,并让 SSL 证书由 google 管理。

现在唯一要做的就是强制使用 SSL,所以当我访问 http://subdomain.domain.com I automatically get redirected to https://subdomain.domain.de 时。在 google 创建的标准 appspot 域上一切正常。

现在我尝试了很多方法来解决这个问题,我不知道该应用程序是 运行 在标准环境还是 flex 环境中,所以尝试了我找到的所有方法。

文档说对于标准环境,您应该在 app.yml 中使用 secure: always,就像在这个问题 中一样。它没有用,这就是我配置它的方式:

runtime: python27
api_version: 1
threadsafe: true
service: default
handlers:
  - url: /
    static_files: immocheck/index.html
    upload: immocheck/index.html
  - url: /
    static_dir: immocheck
  - url: /.*
    secure: always
    redirect_http_response_code: 301
    script: auto

然后我尝试按照这个答案 配置 nginx-app.conf 但它没有用:

set $test "";

if ($http_x_forwarded_proto = 'http') {
    set $test "http";
}

if ($test = 'http') {
    return 301 https://$host$request_uri;
}

然后我想也许我在一个灵活的环境中,尽管我认为我是在标准环境中。所以我在文档中找到了一些关于使用头盔包的内容,并遵循了这个非常详细的答案来实现它:

不幸的是,这也不起作用,现在非常感谢你的帮助。

提前致谢

您的第一个 / 处理程序匹配,并且永远不会到达 secure: always 指令。尝试:

handlers:
  - url: /
    static_files: immocheck/index.html
    upload: immocheck/index.html
    secure: always

  - url: /  # <-- this url will never get hit, as it already matched above, so try this:

  - url: /(.*\.(html|gif|png|jpg|js|css))$
    static_files: immocheck/
    upload: immocheck/.*\.(html|gif|png|jpg|js|css)$
    secure: always

  - url: /.*
    redirect_http_response_code: 301
    script: auto   
    secure: always

您提到您编写了一个 NodeJS/Angular 应用程序,但从 yaml 文件中我看到该应用程序在 Python 2.7 中。为了定义正确的运行时间,请将您的 runtime 元素更改为:

runtime: nodejs12

Here 您可能会找到有关 yaml 配置文件及其元素的所有必要信息。

您还提到:

I don't know whether the app is running in a standard or flex environment.

当您未定义 env 元素时,应用程序将部署在 App Engine Standard 上。您可以通过导航来确认这一点:

  1. Navigation Menu > App Engine > Version
  2. Checking under the "Environment" tab of the current version.

正如您正确提到的那样,要为您的应用程序强制使用 HTTPS,请在 app.yaml 中为 each 处理程序指定 secure: always 元素。 Reference

要重定向自定义域的所有连接以使用 HTTPS,您必须在响应中设置 SSL certificates and then set the Strict-Transport-Security header。请记住,您可以选择 Google 托管或使用您自己的证书。

我还建议您查看 Node.js Quickstart for App Engine Standard, and taking a look at the How-to-Guides,以便更深入地了解如何设置环境和设计应用程序。

这解决了我的问题:

runtime: python27
api_version: 1
threadsafe: true
service: default

handlers:
  - url: /
    static_files: immocheck/index.html
    upload: immocheck/index.html
    secure: always
    redirect_http_response_code: 301
  - url: /
    secure: always
    redirect_http_response_code: 301
    static_dir: immocheck

谢谢你让我上路@GAEfan。