使用 WhiteNoise 在生产模式下将 django 部署到 Elastic Beanstalk

Posted

技术标签:

【中文标题】使用 WhiteNoise 在生产模式下将 django 部署到 Elastic Beanstalk【英文标题】:Deploy django to Elastic Beanstalk in production mode using WhiteNoise 【发布时间】:2021-11-08 22:42:04 【问题描述】:

我正在尝试使用 whitenoise 在 debug=false 条件下部署我的 django 应用程序,但我收到 502 Bad Gateway 错误。我不明白我做错了什么,所以我需要有关此主题的帮助。在日志中,有这个错误说:

/var/log/nginx/error.log

2021/09/13 15:06:13 [错误] 2770#2770: *15675 open() "/var/app/current/static/js/bootstrap.bundle.min.js.map" 失败 (2 :没有这样的文件或目录),客户端:172.31.7.18,服务器:,请求:“GET /static/js/bootstrap.bundle.min.js.map HTTP/1.1”,主机:“www.gamehunterz.com” 2021/09/13 15:06:13 [错误] 2770#2770: *15675 open() "/var/app/current/static/css/bootstrap.min.css.map" 失败(2:没有这样的文件或目录),客户端:172.31.7.18,服务器:,请求:“GET /static/css/bootstrap.min.css.map HTTP/1.1”,主机:“www.gamehunterz.com” 2021/09/13 16:02:23 [错误] 4831#4831: *1 connect() 在连接到上游时失败(111:连接被拒绝),客户端:172.31.37.154,服务器:,请求:“GET /28105/ albedo-and-cast-of-the-seven-godsends HTTP/1.1”,上游:“http://127.0.0.1:8000/28105/albedo-and-cast-of-the-seven-godsends”,主机: “www.gamehunterz.com” 2021/09/13 16:03:22 [错误] 4831#4831: *18 connect() 在连接到上游时失败(111:连接被拒绝),客户端:172.31.7.18,服务器:,请求:“GET / HTTP/ 1.1”,上游:“http://127.0.0.1:8000/”,主机:“www.gamehunterz.com” 2021/09/13 16:03:22 [错误] 4831#4831: *18 connect() 在连接到上游时失败(111:连接被拒绝),客户端:172.31.7.18,服务器:,请求:“GET /favicon. ico HTTP/1.1”,上游:“http://127.0.0.1:8000/favicon.ico”,主机:“www.gamehunterz.com”,引荐来源:“https://www.gamehunterz.com/”

我的 django.config 文件:

option_settings:
  aws:elasticbeanstalk:container:python:
    WSGIPath: newgamehunterz.wsgi:application
  aws:elasticbeanstalk:environment:proxy:staticfiles:
    /static: static

我的设置文件是这样的:

import os
from pathlib import Path
import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration
BASE_DIR = Path(__file__).resolve().parent.parent
sentry_sdk.init(
    dsn="***",
    integrations=[DjangoIntegration()],
traces_sample_rate=1.0,


    



SECRET_KEY = 'secret-key'

DEBUG = False;

ALLOWED_HOSTS = [
    '127.0.0.1', 'localhost',
    'www.gamehunterz.com',
    'gamehunterz.com'
]


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',

    'games',
    'authapp',
    'crispy_forms',
    'django.contrib.sitemaps',
     'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.google',

]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',

    '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 = 'newgamehunterz.urls'


STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'


TEMPLATES = [
    
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['newgamehunterz/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',
                'newgamehunterz.context_processors.neceseeary',
                'newgamehunterz.context_processors.fav_games',

            ],
        ,
    ,
]

WSGI_APPLICATION = 'newgamehunterz.wsgi.application'
X_FRAME_OPTIONS = 'SAMEORIGIN'

AUTH_USER_MODEL = 'authapp.User'

# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases

DATABASES = 
    'default': 
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': '***',
        'USER':'***',
        'PASSWORD':'***',
        'HOST':'***',
        'PORT':'***',

    


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',
    ,
]
SITE_ID = 1

AUTH_USER_MODEL = 'authapp.User'
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.yandex.com.tr'
EMAIL_HOST_USER = '****'
EMAIL_HOST_PASSWORD = '***'
EMAIL_PORT = ***
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = '****'


PASSWORD_RESET_TIMEOUT=360

# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True



SECURE_SSL_REDIRECT = False;

AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend', # existing backend
    'allauth.account.auth_backends.AuthenticationBackend',
)
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
SOCIALACCOUNT_PROVIDERS = 
    'google': 
        'SCOPE': [
            'profile',
            'email',
        ],
        'AUTH_PARAMS': 
            'access_type': 'online',
        
    

PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'static'),
)
#STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'), ]

MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media')
MEDIA_URL = '/media/'

STATICFILES_DIRS = [

    os.path.join(BASE_DIR, "static")
]

LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = '/'

【问题讨论】:

【参考方案1】:

安装 gunicorn 解决了这个问题。

【讨论】:

以上是关于使用 WhiteNoise 在生产模式下将 django 部署到 Elastic Beanstalk的主要内容,如果未能解决你的问题,请参考以下文章

在电子生产模式下将 SQLITE db 文件存储在哪里?

gzip 在带有 Whitenoise 的 Django 中不起作用

django/whitenoise 存储后端导致错误

如何调试使用 whitenoise、gunicorn 和 heroku 服务的 django 静态文件?

仅使用 gunicorn、django 和 whitenoise 我如何提供媒体服务?

如何在 Django 中为 WhiteNoise 5.1.0 设置 wsgi.py 文件?