我知道有很多类似的问题,但是找不到适合我的情况的
我在单个docker容器中使用了带有gunicorn和nginx的django(2.1.7)(对我们在工作中使用的基础架构的要求),但是在管理菜单上遇到了麻烦。在本地执行并访问http://localhost:8080/admin/
时,页面呈现为不带CSS。
经过一番挖掘,我发现nginx实际上是在提供文件。例如,我可以在浏览器中访问http://localhost:8080/static/admin/css/base.css
并加载相关文件。另外,我的BASEDIR
中的静态目录包含所有预期的文件...
我不知道该如何继续进行调查。所有相关问题似乎在其STATIC_ROOT
配置中存在缺陷,忘记了collectstatic
命令或在nginx配置中误用了alias
。所有这些似乎都对我有用...
我觉得我缺少一些非常基本的东西。你们中有人有想法吗?感谢您的帮助!
在我的配置文件下面:
settings.py
:
"""
Django settings for mysite project.
Generated by 'django-admin startproject' using Django 2.1.7.
For more information on this file, see
https://docs.djangoproject.com/en/2.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.1/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'u8$jv5m9+^pcpcjz$a!uipnq-ufu(kjwfq9ft2)no^-bqv&%$='
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = bool(int(os.environ["DEBUG"]))
ALLOWED_HOSTS = ["*"]
# Application definition
INSTALLED_APPS = [
'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 = 'mysite.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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 = 'mysite.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases
DATABASES = {
"default": {
"ENGINE": os.environ["DB_ENGINE"],
"NAME": os.environ["DB_NAME"],
"USER": os.environ["DB_USER"],
"PASSWORD": os.environ["DB_PASSWORD"],
"HOST": os.environ["DB_HOST"],
"PORT": os.environ["DB_PORT"],
"ATOMIC_REQUESTS": True,
}
}
# Password validation
# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators
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',
},
]
# Internationalization
# https://docs.djangoproject.com/en/2.1/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
gunicorn_config.py
:
bind = '0.0.0.0:5000'
workers = 2
chdir = "./mysite"
reload = True
nginx.conf
:
http {
server {
listen 8080;
location /static/ {
alias /code/mysite/static/;
expires max;
}
location / {
proxy_pass http://127.0.0.1:5000;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
}
}
events {
}
docker命令:(如果需要,还可以发布整个Dockerfile和docker-compose)
python ./mysite/manage.py makemigrations;
python ./mysite/manage.py migrate;
python ./mysite/manage.py collectstatic --noinput;
service nginx start;
gunicorn --config gunicorn_config.py mysite.wsgi:application
答案 0 :(得分:0)
由于nik_m的回答,我设法找到了解决方案。问题在于,css文件已提供但以文本/纯文本发送(您可以在响应标题的Content-Type中看到)。
解决方案是在mime.types
中加入nginx.conf
,类似于显示here
这是我的新nginx.conf:
http {
include /etc/nginx/mime.types;
server {
listen 8080;
location /static/ {
alias /code/mysite/static/;
expires max;
}
location / {
proxy_pass http://127.0.0.1:5000;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
}
}
events {
}
使用nginx文档中的完整mime.types并将其存储在/etc/nginx/mime.types
注意:在使用cmd+shift+R
重新加载页面时,请不要忘记执行硬刷新;)