Nginx – Configuring nginx on Google App Engine Flexible Environment with Gunicorn

google-app-enginegoogle-cloud-platformnginxpython

I'm working on a Django app and I was deploying it to App Engine Standard environment until now, but then I started using Google Cloud Vision Python library and due to some restrictions I ad to switch to Flexible Environment.

My code works correctly on my local machine with python manage.py runserver, but when I deploy it to App Engine I get the following

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

When I researched the problem, I found out that I need to change nginx settings as stated here.

Nginx Settings to Apply

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

But I was unable to find a way to do this. I'm also new to backend development so I don't have much experience.

Here's my app.yaml file

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

I tried setting entrypoint to the following but it didn't work.

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

EDIT 1: This error only occurs with POST requests.

EDIT 2:
Here's the error log with http request:

{
 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"   
}

And this is the error log with https request:

{
 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"   
}

Best Answer

I believe there is no actual need for you to be touching Nginx. The link you are basing yourself on has to do with the HTTP Load Balancer, not with App Engine Flexible.

An example of working Django application on App Engine flexible may be found here.

Related Topic