2015-10-10 91 views
1

編輯以SHOW更新的配置靜態文件未顯示在生產

沒有靜態文件在生產中被顯示。文件被正確地顯示在發展

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報告文件已成功複製,但仍然沒有顯示。

部署由面料

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() 
+3

你如何提供這些文件開始?使用'staticfiles'應用程序?它是它的默認配置,它只能在DEBUG模式下工作。您可能需要[從文檔開始](https://docs.djangoproject.com/en/1.8/howto/static-files/)。 –

+1

@Franco不,你沒有。關於你如何提供這些文件,你一點也沒有說。 –

+0

誤讀了評論,現在更新了問題 – Franco

回答

1

首先處理的,在我看來,你的編輯STATICFILES_DIRS指向錯誤的文件夾,因爲你有/static/文件夾,而不是/staticfiles/。應該是因爲它原是:

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

其次,STATIC_ROOT應指向將由網絡服務器中提供靜態文件夾親(但最好不要在項目文件夾)。你的情況:

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所有靜態文件目錄。

+0

謝謝。我完全遵循了這一點,但不幸的是它沒有奏效。同樣的問題。 – Franco

+0

再一次,您的'STATIC_ROOT'指向'/ opt/soundshelter/static /',但是打印'STATIC_ROOT'會導致'/ opt/soundshelter/soundshelter/staticfiles/static'。這可能如何?我建議你設置你的'STATICFILES_DIRS'和'STATIC_ROOT',就像我在答案中顯示的那樣,然後執行'collectstatic'。接下來嘗試通過直接在URL中請求它來打開一些靜態文件(css文件或img)。打印你會在這裏得到什麼。 – chem1st

+0

對不起,這是一個錯字,現在更新以反映正確的輸出。感謝您的評論。 – Franco

0

壞的解決方案 - 請不要在生產中使用

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) 
+1

不!正如文件中所說:「這不適合生產使用!」。它只能在開發過程中使用。 – chem1st

+0

@ chem1st注意到並從urls.py中移除......現在它不再工作:-)問題更新與當前配置。 – Franco

0

在你的Nginx配置你嘗試設置:

location /static { 

而不是

location /static/ { 

也確保運行nginx的用戶具有對靜態文件夾的讀取權限。

+0

剛剛嘗試過,不幸的是它沒有任何區別。進行更改後重新啓動Nginx。還使用'sudo chmod -R 755/opt/soundshelter/static'更新了讀取權限 – Franco

+0

我也試過了這裏的所有變化http://stackoverflow.com/a/1474427/545966但是沒有任何工作。 – Franco

相關問題