0

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"   
}
  • Does the entrypoint command work to run your app if given PORT=8080 in your dev environment? – Nick Apr 20 '17 at 17:03
  • I'm using manage.py runserver in my local development environment. The error occurs only with the POST requests. Apparently I forgot to mention that, I apologize. I'm editing my question to reflect that information. – Furkanzmc Apr 21 '17 at 12:57
  • Well at this point the natural question to ask, and the natural test to perform, is: "what happens when you run gunicorn --keep-alive 650 -b :$PORT app.wsgi?" – Nick Apr 26 '17 at 22:16
  • I get the same error. – Furkanzmc Apr 27 '17 at 7:41
  • 1
    It's not clear then that what you're seeing is actually related to the link in question then. At any rate the 502's only occur in production. There are other places you can check for logs, such as by SSH'ing into the actual instance itself, and once in there, you can also check inside the container running the app or the container running nginx. – Nick May 4 '17 at 22:12
1

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.

0

It seems that your app does not run well on the local test machine. When it does, it will become easy to migrate it from a standard environment app to flex by following the "Migrating Services from the Standard Environment to the Flexible Environment" on-line guide.

0

I had the same issue. I managed to fix this by increasing the timeout setting (defaults to 30 seconds).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.