Configuring nginx on Google App Engine Flexible Environment with Gunicorn
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 videos on Youtube
Furkanzmc
Updated on September 18, 2022Comments
-
Furkanzmc over 1 year
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
fileruntime: 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" }
-
Nick about 7 yearsDoes the entrypoint command work to run your app if given
PORT=8080
in your dev environment? -
Furkanzmc about 7 yearsI'm using
manage.py runserver
in my local development environment. The error occurs only with thePOST
requests. Apparently I forgot to mention that, I apologize. I'm editing my question to reflect that information. -
Nick about 7 yearsWell 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
?" -
Furkanzmc about 7 yearsI get the same error.
-
Nick about 7 yearsWhen running that command in dev, you send a POST request and get the exact 502 html payload shown in your question? And this is no differnt from when you run it using "manage.py"?
-
Furkanzmc about 7 yearsThe 502 error happens when I deploy the project to the Flex environment. I can run and send POST request without any problems on my local machine.
-
Nick about 7 yearsYou had said that running gunicorn in dev produces the same error. Do you not see the error in dev?
-
Nick about 7 yearsHave you inspected all logs to ensure that there's no information on the error there?
-
Nick about 7 yearsFinally, can you try to build and run the docker image on your local machine, to see if the issue is really in production or simply the image itself?
-
Furkanzmc about 7 yearsI'm sorry, I said runnging
python manage.py runserver
doesn't give me the same error on my local machine. But I get the same error If I run thegunicorn
command on my local machine. I checked the logs and the problem got me to this solution (As mentioned in my question). I just don't know how to apply that solution. -
Furkanzmc about 7 yearsI'm sorry. You are right. I added the error logs I got from GAE to my question. I also tried running
gunicorn -b 127.0.0.1:8080 cuz.wsgi
on a macOS, and I did NOT get the same error. It worked the way it's supposed to. -
Nick about 7 yearsIt'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.
-