django - 在 nginx 中获取静态文件失败并显示 404

标签 django ubuntu nginx gunicorn

我现在正在 ubuntu 12 上使用 nginx 和 gunicorn 部署一个 django 应用。

然后我将 nginx 虚拟主机文件配置如下:

server {
    listen 80;
    server_name mydomain.com;
    access_log  /var/log/nginx/gunicorn.log;

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

    location /static/ {
        root /var/www/django/ecerp/erp/static/;
    }

}

我可以很好地请求 django,但是当请求一个静态文件时,它以 404 状态响应。

我确定静态文件的根路径是正确的。

有人能帮忙吗?

最佳答案

您应该使用 alias 而不是 rootroot 将结尾的 URL 部分附加到您的本地路径(例如 http://test.ndd/trailing/part ,它会将/trailing/part 添加到您的本地路径)。取而代之的是,alias 完全符合您的要求:当 http://test.ndd/static/ 时被请求时,/static 会准确映射到您的别名,而无需再次附加 static。

例如:

location /static {
    alias /var/www/django/ecerp/erp/static/;
}

如果文件 /var/www/django/ecerp/erp/static/foo.html 存在然后去 /static/foo.html 将返回它的内容.

关于django - 在 nginx 中获取静态文件失败并显示 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26237936/

相关文章:

ubuntu - 如何在 Ubuntu 12.04 LTS 上安装 Coderunner?

codeigniter - 将 codeigniter 从 3.1.3 升级到 3.1.4 时出错

nginx - 如何在端口 80/443 的公共(public)节点 IP 上公开 kubernetes nginx-ingress 服务?

linux - 尽管尝试了来自 Stackoverflow 的不同修复程序,但 crontab 仍无法正常工作

python - 多对多的嵌套序列化器

django - 如果已经通过 Django url 登录,是否重定向?

python - django 管理员删除登录页面

python - Django 功能性 LiveServerTestCase - 使用 selenium 提交表单后,对象保存到非测试数据库

ubuntu - python pip 默认为用户安装,因为普通站 pip 包不可写

nginx - Asp.Net Core 谷歌认证