From 784076e6be88393639427c8d6afdb79b0939a9e5 Mon Sep 17 00:00:00 2001 From: bisco Date: Wed, 29 Apr 2026 18:46:53 +0200 Subject: [PATCH] fix(docker): serve django static files via nginx --- backend/azionelab/settings.py | 3 ++- infra/docker/compose.yml | 5 +++++ infra/docker/nginx/templates/default.conf.template | 6 +++--- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/backend/azionelab/settings.py b/backend/azionelab/settings.py index dddd29e..a835e8b 100644 --- a/backend/azionelab/settings.py +++ b/backend/azionelab/settings.py @@ -112,7 +112,8 @@ DEFAULT_FROM_EMAIL = os.environ.get("DEFAULT_FROM_EMAIL", "no-reply@azionelab.lo if "test" in sys.argv: EMAIL_BACKEND = "django.core.mail.backends.locmem.EmailBackend" -STATIC_URL = "static/" +STATIC_URL = "/static/" +STATIC_ROOT = BASE_DIR / "staticfiles" DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" REST_FRAMEWORK = { diff --git a/infra/docker/compose.yml b/infra/docker/compose.yml index 37b0f52..0d26a35 100644 --- a/infra/docker/compose.yml +++ b/infra/docker/compose.yml @@ -19,11 +19,14 @@ services: POSTGRES_PORT: ${POSTGRES_PORT:-5432} expose: - "${BACKEND_PORT:-8000}" + volumes: + - django_static:/app/staticfiles depends_on: postgres: condition: service_healthy networks: - internal + user: "0:0" restart: unless-stopped frontend: @@ -66,6 +69,7 @@ services: NGINX_ENVSUBST_FILTER: "^(BACKEND_HOST|BACKEND_PORT|FRONTEND_HOST|FRONTEND_PORT)$" volumes: - ./nginx/templates:/etc/nginx/templates:ro + - django_static:/var/www/static:ro depends_on: - backend - frontend @@ -75,6 +79,7 @@ services: volumes: postgres_data: + django_static: networks: internal: diff --git a/infra/docker/nginx/templates/default.conf.template b/infra/docker/nginx/templates/default.conf.template index 664e95f..50a3f3d 100644 --- a/infra/docker/nginx/templates/default.conf.template +++ b/infra/docker/nginx/templates/default.conf.template @@ -29,9 +29,9 @@ server { } location /static/ { - proxy_pass http://azionelab_backend; - proxy_set_header Host $host; - proxy_set_header X-Forwarded-Proto $scheme; + alias /var/www/static/; + access_log off; + expires 1d; } location /media/ {