首页 > 解决方案 > 带有 nginx 静态文件的 Django 无法加载

问题描述

我使用 Debug = True 选项运行 django 服务器。

昨天,我把它改成Debug = False,然后静态文件没有加载。

/etc/nginx/site-enabled/web :

server {
    server_name my_domain;

    location / {
            proxy_pass http://127.0.0.1:8000;
            proxy_set_header X-Real-IP $remote_addr;
    }

    location /static/ {
            alias /home/ubuntu/project/static/;
    }
}

settings.py 静态设置:

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

静态目录路径没有错。我运行服务器python mange.py runserver 0.0.0.0:8000

标签: pythondjangonginx

解决方案


您必须向您的程序宣布静态文件的位置,因此请像这样更改 urls.py 文件:

从 django.conf 导入设置 从 django.conf.urls.static 导入静态

urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

然后运行以下命令

python manage.py collectstatic

推荐阅读