【发布时间】: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