繁体   English   中英

Django + nginx + gunicorn没有加载/提供静态文件

[英]Django + nginx + gunicorn No static files loaded/served

我正在采取最后步骤将页面公开,但是在设置nginx + gunicorn之后,我可以加载页面,但是没有加载css,即使我的nginx-log文件未显示任何错误。

这是我的nginx.conf

server {
    listen 8000;
    server_name 127.0.0.1;

    access_log /<direct_path>/logs/nginx-access.log;     # <- make sure to create the logs directory
    error_log /<direct_path>/logs/nginx-error.log;       # <- you will need this file for debugging

    location / {
        proxy_pass http://127.0.0.1:9000;         # <- let nginx pass traffic to the gunicorn server
    }

    location /static {
        alias /<project path>/chemicalizer;  # <- let nginx serves the static contents
    }
}

和我的gunicorn.conf.py,与manage.py位于同一目录:

bind = "127.0.0.1:9000"                   
errorlog = '/<direct_path>/logs/gunicorn-error.log' 
accesslog = '/<direct_path>/logs/gunicorn-access.log'
loglevel = 'debug'
workers = 4     

和我的项目settings.py文件

import os, djcelery
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Celery setup
djcelery.setup_loader()
BROKER_URL = 'amqp://guest:guest@localhost:5672'

SECRET_KEY = censored...

DEBUG = False
ALLOWED_HOSTS = ['0.0.0.0', '127.0.0.1', 'localhost']

# Application definition
INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.sites',
    'django.contrib.staticfiles',
    'interface',
    'chemrun',
    'djcelery',
    'kombu.transport.django',
    'chemicalizer.tasks',
    'json',
    'django_nvd3',
    'djangobower',
    'allauth',
    'allauth.account',
    'djangosecure',
)

MIDDLEWARE_CLASSES = (
    'djangosecure.middleware.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)   

ROOT_URLCONF = 'chemicalizer.urls'

TEMPLATES = [
    {
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': ['interface'],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.i18n',
            'django.template.context_processors.media',
            'django.template.context_processors.static',
            'django.template.context_processors.tz',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
        ],
    },
    },
]  

任何帮助是极大的赞赏。 不知道可能需要显示其他文件,所以请告诉我是否应包含其他文件。

更新经过一番环顾后,我发现我的css文件是由nginx加载和提供的,但是html或gunicorn却不起作用。 如果我使用Chrome's "developer tools"查看html页面的内容,则会发现css文件已成功定位并已在网络part加载,但是在“ sources选项卡中css文件为空。

我还看到我的gunicorn-access.log没有发送css文件的GET命令,当我运行Debug server时代码正常工作时,是否有这种情况不会发生?

看起来您要将Nginx指向主项目目录,而不是指向包含静态文件的目录。

location /static {
    alias /<project path>/chemicalizer;  # <- let nginx serves the static contents
}

应该

location /static {
    alias /<project path>/chemicalizer/static;  # <- let nginx serves the static contents
}

经过反复尝试,我找到了解决方案,并找到了解决方案。 看来我在html中有一个小错误,有时会生成GET 但是导致css文件无法加载的错误是错误的mime type很难。 通过在我的/etc/nginx/目录中创建包含内容的文件mime.types可以解决此问题。

types {
    text/css            css;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM