How to Fix 502 Bad Gateway Errors in Production Flask Apps Running NGINX & Gunicorn

Today I deployed some new code to production for a Flask app that I’m currently building. On my local machine the code which I wrote was running fine without any issues. However, after deploying the code to production I noticed my new endpoint was not working at all and was consistently returning 502 Bad Gateway errors.

A few things I tried tweaking to fix this included updating the config files for NGINX and Gunicorn and explicitly setting timeout durations because the new code that I pushed to production was pretty long running and slow, so I thought maybe something was just timing out. However, after updating my timeout settings in my NGINX and Gunicorn config files to 5 minutes, my code continued to fail in production.

Then I happened to stumble upon a forum post where someone mentioned that this error can sometimes occur if you run out of memory or compute resources. My production server at the time was pretty weak, especially compared to my laptop, running a single CPU with 1 gigabyte of RAM.

The next fix I tried was bumping the resources for my server via DigitalOcean’s server hosting dashboard. And voila! After bumping the resources to 2 CPUs and 4 gigabytes of RAM my code started running as expected, and the 502 bad gateway error went away.

So there you have it. If you have tried bumping the timeout setting for NGINX and Gunicorn and are still running into 502 bad gateway errors, try allocating more CPU and memory resources to your production flask app, and that may fix the issue, especially if the code that is failing is something long running and slow.

UPDATE: I ended up having to bump the resources again, this time to 4 CPUs with 8 gigabytes of RAM.