502 Bad Gateway Error Deploying Django App to GCP App Engine with Gunicorn

I've successfully deployed my Django application to Google Cloud Platform's App Engine, but when trying to access it via the provided URL, I encounter a "502 Bad Gateway" error served by Nginx. Locally, my application runs fine using Waitress (for testing purposes), and I'm able to access it through a browser without any issues.

Here's a brief overview of my setup:

  • Environment: Python 3.11
  • Framework: Django 4.1
  • WSGI Server for Deployment: Gunicorn
  • Deployment Platform: Google Cloud Platform App Engine
  • Error Message: "502 Bad Gateway nginx"

My app.yaml looks like this:

runtime: python311
service: forecasting-django-app
entrypoint: gunicorn -b :$PORT sales_forecasting.wsgi:application

handlers:
- url: /static/
  static_dir: staticfiles/
- url: /.*
  script: auto

And my project structure:

sales_forecasting/

  • forecasting/ (Django app)
  • sales_forecasting/ (Django project)
  • staticfiles/
  • app.yaml
  • main.py
  • manage.py
  • requirements.txt
  • results.json

Upon reviewing GCP logs, I noticed an error suggesting that a worker was killed, potentially due to running out of memory: [ERROR] Worker (pid:17) was sent SIGKILL! Perhaps out of memory?

What I've tried so far:

  1. Ensuring gunicorn is properly installed and works locally.
  2. Checking that static files are correctly configured and collected.
Back to Top