使用nginx(Django)提供静态文件的问题

时间:2015-10-08 19:21:08

标签: django nginx static

我对此有点新意,但我正在尝试使用nginx / gunicorn将我使用Django构建的网站部署到DigitalOcean。

我的nginx文件看起来如此:

server {
  listen 80;
  server_name xxx.xxx.xxx.xx;

  location / {
    proxy_pass http://127.0.0.1:8000;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }

  location /static/ {
    alias ~/dev/WebPortfolio/static/;
  }
}

我的settings.py文件看起来如此:

STATIC_ROOT = '~/dev/WebPortfolio/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = ()

每次运行python managy.py收集静态时,错误都会如此:

You have requested to collect static files at the destination
location as specified in your settings:

    /root/dev/WebPortfolio/~/dev/WebPortfolio/static

查看nginx错误日志,我看到(剪掉重复的东西):

2015/10/08 15:12:42 [error] 23072#0: *19 open() "/usr/share/nginx/~/dev/WebPortfolio/static/http:/cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.3/jquery.e                                                                                                                                                                  asing.min.js" failed (2: No such file or directory), client: xxxxxxxxxxxxxx, server: xxxxxxxxxxxxxx, request: "GET /static/http%3A//cdnjs.cloudflare.com/aj                                                                                                                                                                  ax/libs/jquery-easing/1.3/jquery.easing.min.js HTTP/1.1", host: "XXXXXXXX.com", referrer: "http://XXXXXXXX.com/"

2015/10/08 15:14:28 [error] 23072#0: *24 connect() failed (111: Connection refused) while connecting to upstream, client: xxxxxxxx, server: 104.236.174.46, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8000/", host: "xxxxxxxx.com"

1)我不完全确定为什么静态文件的目的地是'/ root / dev / WebPortfolio /〜/ dev / WebPortfolio / static'

1 个答案:

答案 0 :(得分:2)

因为您已经使用过'〜'在一条路上。这是一个shell的东西,而不是一般的路径,除非你具体告诉Python,它不知道如何处理它。在Django设置和nginx中使用完整的绝对路径。