【问题标题】:Django + nginx + gunicorn No static files loaded/servedDjango + nginx + gunicorn 没有加载/提供静态文件
【发布时间】:2016-01-02 07:08:06
【问题描述】:

我正在采取最后一步来公开我的页面,但是在设置 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 命令,当我运行@987654337 时代码没有问题时,是否有原因不会发生这种情况@?

【问题讨论】:

  • 静态文件的系统路径是什么?
  • /Users/tobias/Dropbox/Science/PythonWorkspace/NewChemicalizer/chemicalizer/static

标签: css django nginx static gunicorn


【解决方案1】:

看起来您将 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
}

【讨论】:

  • 哦,我很高兴解决方案如此简单。我从路径中排除 /static 的原因是我关注了this tutorial,它说它应该被排除,因为 nginx 添加了这个。但现在读它,我可能只是误读了它。非常感谢您的快速帮助!
  • 这是因为您在教程中使用“静态”而不是“root” - “您可能会问为什么不转发到 '/Users/webapp/Apps/testproject /vis'?(最后没有'/static')因为在配置中使用'root'时,它会在其后附加'/static'部分。所以请注意!你可以通过使用别名而不是root来解决这个问题并将 /static 附加到路径的末尾:"
  • 看来我跳得太快了。显然,css 文件不知何故仍在缓存中,因为今天早上我再次启动服务器时,css 不再提供服务。我尝试了alias 和/static 的两个选项,在末尾添加了root,没有添加/static。没有找到静态文件还有其他可能的原因吗?
  • 您确定您没有更改任何其他内容吗?如果它在某个阶段不工作,它一开始就不应该在您的缓存中。如果添加“autoindex on;”在您的别名行下方,然后浏览到 yoursite.com/static - 您看到了什么?
  • 这是一个非常漂亮的命令。我添加了它并转到 site.com/static 给了我所有静态文件的列表。我尝试将 runserver 作为调试运行,django 可以在其中提供静态文件。我昨天确实在两次尝试之间短暂地尝试过,并想象这就是它如何以某种方式卡在缓冲区或其他东西中。因为如果我运行runserver 而不是通过gunicorn 就可以工作,所以我加载静态文件的方式不会是问题,对吧? {% load staticfiles %} 在 base.html 顶部 &lt;link href="{% static 'css/main.css' %}" rel="stylesheet" type="text/css"&gt; 在 html 头部
【解决方案2】:

经过多次尝试,我找到了解决方案并找到了解决方案。看来我的html 中有一个小错误,有时会生成GET。 但是导致css文件无法加载的错误是很难错误mime type。这已通过在我的 /etc/nginx/ 目录中创建一个文件 mime.types 来解决

types {
    text/css            css;
}

【讨论】:

    猜你喜欢
    • 2015-11-30
    • 2013-06-27
    • 2017-05-31
    • 2021-07-18
    • 2021-08-08
    • 2023-03-05
    • 2021-05-27
    • 1970-01-01
    • 2019-02-13
    相关资源
    最近更新 更多