使用 Gunicorn 在 Google App Engine 灵活环境中配置 nginx

使用 Gunicorn 在 Google App Engine 灵活环境中配置 nginx

我正在开发一个 Django 应用程序,到目前为止我已经将其部署到 App Engine 标准环境,但后来我开始使用 Google Cloud Vision Python 库,并且由于一些限制,我决定切换到灵活环境。

我的代码在本地机器上正常运行python manage.py runserver,但当我将其部署到 App Engine 时,我得到以下信息

<html>
    <head>
        <title>502 Bad Gateway</title>
    </head>
    <body bgcolor="white">
        <center>
            <h1>502 Bad Gateway</h1>
        </center>
        <hr>
        <center>nginx</center>
    </body>
</html>

当我研究该问题时,我发现我需要按照说明更改 nginx 设置这里

要应用的 Nginx 设置

# Tune nginx keepalives to work with the GCP HTTP(S) Load Balancer:
keepalive_timeout 650;
keepalive_requests 10000;

但我无法找到方法来实现这一点。我也是后端开发新手,所以没有太多经验。

这是我的app.yaml文件

runtime: python
api_version: 1
threadsafe: yes
env: flex
entrypoint: gunicorn -b :$PORT app.wsgi

runtime_config:
  python_version: 3

handlers:
  - url: /static
    static_dir: static
  - url: .*
    script: app.wsgi.application

builtins:
  - django_wsgi: on

我尝试进行entrypoint以下设置,但没有效果。

entrypoint: gunicorn --keep-alive 650 -b :$PORT app.wsgi

编辑1:此错误仅在POST请求时发生。

编辑2: 这是请求的错误日志http

{
 insertId: "1c0for6fm6z812"   
 jsonPayload: {
  trace: "$traceId"    
  latencySeconds: "30.529"    
  time: null    
 }
 httpRequest: {
  requestMethod: "POST"    
  requestUrl: "/users/register/"    
  status: 502    
  responseSize: "568"    
  userAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36"    
  remoteIp: "130.211.3.116"    
  referer: "-"    
 }
 resource: {
  type: "gae_app"    
  labels: {
   project_id: "cuz-api"     
   version_id: "20170504t110352"     
   module_id: "default"     
  }
 }
 timestamp: "2017-05-04T08:30:14.560885079Z"   
 labels: {
  compute.googleapis.com/resource_name: "68f73c569efa"    
  compute.googleapis.com/resource_id: "8300604998554181598"    
  compute.googleapis.com/zone: "us-central1-b"    
  appengine.googleapis.com/trace_id: "7835424c5d67f87f56649adf26e71250"    
 }
 logName: "projects/cuz-api/logs/appengine.googleapis.com%2Fnginx.request"   
}

这是请求的错误日志https

{
 insertId: "95tl7qfm6m4zy"   
 jsonPayload: {
  time: null    
  trace: "$traceId"    
  latencySeconds: "30.028"    
 }
 httpRequest: {
  requestMethod: "POST"    
  requestUrl: "/users/register/"    
  status: 502    
  responseSize: "568"    
  userAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36"    
  remoteIp: "130.211.3.237"    
  referer: "-"    
 }
 resource: {
  type: "gae_app"    
  labels: {
   version_id: "20170504t110352"     
   module_id: "default"     
   project_id: "cuz-api"     
  }
 }
 timestamp: "2017-05-04T08:26:38.540802339Z"   
 labels: {
  compute.googleapis.com/zone: "us-central1-b"    
  appengine.googleapis.com/trace_id: "3eab23e9502e250d9f00dd5bc7eb465e"    
  compute.googleapis.com/resource_name: "68f73c569efa"    
  compute.googleapis.com/resource_id: "8300604998554181598"    
 }
 logName: "projects/cuz-api/logs/appengine.googleapis.com%2Fnginx.request"   
}

答案1

我认为您实际上没有必要接触 Nginx。您所依据的链接与 HTTP 负载均衡器有关,而与 App Engine Flexible 无关。

可以找到在 App Engine 上灵活运行的 Django 应用程序的示例这里

答案2

您的应用似乎在本地测试机器上运行不佳。如果运行良好,只需按照在线的“将服务从标准环境迁移到灵活环境”操作,即可轻松将其从标准环境应用迁移到 Flex。指导

答案3

我也遇到了同样的问题。我设法通过增加timeout设置(默认为 30 秒)。

相关内容