2012-04-17 141 views
1

我跟着這個guide在Ubuntu 11.10上部署Django + Nginx。 Django運行良好,但Nginx不提供任何靜態文件。在Ngnix下運行的Django不提供靜態內容

我想指出app.com/static/到本地文件夾/項目/應用/靜態/

我也試着在這個question一切都無濟於事

下面是摘錄從我的settings.py:

STATIC_ROOT = '' 

STATIC_URL = '/static/' 

ADMIN_MEDIA_PREFIX = DOMAIN_URL + '/static/admin/' 

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

而且從nginx.conf

server { 
       listen 80; 
       server_name localhost; 
       # site_media - folder in uri for static files 
       location /static/ { 
         autoindex on; 
         alias /project/app/static; 
         } 
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|p$ 
    access_log off; # po co mi logi obrazków :) 
    expires  30d; 
} 
       location/{ 
         # host and port to fastcgi server 
         fastcgi_pass 127.0.0.1:8080; 
         fastcgi_param PATH_INFO $fastcgi_script_name; 
         fastcgi_param REQUEST_METHOD $request_method; 
         fastcgi_param QUERY_STRING $query_string; 
         fastcgi_param CONTENT_TYPE $content_type; 
         fastcgi_param CONTENT_LENGTH $content_length; 
         fastcgi_pass_header Authorization; 
         fastcgi_intercept_errors off; 
         } 

我在這裏做什麼?

這裏的nginx的日誌

WSGIServer: missing FastCGI param SERVER_PORT required by WSGI! 
WSGIServer: missing FastCGI param SERVER_PROTOCOL required by WSGI!" while read$ 
2012/04/17 13:48:24 [error] 15598#0: *3 open() "/etc/nginx/html/static/css/rese$ 
2012/04/17 13:48:24 [error] 15598#0: *4 open() "/etc/nginx/html/static/images/l$ 
2012/04/17 13:48:24 [error] 15598#0: *5 open() "/etc/nginx/html/static/js/scrip$ 
2012/04/17 13:48:24 [error] 15598#0: *6 open() "/etc/nginx/html/static/js/jquery$ 
2012/04/17 13:48:24 [error] 15598#0: *7 open() "/etc/nginx/html/static/style.cs$ 
2012/04/17 13:48:24 [error] 15598#0: *8 open() "/etc/nginx/html/static/js/jquery$ 
2012/04/17 13:48:24 [error] 15598#0: *5 open() "/etc/nginx/html/static/images/i$ 
2012/04/17 13:48:24 [error] 15598#0: *7 open() "/etc/nginx/html/static/images/i$ 
2012/04/17 13:48:24 [error] 15598#0: *8 open() "/etc/nginx/html/static/images/i$ 
2012/04/17 13:48:24 [error] 15598#0: *6 open() "/etc/nginx/html/static/images/s$ 
2012/04/17 13:48:24 [error] 15598#0: *3 open() "/etc/nginx/html/static/images/s$ 
2012/04/17 13:48:24 [error] 15598#0: *4 open() "/etc/nginx/html/static/images/s$ 
2012/04/17 14:07:33 [error] 16231#0: *1 FastCGI sent in stderr: "WSGIServer: mi$ 
WSGIServer: missing FastCGI param SERVER_PORT required by WSGI! 
WSGIServer: missing FastCGI param SERVER_PROTOCOL required by WSGI!" while read$ 
2012/04/17 14:07:33 [error] 16231#0: *3 open() "/etc/nginx/html/static/css/rese$ 
2012/04/17 14:07:33 [error] 16231#0: *4 open() "/etc/nginx/html/static/style.cs$ 
2012/04/17 14:07:33 [error] 16231#0: *5 open() "/etc/nginx/html/static/js/jquery$ 
2012/04/17 14:07:33 [error] 16231#0: *6 open() "/etc/nginx/html/static/js/scrip$ 
2012/04/17 14:07:33 [error] 16231#0: *7 open() "/etc/nginx/html/static/js/jquery$ 
2012/04/17 14:07:34 [error] 16231#0: *7 open() "/etc/nginx/html/static/images/l$ 
2012/04/17 14:07:34 [error] 16231#0: *6 open() "/etc/nginx/html/static/images/i$ 
2012/04/17 14:07:34 [error] 16231#0: *4 open() "/etc/nginx/html/static/images/i$ 
2012/04/17 14:07:34 [error] 16231#0: *5 open() "/etc/nginx/html/static/images/i$ 
2012/04/17 14:07:34 [error] 16231#0: *3 open() "/etc/nginx/html/static/images/s$ 
2012/04/17 14:07:34 [error] 16231#0: *8 open() "/etc/nginx/html/static/images/s$ 
2012/04/17 14:07:34 [error] 16231#0: *5 open() "/etc/nginx/html/static/images/s$ 
2012/04/17 14:07:35 [error] 16231#0: *5 open() "/etc/nginx/html/static/images/f$ 
2012/04/17 14:10:30 [error] 16231#0: *9 FastCGI sent in stderr: "WSGIServer: mi$ 
WSGIServer: missing FastCGI param SERVER_PORT required by WSGI! 
WSGIServer: missing FastCGI param SERVER_PROTOCOL required by WSGI!" while read$ 
2012/04/17 14:11:57 [error] 16471#0: *1 FastCGI sent in stderr: "WSGIServer: mi$ 
WSGIServer: missing FastCGI param SERVER_PORT required by WSGI! 
WSGIServer: missing FastCGI param SERVER_PROTOCOL required by WSGI!" while read$ 

UPDATE:

解決。我沒有指定根目錄在nginx.conf中。 一旦我做到了,它就全部落實到位了!

捂臉

server_name localhost; 
       root /home/user/projects; 
       # site_media - folder in uri for static files 
       location /static/ { 

         root /static/; 
} 
+0

這不是一個Django的問題。相反,你有一個nginx配置問題。檢查你的nginx日誌文件,看看有什麼問題。如果您無法理解錯誤,請在此處發佈錯誤。 – 2012-04-17 14:07:52

+0

發表於nginx日誌 – 2012-04-17 14:15:39

+0

如果你找到了你的問題的答案,你應該發佈它作爲一個答案,而不是將其添加到您的問題。 – SingleNegationElimination 2012-04-17 14:58:43

回答

1

我沒有指定了nginx.conf中的根目錄 一旦我做到了,它就全部落實到位了! 捂臉

  server_name localhost; 
      root /home/user/projects/; 
      # site_media - folder in uri for static files 
      location /static/ { 

        root /static/;} 
-1

Django是不實際的設計爲靜態文件。他們在他們的文檔中多次提及這一點。只需使用nginx分別提供靜態文件,然後將django指向該位置即可。

+0

是的,我想指出nginx的靜態文件目錄中顯示我的nginx.conf – 2012-04-17 14:02:47

1

它看起來像nginx 服務的文件,只有他們不存在。這是因爲1)你沒有指定一個STATIC_ROOT和2)你顯然沒有運行collectstatic這會給你一個錯誤的影響。

首先,設置:

STATIC_ROOT = '/project/app/static' 

或者,更便於攜帶:

import os.path 

STATIC_ROOT = os.path.join(os.path.dirname(__file__), 'static') 

然後,在生產環境中運行以下命令:

python manage.py collectstatic 
+0

試圖它沒有奏效。請參閱上面的我的更新。 – 2012-04-17 14:57:59