在Google App Engine上部署Django REST应用程序会产生502错误

时间:2017-05-23 13:01:43

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

我有一个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.

日志中没有错误,所以我可以弄清楚出了什么问题。 在控制台中我得到了

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

1 个答案:

答案 0 :(得分:0)

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

ALLOWED_HOSTS = ['*']