2015-06-08 31 views
0

我一直在嘗試約一整天在生產中部署Django的靜態文件,但直到現在我沒有運氣,所以我絕對需要社區的幫助!django生產部署不加載靜態文件

我的nginx的配置是:

worker_processes 1; 

user nobody nogroup; 
pid /tmp/nginx.pid; 
error_log /tmp/nginx.error.log; 

events { 
    worker_connections 1024; 
    accept_mutex off; 
} 

http { 
    include mime.types; 
    default_type application/octet-stream; 
    access_log /tmp/nginx.access.log combined; 
    sendfile on; 

    upstream app_server { 
     server unix:/tmp/gunicorn.sock fail_timeout=0; 
     # For a TCP configuration: 
     # server 192.168.0.7:8000 fail_timeout=0; 
    } 

    server { 
     listen 80 default; 
     client_max_body_size 4G; 
     server_name _; 

     keepalive_timeout 5; 

     # path for static files 
     root /home/ubuntu/src/static; 

     location/{ 
      # checks for static file, if not found proxy to app 
      try_files $uri @proxy_to_app; 
     } 

     location @proxy_to_app { 
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
      proxy_set_header Host $http_host; 
      proxy_redirect off; 

      proxy_pass http://app_server; 
     } 

     error_page 500 502 503 504 /500.html; 
     location = /500.html { 
      root /home/ubuntu/src/static; 
     } 
    } 
} 

,並在我的Django的settings.py文件我已經設置如下:

STATIC_URL = '/static/' 
STATICFILES_DIRS =() 
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder', 
'django.contrib.staticfiles.finders.AppDirectoriesFinder', 
) 
STATIC_ROOT = os.path.join(BASE_DIR, 'static') 

,當然我已經運行collectstatic Django的命令和存在包含所有適當文件的路徑/home/ubuntu/src/static中的文件夾。

不過我的部署不服務器的任何靜態文件:/

沒有任何人有任何想法可能是錯誤與我的設置?

預先感謝您

回答

0

我用了一個偉大的教程here我的設置。希望這可以幫助。一個明顯的變化,我可以看到的是使用location /static/靜態文件,只是轉發location /直gunicorn(或uwsgi在我的情況)

0

添加靜態別名到服務器的conf

location /static { 
      alias /home/ubuntu/src/static; # your Django project's static files - amend as required 
     } 

滿配置應該是這樣

server { 
     listen 80 default; 
     client_max_body_size 4G; 
     server_name _; 

     keepalive_timeout 5; 

     # path for static files 
     # root /home/ubuntu/src/static; 

     location /static { 
      alias /home/ubuntu/src/static; # your Django project's static files - amend as required 
     } 

     location/{ 
      # checks for static file, if not found proxy to app 
      try_files $uri @proxy_to_app; 
     } 



     location @proxy_to_app { 
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
      proxy_set_header Host $http_host; 
      proxy_redirect off; 

      proxy_pass http://app_server; 
     } 

     error_page 500 502 503 504 /500.html; 
     location = /500.html { 
      root /home/ubuntu/src/static; 
     } 
    } 
} 
+0

它仍然沒有服務器的任何靜態文件.. –

+0

你可以請張貼nginx錯誤日誌? – moonstruck

+0

它不吐出任何相關的錯誤。最後一個是'2015/06/08 00:24:50 [emerg] 3209#0:仍然無法綁定()'我之前解決的。 –