【问题标题】:django nginx not serving static filesdjango nginx 不提供静态文件
【发布时间】:2021-12-02 15:52:00
【问题描述】:

我正在使用 nginx 和 gunicorn 部署 django。当我访问该网站时,我收到以下错误。

 open() "/home/x/aroundu/core/static/static/rest_framework/js/default.js" failed

它访问静态文件错误,因为路径应该是这样的

 open() "/home/x/aroundu/core/core/static/rest_framework/js/default.js" failed
server {
    listen 80;

    location /static/ {
        alias /home/x/aroundu/core/core/static/;
    }

    location /media/ {
        alias /home/x/aroundu/core/media/;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')


MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = "/media/"

【问题讨论】:

    标签: django nginx django-rest-framework gunicorn


    【解决方案1】:

    这应该是:

    location /static/ {
        root /home/x/aroundu/core/core/;
    }
    
    location /media/ {
        root /home/x/aroundu/core/;
    }
    

    对于静态你有/core/core/,对于媒体你只有/core/。我不知道哪个是正确的,但考虑到您的设置,它们应该是相同的。

    【讨论】:

    • 我试过了,同样的错误没有改变。也许我以错误的方式重新启动服务器
    • 试试root而不是alias
    猜你喜欢
    • 2020-09-14
    • 1970-01-01
    • 1970-01-01
    • 2016-01-28
    • 2022-10-06
    • 2018-08-26
    • 2017-05-31
    • 1970-01-01
    • 2011-01-27
    相关资源
    最近更新 更多