简体   繁体   English

找不到 Django 管理静态文件(未找到 404)

[英]Cannot find Django admin static file (404 not found)

My Django project deployed on EC2 instance.我的 Django 项目部署在 EC2 实例上。 Everything is fine except static files.一切都很好,除了静态文件。 Here is my project directory path tree这是我的项目目录路径树

my_project
    └─admin
        ├── admin
        │   ├── admin
        │   ├── keywords
        │   ├── manage.py
        │   └── static
        ├── README.md

And this is my settings.py setting code about staticfiles这是我关于静态文件的settings.py设置代码


# settings.py

debug=False

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
ROOT_DIR = os.path.dirname(BASE_DIR)

STATIC_URL = '/static/'

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

STATICFILES_DIRS = [
    STATIC_ROOT
]

And this is my nginx.conf files这是我的nginx.conf文件

root /usr/share/nginx/html;

location = /favicon.ico { access_log off; log_not_found off; }

location /static {
    alias /static/;
}

location / {
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_pass http://unix:/home/devel/run/gunicorn.sock;
}

error_page 404 /404.html;
    location = /40x.html {
}

error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    }
}

And also my machine's environments:还有我机器的环境:

Centos 7.9 python 3.6.8 Django 4.0.6 Centos 7.9 python 3.6.8 Django 4.0.6

I already did python manage.py collectstatic command.我已经做了python manage.py collectstatic命令。

When I enter into this project's Admin page, Every UI have loaded without CSS and JS though.当我进入这个项目的管理页面时,虽然每个 UI 都没有加载 CSS 和 JS。

Is somewhere of my setting is wrong?是不是我的某个地方设置错了? I wonder why django cannot find any static files.我想知道为什么 django 找不到任何静态文件。

Assuming your BASE_DIR is the same as /usr/share/nginx/html, and the BASE_DIR/myproject/static exists and has content, try假设您的 BASE_DIR 与 /usr/share/nginx/html 相同,并且 BASE_DIR/myproject/static 存在且有内容,请尝试

location /static/ {
    root /myproject/;
}

(Using root rather than alias as the end directories are the same, as per nginx docs ) (根据nginx 文档,使用 root 而不是别名作为最终目录是相同的)

I am assuming your earlier root definition will prefix the location - if not you might have to use root /usr/share/nginx/html/myproject/ explicitly instead我假设您之前的根定义将作为该位置的前缀 - 如果不是,您可能必须明确使用root /usr/share/nginx/html/myproject/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM