Django管理不即使nginx的是服务静态文件CSS

问题描述 投票:0回答:1

我知道有吨这样类似的问题,但我找不到任何适合我的情况:

我使用的是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 {

}

搬运工命令:(也可以张贴整个Dockerfile并且如果需要搬运工-组成)

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
python django docker nginx static-files
1个回答
0
投票

下车,由于nik_m的答案,我设法找到解决方案。问题是,该css文件分别担任,但由于发送纯文本/(您可以在响应头的内容类型看)。

解决的办法是包括在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页面时硬刷新;)

© www.soinside.com 2019 - 2024. All rights reserved.