【问题标题】:Why app engine ignore PORT env variable in app.yaml为什么应用引擎会忽略 app.yaml 中的 PORT 环境变量
【发布时间】:2019-09-02 09:43:07
【问题描述】:

我正在尝试从 Go 1.9 迁移到 Go 1.11。我从the migration document复制了main函数

这是我的 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 上运行。但是,部署后,应用程序没有响应。我检查了日志,它显示“正在侦听端口 8081”。我不明白为什么它需要 8081 而不是 443。这个 8081 来自哪里?

如果我将 main 函数硬编码为使用端口 443。它在日志中给了我这个“应用程序正在侦听端口 443,它应该侦听由 PORT 环境变量定义的端口。因此,nginx 不能已启动。性能可能会下降。请侦听 PORT 环境变量定义的端口。"

我错过了什么?

【问题讨论】:

    标签: google-app-engine go port


    【解决方案1】:

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

    请参阅list of environment variables set by the runtime

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

    【讨论】:

      猜你喜欢
      • 2012-04-05
      • 2016-10-13
      • 2021-10-18
      • 2019-05-11
      • 2012-09-05
      • 1970-01-01
      • 2011-04-13
      • 1970-01-01
      • 2014-07-08
      相关资源
      最近更新 更多