Django、gunicorn 和 nginx 代理:静态文件给出 404

Django, gunicorn, and nginx proxy: static files give 404

我 运行 在本地机器上通过 gunicorn 开发了一个 django 项目。出于原因™,我想将 nginx 设置为它的代理。到目前为止,还不错:

location /intranet {
    return 301 /intranet/;
}

location /intranet/ {
    rewrite ^/intranet(.*) / break; 
    proxy_redirect default;
    proxy_pass http://127.0.0.1:8000;
}

这很好。但是,none 的静态文件被切断了:我得到的只是 404。

如何修改上面的nginx配置,让静态内容被切断?

请注意,使用 https::127.0.0.1:8000,静态文件可以正常使用。

在项目中你应该点URL:

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

如果您在每个应用程序中都有一个静态文件夹,您可以使用:

python manage.py collectstatic

这会抓取所有静态文件并将它们放在同一个静态文件夹中 (STATIC_ROOT)

然后你的 Ngnix 也必须知道 statifiles 存储在哪里

server {
    access_log /pathto/log/acces.log;
    error_log  /pathto/log/error.log;
    server_name ******
    charset     utf-8;

    location /static {
        alias /path/to/your/static; <---- This Line
    }

    location /intranet/ {
        rewrite ^/intranet(.*) / break; 
        proxy_redirect default;
        proxy_pass http://127.0.0.1:8000;
    }
}