Django 在 Google App Engine 上给出“502 Bad Gateway”错误
Django gives "502 Bad Gateway" error on Google App Engine
我正在尝试在标准 Python3.8 环境中的 Google App Engine 上部署 Django 应用程序。我今天遵循了 this document. App runs fine on my local with google cloud database. However I receive 502 Bad Gateway
error visiting Web Url. I found a lot of different questions but technology is changing a lot, and none of them helped so far. Lastly, I came across with this solution 中的所有步骤,但它说创建一个我在 google 文档中从未见过的 gcloud.py
文件,这就是为什么我没有应用该解决方案但决定问你的原因。
访问页面时出现此错误
Traceback (most recent call last):
_find_and_load (<frozen importlib._bootstrap>)
这是错误的堆栈跟踪:
File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked: ModuleNotFoundError: No module named 'main'
at _find_and_load (<frozen importlib._bootstrap>:991)
at _gcd_import (<frozen importlib._bootstrap>:1014)
at import_module (/opt/python3.8/lib/python3.8/importlib/__init__.py:127)
at import_app (/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/util.py:358)
at load_wsgiapp (/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py:39)
at load (/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py:49)
at wsgi (/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/app/base.py:67)
at load_wsgi (/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/workers/base.py:144)
at init_process (/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/workers/base.py:119)
at init_process (/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/workers/gthread.py:92)
at spawn_worker (/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/arbiter.py:583)
my_site/wsgi.py
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_site.settings")
application = get_wsgi_application()
这是我的app.yaml
runtime: python38
handlers:
# This configures Google App Engine to serve the files in the app's
# static directory.
- url: /static
static_dir: static/
# This handler routes all requests not caught above to the main app.
# It is required when static routes are defined, but can be omitted
# (along with the entire handlers section) when there are no static
# files defined.
- url: /.*
script: auto
这是settings.py。我尝试使用注释行,但它仍然给出相同的错误。
STATIC_URL = '/static/'
STATIC_ROOT = 'static'
# STATIC_ROOT = os.path.join(BASE_DIR, '/static/')
# STATICFILES_DIRS = (
# os.path.join(BASE_DIR, 'static'),
# )
# MEDIA_URL = '/media/'
# MEDIA_ROOT = os.path.join(BASE_DIR, "media")
# STATICFILES_FINDERS = [
# 'django.contrib.staticfiles.finders.FileSystemFinder',
# 'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# ]
appengine_config.py
from google.appengine.ext import vendor
vendor.add('lib')
我的 lib 文件夹中有这两个库。我从 env 文件夹中手动移动了它们。
django
MySQLdb
应用程序抱怨找不到 main
模块,您 mentioned 的文档遇到的问题与您的不同,您没有达到使用存储库的程度。
关于您的具体问题。如documentation中所述:
The runtime starts your app by running the command you specify in the
entrypoint field in your app.yaml file.
在这种情况下,您的 app.yaml
没有指定入口点,发生这种情况时,您必须确保满足以下要求:
If your app meets the following requirements, App Engine will start
your app with the gunicorn web server if you don't specify the
entrypoint field:
The root of your app directory contains a main.py file with a
WSGI-compatible object called app.
Your app does not contain Pipfile or Pipfile.lock files.
在这种情况下,您的应用程序缺少 main.py
文件,这是 App Engine 抱怨的错误。有多种方法可以避免此错误(例如指定 entrypoint)。在这种情况下,我的建议是在应用程序的 root 文件夹中创建一个名为 main.py
的文件,并包含下一个:
from mysite.wsgi import application
app = application
可以找到包含 运行 Django 应用程序所需结构的示例 here
我正在尝试在标准 Python3.8 环境中的 Google App Engine 上部署 Django 应用程序。我今天遵循了 this document. App runs fine on my local with google cloud database. However I receive 502 Bad Gateway
error visiting Web Url. I found a lot of different questions but technology is changing a lot, and none of them helped so far. Lastly, I came across with this solution 中的所有步骤,但它说创建一个我在 google 文档中从未见过的 gcloud.py
文件,这就是为什么我没有应用该解决方案但决定问你的原因。
访问页面时出现此错误
Traceback (most recent call last):
_find_and_load (<frozen importlib._bootstrap>)
这是错误的堆栈跟踪:
File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked: ModuleNotFoundError: No module named 'main'
at _find_and_load (<frozen importlib._bootstrap>:991)
at _gcd_import (<frozen importlib._bootstrap>:1014)
at import_module (/opt/python3.8/lib/python3.8/importlib/__init__.py:127)
at import_app (/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/util.py:358)
at load_wsgiapp (/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py:39)
at load (/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py:49)
at wsgi (/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/app/base.py:67)
at load_wsgi (/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/workers/base.py:144)
at init_process (/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/workers/base.py:119)
at init_process (/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/workers/gthread.py:92)
at spawn_worker (/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/arbiter.py:583)
my_site/wsgi.py
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_site.settings")
application = get_wsgi_application()
这是我的app.yaml
runtime: python38
handlers:
# This configures Google App Engine to serve the files in the app's
# static directory.
- url: /static
static_dir: static/
# This handler routes all requests not caught above to the main app.
# It is required when static routes are defined, but can be omitted
# (along with the entire handlers section) when there are no static
# files defined.
- url: /.*
script: auto
这是settings.py。我尝试使用注释行,但它仍然给出相同的错误。
STATIC_URL = '/static/'
STATIC_ROOT = 'static'
# STATIC_ROOT = os.path.join(BASE_DIR, '/static/')
# STATICFILES_DIRS = (
# os.path.join(BASE_DIR, 'static'),
# )
# MEDIA_URL = '/media/'
# MEDIA_ROOT = os.path.join(BASE_DIR, "media")
# STATICFILES_FINDERS = [
# 'django.contrib.staticfiles.finders.FileSystemFinder',
# 'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# ]
appengine_config.py
from google.appengine.ext import vendor
vendor.add('lib')
我的 lib 文件夹中有这两个库。我从 env 文件夹中手动移动了它们。
django
MySQLdb
应用程序抱怨找不到 main
模块,您 mentioned 的文档遇到的问题与您的不同,您没有达到使用存储库的程度。
关于您的具体问题。如documentation中所述:
The runtime starts your app by running the command you specify in the entrypoint field in your app.yaml file.
在这种情况下,您的 app.yaml
没有指定入口点,发生这种情况时,您必须确保满足以下要求:
If your app meets the following requirements, App Engine will start your app with the gunicorn web server if you don't specify the entrypoint field:
The root of your app directory contains a main.py file with a WSGI-compatible object called app.
Your app does not contain Pipfile or Pipfile.lock files.
在这种情况下,您的应用程序缺少 main.py
文件,这是 App Engine 抱怨的错误。有多种方法可以避免此错误(例如指定 entrypoint)。在这种情况下,我的建议是在应用程序的 root 文件夹中创建一个名为 main.py
的文件,并包含下一个:
from mysite.wsgi import application
app = application
可以找到包含 运行 Django 应用程序所需结构的示例 here