簡體   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