Google App Engine 上的 HTTPS - YAML 文件中的重定向不起作用

HTTPS on Google App Engine - redirect in YAML file does not work

我在 Google App Engine(灵活)上有一个 Python 3 应用程序 运行,没有自定义域。当应用程序从路由“/”路由到“/form”时,它会将协议从 https 更改为 http。下面是我的应用程序,yaml 文件:

runtime: python
env: flex
entrypoint: gunicorn -t 300 -b :$PORT main:app

runtime_config:
  python_version: 3

manual_scaling:
  instances: 1
resources:
  cpu: 1
  memory_gb: 3.5
  disk_size_gb: 10
  
liveness_check:
  check_interval_sec: 300
  timeout_sec: 200
  failure_threshold: 2
  success_threshold: 2

handlers:
- url: /.*
  secure: always
  redirect_http_response_code: 301
  script: auto
  
- url: /js
  static_dir: static/js

我想使用 https 为所有页面/路由提供服务。我做错了什么?

非常感谢您的帮助。

A​​pp Engine Flex 环境不支持 app.yaml 中的处理程序元素,虽然它没有给您任何错误,但它只是被忽略了。正如你在官方documentation, in the yaml reference there's no mention for the handlers block for Flexible. Opposed to that, in the Standard环境中看到的那样,可以指定处理程序环境并且不会被忽略。

因此,为了服务所有来自 HTTPS 而不是 HTTP 的请求,您必须从代码本身强制执行该行为。为此,您需要在回复中包含 Strict-Transport-Security header。文档也涵盖了这个主题,你可以查看here

在我的代码中添加以下行解决了我的问题:

DEFAULT_REFERRER_POLICY = 'strict-origin-when-cross-origin'