When I run the Docker container, I can see that Django correctly specifies the paths to the static and media files, but Nginx still can’t find them. This causes a 404 error when trying to access files like bootstrap.min.css.
Django settings:
STATIC_URL = "static/"
STATIC_ROOT = os.path.join(BASE_DIR, "static") # NOQA
# STATICFILES_DIRS = [
# os.path.join(BASE_DIR, "static"), # NOQA F405
# ]
MEDIA_URL = "media/"
MEDIA_ROOT = BASE_DIR / "media/" # NOQA
print(os.path.join(BASE_DIR, 'static')) # I wanted to see what the path would be
print(os.path.join(BASE_DIR, 'media'))
the console displays this path:
/shop/src/static
/shop/src/media
Nginx configuration:
server {
listen 80 default_server;
listen 443 default_server;
server_name shop;
location /static/ {
alias /shop/src/static/;
}
location /media/ {
alias /shop/src/media/;
}
location / {
proxy_set_header Host $host;
proxy_pass http://backend:8010;
}
}
In the Nginx logs, I get:
2024/07/30 07:53:31 [error] 30#30: *1 open() '/shop/src/static/css/bootstrap.min.css' failed (2: No such file or directory), client: 172.18.0.1, server: shop, request: 'GET /static/css/bootstrap.min.css HTTP/1.1', host: 'localhost'
New contributor
Глеб Дубинин is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.