[英]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.