【问题标题】:Issues serving static files with nginx (Django)使用 nginx (Django) 提供静态文件的问题
【发布时间】:2015-10-08 19:21:08
【问题描述】:

我对此有点陌生,但我正在尝试使用 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 collect static 时,错误都会如下所示:

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'

【问题讨论】:

  • 除了丹尼尔给出的答案之外,我相信你在这里缺少一个斜杠“http://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.3/j”

标签: django nginx static


【解决方案1】:

因为您在路径中使用了“~”。那是一个 shell 的东西,不是一个通用的路径的东西,除非你专门告诉 Python,它不会知道如何处理它。在 Django 设置和 nginx 中使用完整的绝对路径。

【讨论】:

    猜你喜欢
    • 2013-06-27
    • 2021-12-02
    • 2020-09-14
    • 1970-01-01
    • 1970-01-01
    • 2015-12-15
    • 2021-07-18
    • 2019-02-22
    • 2022-11-10
    相关资源
    最近更新 更多