简体   繁体   中英

Django + nginx + gunicorn No static files loaded/served

I am taking the last steps to making my page public, but after setting up nginx + gunicorn I get the page to load, but none of the css is loading, even though my nginx-log file shows no errors.

Here is my 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
    }
}

and my gunicorn.conf.py, located in same directory as 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     

and my projects settings.py file

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',
        ],
    },
    },
]  

Any help is greatly appreciated. Not sure what other files might be necessary to show so let me know if I should include some other files.

Update After some more looking around I find that my css file is loaded and supplied by nginx , but somehow the html, or gunicorn , is not doing its job. If I look at the contents of the html page using Chrome's "developer tools" I find that the css file is located and loaded successfully in the network part , but in the sources tab the css file is empty.

IWhat I also see is that my gunicorn-access.log is not sending a GET command for the css file, is there a reason for that to not happen when the code works without problems when I instead run a Debug server ?

Looks like you're pointing Nginx to your main project directory rather than the directory holding your static files.

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

should be

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

I found a solution after much trying and found a solution. It appears that I had a small mistake in my html that sometimes generated a GET . But the error causing the css file not to be loaded was that it hard the wrong mime type . This was fixed by creating a file mime.types in my /etc/nginx/ directory with the contents

types {
    text/css            css;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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