为什么应用引擎忽略 app.yaml 中的 PORT 环境变量

Why app engine ignore PORT env variable in app.yaml

我正在尝试从 Go 1.9 迁移到 Go 1.11。我从 the migration document

复制了主函数

这是我的app.yaml

runtime: go111
env: standard
instance_class: F1
handlers:
  - url: /.*
    script: auto
    secure: always
    redirect_http_response_code: '301'
  - url: .*
    script: auto
env_variables:
  PORT: '443'

这是我的主要功能

func main() {
    http.HandleFunc("/demo", demoHandler)

    port := os.Getenv("PORT")
    if port == "" {
            port = "443"
            syslog.Printf("Defaulting to port %s", port)
    }

    syslog.Printf("Listening on port %s", port)
    syslog.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}

我的应用程序需要 运行 使用 HTTPS 在端口 443 上。但是,部署后,应用程序没有响应。我检查了日志,上面写着 "Listening on port 8081"。我不明白为什么它需要8081而不是443。这个8081来自哪里?

如果我将主要功能硬编码为使用端口 443。它在日志中给出了这个 "App is listening on port 443, it should instead listen on the port defined by the PORT environment variable. As a consequence, nginx cannot be started. Performance may be degraded. Please listen on the port defined by the PORT environment variable."

我在这里想念什么?

PORT 环境变量将由 AppEngine 平台设置,您不必在 app.yaml 配置文件中设置它。事实上,您甚至无法覆盖它。

参见list of environment variables set by the runtime

因此,只需像在 Go 代码中那样使用 PORT env var,但将其从 app.yaml 配置中删除。您只需要启动一个非HTTPS的网络服务器,平台将为您提供HTTPS支持。