'Error: Bad Gateway 502 when opening Google App Engine Python Domain

When I'm visiting my website (https://osm-messaging-platform.appspot.com), I get this error on the main webpage:

502 Bad Gateway. nginx/1.14.0 (Ubuntu).

It's really weird, since when I run it locally

python app.py

I get no errors, and my app and the website load fine.

I've already tried looking it up, but most of the answers I've found on stack overflow either have no errors or don't relate to me. Here is the error when I look at my GCloud logs:

019-02-07 02:07:05 default[20190206t175104]  Traceback (most recent 
call last):    File "/env/lib/python3.7/site- 
packages/gunicorn/arbiter.py", line 583, in spawn_worker      
worker.init_process()    File "/env/lib/python3.7/site- 
packages/gunicorn/workers/gthread.py", line 104, in init_process      
super(ThreadWorker, self).init_process()    File 
"/env/lib/python3.7/site-packages/gunicorn/workers/base.py", line 
129, in init_process      self.load_wsgi()    File 
"/env/lib/python3.7/site-packages/gunicorn/workers/base.py", line 
138, in load_wsgi      self.wsgi = self.app.wsgi()    File 
"/env/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in 
wsgi      self.callable = self.load()    File 
"/env/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 52, 
in load      return self.load_wsgiapp()    File 
"/env/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 41, 
in load_wsgiapp      return util.import_app(self.app_uri)    File 
"/env/lib/python3.7/site-packages/gunicorn/util.py", line 350, in 
import_app      __import__(module)  ModuleNotFoundError: No module 
named 'main'
2019-02-07 02:07:05 default[20190206t175104]  [2019-02-07 02:07:05 
+0000] [25] [INFO] Worker exiting (pid: 25)
2019-02-07 02:07:05 default[20190206t175104]  [2019-02-07 02:07:05 
+0000] [8] [INFO] Shutting down: Master
2019-02-07 02:07:05 default[20190206t175104]  [2019-02-07 02:07:05 
+0000] [8] [INFO] Reason: Worker failed to boot.

And here are the contents of my app.yaml file:

runtime: python37

handlers:
 # This configures Google App Engine to serve the files in the app's 
static
 # directory.
 - url: /static
 static_dir: static


- url: /.*
  script: auto

I expected it to show my website, but it didn't. Can anyone help?



Solution 1:[1]

The error is produced because the App Engine Standard Python37 runtime handles the requests in the main.py file by default. I guess that you don't have this file and you are handling the requests in the app.py file.

Also the logs traceback is pointing to it: ModuleNotFoundError: No module named 'main'

Change the name the name of the app.py file to main.py and try again.

As a general rule it is recommended to follow this file structure present in the App Engine Standard documention:

  • your-app/
    • app.yaml
    • main.py
    • requirements.txt
    • static/
      • script.js
      • style.css
    • templates/
      • index.html


I believe this would be overkill for your situation but If you need a custom entrypoint read this Python3 runtime documentation to know more about how to configure it.

Solution 2:[2]

My mistake was naming the main app "main" which conflicted with main.py. It worked fine locally as it did not use main.py. I changed it to root and everything worked fine. It took me a whole day to solve it out.

Solution 3:[3]

I resolved the issue in main.py by changing the host from:

app.run(host="127.0.0.1", port=8080, debug=True)

to

app.run(host="0.0.0.0", port=8080, debug=True)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 llompalles
Solution 2 Pol Clota
Solution 3 Freddy Mcloughlan