NLPPassionate
NLPPassionate

Reputation: 11

I get a 502 Bad Gateway error with django app on AppEngine

I'm deploying a django app on Google app engine (flexible environment). The app works fine locally and the deployment (using gcloud app deploy) goes well. While the homepage loads well, I get a 502 Bad Gateway nginx error when I load some binary data (about 40Mo) using pickle from a directory in the same app directory (through a POST request). I've tried many proposed solutions (change the PORT to 8080, add the gunicorn timeout or add --preload, change n° of workers..), but still have the problem. I think that the problems comes from the fact that I load a heavy file, since I can access the django admin on the deployed version..

I'm not really knowledgeable in gunicorn/nginx (the first time I deploy an app). I'll be very thankful if you have some ideas after so much time spent on this!

The log file doesn't show any error:

2021-10-30 14:38:46 default[20211030t141946]  [2021-10-30 14:38:46 +0000] [1] [INFO] Starting gunicorn 19.9.0
2021-10-30 14:38:46 default[20211030t141946]  [2021-10-30 14:38:46 +0000] [1] [DEBUG] Arbiter booted
2021-10-30 14:38:46 default[20211030t141946]  [2021-10-30 14:38:46 +0000] [1] [INFO] Listening at: http://0.0.0.0:8080 (1)
2021-10-30 14:38:46 default[20211030t141946]  [2021-10-30 14:38:46 +0000] [1] [INFO] Using worker: sync
2021-10-30 14:38:46 default[20211030t141946]  [2021-10-30 14:38:46 +0000] [10] [INFO] Booting worker with pid: 10
2021-10-30 14:38:46 default[20211030t141946]  [2021-10-30 14:38:46 +0000] [1] [DEBUG] 1 workers
2021-10-30 14:39:04 default[20211030t133157]  "GET /nginx_metrics" 200
2021-10-30 14:39:31 default[20211030t141946]  [2021-10-30 14:39:31 +0000] [10] [DEBUG] GET /
2021-10-30 14:39:47 default[20211030t141946]  "GET /nginx_metrics" 200
2021-10-30 14:40:04 default[20211030t141946]  [2021-10-30 14:40:04 +0000] [10] [DEBUG] POST /
2021-10-30 14:40:04 default[20211030t141946]  POST REQUEST (I click here)
2021-10-30 14:40:20 default[20211030t133157]  [2021-10-30 14:40:20 +0000] [1] [INFO] Handling signal: term
2021-10-30 14:40:20 default[20211030t133157]  [2021-10-30 14:40:20 +0000] [14] [INFO] Worker exiting (pid: 14)
2021-10-30 14:40:21 default[20211030t133157]  [2021-10-30 14:40:21 +0000] [1] [INFO] Shutting down: Master
2021-10-30 14:40:47 default[20211030t141946]  "GET /nginx_metrics" 200

My app.yaml file :

runtime: python
env: flex
env_variables:
  SECRET_KEY: 'DJANGO-SECRET-KEY' 
  DEBUG: 'False' 
  
  DB_HOST: '/cloudsql/django-naimai:europe-west1:naimai-sql'
  DB_PORT: '5432' # PostgreSQL port
  DB_NAME: 'postgres'
  DB_USER: 'postgres' 
  DB_PASSWORD: 'DB_PASSWORD'

entrypoint: gunicorn -b :$PORT --log-level=debug --timeout=120 django_naimai.wsgi

manual_scaling: 
  instances: 1

beta_settings:
    cloud_sql_instances: django-naimai-west1:naimai-sql

runtime_config:
  python_version: 3

resources:
  cpu: 2
  memory_gb: 2.3
  disk_size_gb: 20
  volumes:
  - name: ramdisk1
    volume_type: tmpfs
    size_gb: 2

My settings.py file :

DEBUG = os.environ['DEBUG']
ALLOWED_HOSTS = ["django-naimai.oa.r.appspot.com","127.0.0.1",]
DATABASES = {"default": {
      'ENGINE': 'django.db.backends.postgresql',
      'HOST': os.environ['DB_HOST'],
      'PORT': os.environ['DB_PORT'],
      'NAME': os.environ['DB_NAME'],
      'USER': os.environ['DB_USER'],
      'PASSWORD': os.environ['DB_PASSWORD']
}}

if os.getenv("USE_CLOUD_SQL_AUTH_PROXY", None):
    DATABASES["default"]["HOST"] = "127.0.0.1"
    DATABASES["default"]["PORT"] = 5432

GS_BUCKET_NAME="naimai_bucket"
STATIC_URL = "/static/"
DEFAULT_FILE_STORAGE = "storages.backends.gcloud.GoogleCloudStorage"
STATICFILES_STORAGE = "storages.backends.gcloud.GoogleCloudStorage"
GS_DEFAULT_ACL = "publicRead"

Upvotes: 0

Views: 1105

Answers (1)

NLPPassionate
NLPPassionate

Reputation: 11

As @gaefan suggested, I needed to max out the memory! I tried 10 in memory_gb in the yaml file and it worked.

Upvotes: 1

Related Questions