Django + nginx + gunicorn 无静态文件loaded/served
Django + nginx + gunicorn No static files loaded/served
我正在执行制作我的页面 public 的最后步骤,但是在设置 nginx + gunicorn 之后我加载了页面,但是 none 的 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
中找到并成功加载,但在 [=22] =] 选项卡 css
文件为空。
我还看到,我的 gunicorn-access.log
没有为 css
文件发送 GET
命令,有没有理由在代码运行时不会发生这种情况当我改为 运行 a 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;
}
我正在执行制作我的页面 public 的最后步骤,但是在设置 nginx + gunicorn 之后我加载了页面,但是 none 的 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
中找到并成功加载,但在 [=22] =] 选项卡 css
文件为空。
我还看到,我的 gunicorn-access.log
没有为 css
文件发送 GET
命令,有没有理由在代码运行时不会发生这种情况当我改为 运行 a 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;
}