Cannot find Django admin static file (404 not found)

Viewed 74

My Django project deployed on EC2 instance. 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

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

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

I already did python manage.py collectstatic command.

When I enter into this project's Admin page, Every UI have loaded without CSS and JS though.

Is somewhere of my setting is wrong? I wonder why django cannot find any static files.

1 Answers

Assuming your BASE_DIR is the same as /usr/share/nginx/html, and the BASE_DIR/myproject/static exists and has content, try

location /static/ {
    root /myproject/;
}

(Using root rather than alias as the end directories are the same, as per nginx docs)

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

Related