[英]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. 我正在采取最后步骤将页面公开,但是在设置nginx + gunicorn之后,我可以加载页面,但是没有加载css,即使我的nginx-log文件未显示任何错误。
Here is my nginx.conf
: 这是我的
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
: 和我的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
and my projects settings.py
file 和我的项目
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',
],
},
},
]
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. 更新经过一番环顾后,我发现我的
css
文件是由nginx
加载和提供的,但是html或gunicorn
却不起作用。 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. 如果我使用
Chrome's "developer tools"
查看html页面的内容,则会发现css文件已成功定位并已在网络part
加载,但是在“ sources
选项卡中css
文件为空。
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
? 我还看到我的
gunicorn-access.log
没有发送css
文件的GET
命令,当我运行Debug server
时代码正常工作时,是否有这种情况不会发生?
Looks like you're pointing Nginx to your main project directory rather than the directory holding your static files. 看起来您要将Nginx指向主项目目录,而不是指向包含静态文件的目录。
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
. 看来我在
html
中有一个小错误,有时会生成GET
。 But the error causing the css file not to be loaded was that it hard the wrong mime type
. 但是导致css文件无法加载的错误是错误的
mime type
很难。 This was fixed by creating a file mime.types
in my /etc/nginx/
directory with the contents 通过在我的
/etc/nginx/
目录中创建包含内容的文件mime.types
可以解决此问题。
types {
text/css css;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.