【问题标题】:Deploying Django REST app on Google App Engine gives 502 Error在 Google App Engine 上部署 Django REST 应用程序会出现 502 错误
【发布时间】:2017-05-23 13:01:42
【问题描述】:

我有一个 Django REST 应用程序,我设法将它部署到 Google App Engine 柔性环境 (python 3) (https://cloud.google.com/python/django/flexible-environment) 部署顺利,我可以使用 Cloud SQL 代理在本地运行应用程序。但是,当我浏览到提供的 URL 时,我收到 502 错误:

Error: Server Error

The server encountered a temporary error and could not complete your 
request.
Please try again in 30 seconds.

日志中没有错误,所以我可以找出问题所在。 在控制台中我得到 ​​p>

GET https://*.appspot.com/ 502 ()
GET https://*.appspot.com/favicon.ico 502 ()

如果有人遇到过这样的事情,请给我一些指点。

包括 app.yaml:

# [START runtime]
runtime: python
env: flex
entrypoint: gunicorn -b :$PORT myapp_api.wsgi

beta_settings:
    cloud_sql_instances: test-project:europe-west1:test-db

runtime_config:
  python_version: 3
# [END runtime]

包括settings.py:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY =  '...'
DEBUG = True
ALLOWED_HOSTS = []


INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'corsheaders',
    'rest_framework',
    'oauth2_provider',
    'rest_framework_tracking',
    'accounts'
]

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',
    'corsheaders.middleware.CorsMiddleware',
]

ROOT_URLCONF = 'myapp_api.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'accounts/templates')],
        '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'
            ],
        },
    },
]

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'oauth2_provider.ext.rest_framework.OAuth2Authentication',
    ),
}
AUTHENTICATION_BACKENDS = (
   'django.contrib.auth.backends.ModelBackend',
)


WSGI_APPLICATION = 'myapp_api.wsgi.application'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'test',
        'USER': 'foo',
        'PASSWORD': 'password'
    }
}

DATABASES['default']['HOST'] = '/cloudsql/test-project:europe-west1:test-db'
if os.getenv('GAE_INSTANCE'):
    pass
else:
    DATABASES['default']['HOST'] = '127.0.0.1'
    DATABASES['default']['PORT'] = '5432'

AUTH_PASSWORD_VALIDATORS = [ ... ]
PASSWORD_HASHERS = [ ... ]
LOGGING = {
'version': 1, ...
}

STATIC_URL = 'https://storage.googleapis.com/test-bucket/static/'
STATIC_ROOT = 'static/'

FAVICON_PATH = STATIC_URL + 'favicon.ico'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "myapp_api", "img")
]

CORS_ORIGIN_ALLOW_ALL = False
CORS_ORIGIN_WHITELIST = (

)

CORS_ORIGIN_REGEX_WHITELIST = (
    r'^(https?://)?localhost',
    r'^(https?://)?127.',
)

OAUTH2_PROVIDER = {
    'ACCESS_TOKEN_EXPIRE_SECONDS':  3600
}

LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'locale'),
)

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True

【问题讨论】:

  • 发布您的应用 yaml 和 django 设置文件
  • 您的问题解决了吗?我遇到了同样的事情。

标签: google-app-engine django-rest-framework


【解决方案1】:

添加允许的主机列表应该可以解决问题。

ALLOWED_HOSTS = ['*']

【讨论】:

  • 你解决了这个问题吗?
猜你喜欢
  • 2021-10-02
  • 2021-03-31
  • 2017-12-28
  • 2011-07-11
  • 1970-01-01
  • 2020-08-31
  • 2019-07-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多