Home > Mobile >  Nginx not finding static files in Dockered Django app in Azure Web App for containers
Nginx not finding static files in Dockered Django app in Azure Web App for containers

Time:10-19

I managed to run my Django app locally with docker compose( Django container Nginx container) and it works fine, but when i want to run it in Azure web app for containers nginx can't find the ressources. I don't know if i should change some configuration so it can work in Azure services or i need to enable ports in azure app settings therefore my containers could communicate.

This is my nginx configuration (nginx.conf) :

upstream myapp {
    server web:8000;
}

server {

    listen 80;
    server_name myappname.azurewebsites.net;

    location / {
        proxy_pass http://myapp;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

    location /static/ {
        alias /home/app/web/static/;
    }

    location /media/ {
        alias /home/app/web/media/;
    }

}

I don't know why it did work locally when i run docker compose but in azure web app nginx can't find static files.

Please let me know if I need to clarify or include anything else.

Thanks in advance.

CodePudding user response:

Thank you Joseba S. . Posting your suggestion as an answer to help other community members.

python manage.py collectstatic

You need that command for Django to copy over all your static files to the directory specified in the STATIC_ROOT setting. Remember you have to execute that every time you have some change in your static files.

Spin down the development containers:

$ docker-compose down -v

Test:

$ docker-compose -f docker-compose.prod.yml up -d --build
$ docker-compose -f docker-compose.prod.yml exec web python manage.py migrate --noinput
$ docker-compose -f docker-compose.prod.yml exec web python manage.py collectstatic --no-input --clear

You can also verify in the logs via docker-compose -f docker-compose.prod.yml logs -f that requests to the static files are served up successfully via Nginx.

You can refer to Dockerizing Django with Postgres, Gunicorn, and Nginx, Cannot locate static files after deploying with docker and Nginx failing to serve django static or media files

  • Related