【问题标题】:500 Server Error in my Django App in App Engine (Google Cloud)App Engine(Google Cloud)中我的 Django 应用程序中出现 500 服务器错误
【发布时间】:2021-06-07 06:01:58
【问题描述】:

我是谷歌云的新手,我按照https://cloud.google.com/python/django/appengine 中的步骤成功部署了应用程序。但是,当我转到页面https://PROJECT_ID.REGION_ID.r.appspot.com 时,会显示下一条消息:

错误:服务器错误 服务器遇到错误,无法完成您的请求。 请在 30 秒后重试。

我看到这确实很常见,但找不到任何有用的解决方案。我将不胜感激。


我的设置.py:

from pathlib import Path
import os
BASE_DIR = Path(__file__).resolve().parent.parent
SECRET_KEY = '…'
DEBUG = True
ALLOWED_HOSTS = ['*']
INSTALLED_APPS = [
    'KhalilApp.apps.KhalilappConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'DjangoServer.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,'KhalilApp/templates'), os.path.join(BASE_DIR,'Mapilib')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'DjangoServer.wsgi.application'

import pymysql  # noqa: 402
pymysql.version_info = (1, 4, 6, 'final', 0)  # change mysqlclient version
pymysql.install_as_MySQLdb()
if os.getenv('GAE_APPLICATION', None):
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'HOST': '/cloudsql/…’,
            'USER': 'maestros',
            'PASSWORD': '…',
            'NAME': 'principal',
        }
    }
else:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'HOST': '127.0.0.1',
            'PORT': '…',
            'NAME': 'principal',
            'USER': 'maestros',
            'PASSWORD': '…',
        }
    }
if os.getenv('TRAMPOLINE_CI', None):
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'db.sqlite3')
        }
    }

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'Europe/Madrid'
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATIC_URL = '/static/'
STATIC_ROOT = 'static'

【问题讨论】:

  • 您的问题没有详细信息,例如失败的 HTTP 请求、应用程序代码、应用程序错误(PHP 日志文件)等。您只会得到提示和猜测。

标签: python django google-app-engine google-cloud-platform server


【解决方案1】:

您的代码中有错误。 500 Server Error 表示您的应用程序有问题。

【讨论】:

  • 您好原子,感谢您的回复。该应用程序在我的本地主机上运行良好,代码中的哪种错误可能会导致这种情况?
  • 生产主机和本地主机的部署设置不同。请根据您的应用查找。在生产环境中设置 DEBUG=TRUE 片刻,你会发现错误
  • 我部署了 2 个版本,一个带有 DEBUG=TRUE,另一个带有 DEBUG=FALSE,但它们都没有工作。但是,在注册表中,我可以找到引用 /favicon.ico 的错误。我不知道那是什么意思。
  • 您是否正确设置了nginx和gunicorn?遵循正确的文档不要错过步骤,与 /favicon.ico 相关的错误意味着找不到文件,这不是 500 服务器错误的原因。
  • 我认为这可能是问题所在,因为在收到 500 错误之前它返回了错误 502 bad gateway nginx。但是我修改了 app.yaml 并且它改变了。我会尝试并遵循正确的步骤。
【解决方案2】:

如果您想在 App Engine Standard 中配置 gunicorn,请将以下行添加到您的 app.yaml 文件中:

entrypoint: gunicorn -b :$PORT main:app

你可以检查下面的Documentation添加一个入口点来启动gunicorn,它监听PORT环境变量指定的端口

【讨论】:

  • 请删除之前的评论,因为它包含您的项目 ID。它被视为 PII 数据,请尽快将其删除。您在评论中附加了过滤器,请分享完整错误的日志。
  • 我已经更新了答案,请看一下。
  • 这是否给你一个提示?开始时间:“2021-03-10T07:50:26.510609Z”结束时间:“2021-03-10T07:50:26.511737Z”延迟时间:“0.001128s”方法:“GET”资源:“/favicon.ico”httpVersion:“ HTTP/1.1" 状态:500
  • 行:[0:{时间:“2021-03-10T07:50:26.511699Z”严重性:“错误”日志消息:“请求失败,因为实例无法成功启动”}] appEngineRelease :“1.9.71”
  • 使用 settings.py,我们无法确定您为什么会收到这些错误。 500 个错误来自您的应用程序。你检查过Django first app 吗?请分享您的 app.yaml 文件。
猜你喜欢
  • 2020-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-25
  • 2021-03-19
  • 2020-03-24
  • 1970-01-01
  • 2021-12-04
相关资源
最近更新 更多