Can't serve static files Django + Gunicorn + Nginx + Docker
I have the following structure on my project:
-> api_solar_django
--> manage.py
--> api_app (where are the view.py model.py, etc)
--> api_solar (where are settings.py, etc)
In my docker-compose file I have the following:
services:
django:
build:
context: ./api_solar_django
container_name: django-app
restart: unless-stopped
ports:
- "8090:8090"
volumes:
- ./api_solar_django:/app
- static_volume:/app/static
environment:
- DJANGO_SETTINGS_MODULE=api_solar.settings
- ENC_KEY=key
depends_on:
- db
networks:
- app-network
nginx:
image: nginx:alpine
container_name: nginx
restart: unless-stopped
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
- static_volume:/static
depends_on:
- django
- frontend
networks:
- app-network
networks:
app-network:
driver: bridge
volumes:
postgres_data:
static_volume:
The static files, inside each container is located as:
- Django container: /app folder, same as manage.py
- Nginx container: root folder
My nginx.conf file is configured as:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
# Incluindo o servidor configurado
server {
listen 80;
# Serve arquivos estáticos do Django
location /static/ {
alias /static/;
}
# Proxy para o backend (Django)
location /api/ {
proxy_pass http://django-app:8090/;
proxy_set_header Host $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 para o frontend (Vue.js)
location / {
proxy_pass http://vue-app:5173;
proxy_set_header Host $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;
}
}
}
I am trying to access the admin panel via http://localhost:8090/admin/, although it works, it can't access the static files.