【问题标题】:Static Files not being displayed in Production静态文件未在生产中显示
【发布时间】:2016-01-08 04:30:56
【问题描述】:

已编辑以显示更新的配置

生产中没有显示静态文件。文件在开发中正确显示

settings.py

BASE_DIR = os.path.dirname(__file__)

STATIC_ROOT = '/opt/soundshelter/static/'

print "Base Dir: " + BASE_DIR #this returns /opt/soundshelter/soundshelter/soundshelter
print "Static Root: " + STATIC_ROOT #this returns /opt/soundshelter/static/


STATIC_URL = '/static/'

STATICFILES_DIRS = (

    os.path.join(BASE_DIR, 'static'),

) #/opt/soundshelter/soundshelter/soundshelter/static

在应用程序中调用文件 <link href="{% static "css/portfolio-item.css" %}" rel="stylesheet">

使用 Nginx 和 Gunicorn。

Nginx 配置:

server {
    server_name 46.101.56.50;

    access_log yes;

    location /static {
        autoindex       on;
        alias /opt/soundshelter/static/;
}

    location / {

        proxy_pass http://127.0.0.1:8001;
        proxy_set_header X-Forwarded-Host $server_name;
        proxy_set_header X-Real-IP $remote_addr;
        add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
    }

#       error_log /opt/soundshelter/soundshelter/soundshelter/nginx-error.log;

}

运行python manage.py collectstatic 报告文件已成功复制,但仍未显示。

由 Fabric 处理的部署

from __future__ import with_statement
from fabric.api import *
from fabric.contrib.console import confirm

env.hosts = []

print "Here we go..."

def commit():
    local("git add . && git commit")

def push():
    local("git push origin master")

def prepare_deploy():

    commit()
    push()

def deploy():
    code_dir = '/opt/soundshelter/soundshelter/'
    with cd(code_dir):
        run("git pull")
        run("python manage.py collectstatic -v0 --noinput")
        run("service nginx restart")
        run("ps aux |grep gunicorn |xargs kill -HUP")
        run("gunicorn -b PROD_IP soundshelter.wsgi:application")

commit()
push()
deploy()

【问题讨论】:

  • 您如何开始提供这些文件?使用staticfiles 应用程序?它是它的默认配置,它只在DEBUG 模式下工作。你可能想start with the docs
  • @Franco 不,你没有。你根本没有说你是如何提供这些文件的。
  • 误读评论,现在更新问题
  • 顺便问一下,为什么您的settings.py 文件位于 dev 的静态文件夹中?它不应该在那里。此外,基于此,您的 BASE_DIR 是 /static/...
  • 在你的 nginx conf 中添加这个,error_log /opt/soundshelter/soundshelter/soundshelter/nginx-error.log;。然后检查错误日志。

标签: python django nginx gunicorn


【解决方案1】:

您是否尝试在 Nginx 配置中设置:

location /static {

而不是

location /static/ {

还要确保运行 nginx 的用户对静态文件夹具有读取权限。

【讨论】:

  • 刚刚尝试过,但不幸的是,它没有任何区别。进行更改后重新启动 Nginx。还使用 'sudo chmod -R 755 /opt/soundshelter/static' 更新了读取权限
  • 我也尝试了stackoverflow.com/a/1474427/545966 的所有变体,但没有任何效果。
【解决方案2】:

糟糕的解决方案 - 不要在生产中使用

https://docs.djangoproject.com/en/1.8/howto/static-files/

将此添加到urls.py 成功了

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

【讨论】:

  • 不!正如文档中所说:“这不适合生产使用!”。它只能在开发期间使用。
  • @chem1st 注意到并从 urls.py 中删除......现在它不再工作了 :-) 问题已更新为当前配置。
【解决方案3】:

首先,在我看来,您编辑的STATICFILES_DIRS 指向错误的文件夹,因为您有/static/ 文件夹,而不是/staticfiles/。应该是原来的样子:

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

其次,STATIC_ROOT 应该指向将由 pro 中的网络服务器提供的静态文件夹(但最好不在项目文件夹中)。在你的情况下:

STATIC_ROOT="/opt/soundshelter/soundshelter/soundshelter/static/"

我通常将静态文件夹放在项目一附近并使用动态STATIC_ROOT而不是硬编码:

BASE_DIR = os.path.abspath(os.path.dirname(__file__))
#this will alow to collect your static files in /opt/soundshelter/soundshelter/staticfiles/static
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'staticfiles/static')

现在您应该执行collectstatic,它将收集STATIC_ROOT 目录中的所有静态文件。

【讨论】:

  • 谢谢。我完全遵循了这一点,但不幸的是它没有用。同样的问题。
  • 再一次,您的STATIC_ROOT 指向/opt/soundshelter/static/,但打印STATIC_ROOT 会导致/opt/soundshelter/soundshelter/staticfiles/static。这怎么可能?我建议您设置STATICFILES_DIRSSTATIC_ROOT,就像我在回答中显示的那样,然后执行collectstatic。接下来尝试通过直接在 url 中请求来打开一些静态文件(css 文件或 img)。打印您将在此处获得的内容。
  • 对不起,这是一个错字,现在更新以反映正确的输出。感谢您的评论。
  • 运行 Collectstatic 按预期复制文件并查看诸如 domain.com/js/ajax.js 之类的文件返回 404
  • “按预期”表示所有文件都收集在/opt/soundshelter/static/?你得到 404 因为你试图在错误的地方打开一个文件,应该是domain.com/static/js/ajax.js
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-27
  • 2021-05-30
  • 2014-08-17
  • 2019-04-13
  • 2012-06-03
  • 2013-09-14
相关资源
最近更新 更多