Nginx not seeing static files, when launching with docker compose +gunicorn

The thing is that i have set up dockerfiles, docker-compose is running everything as it should be. Databases is connected and all the staff. But the only problem is, that i cant load staticfiles. Gunicorn informs, that files "Not Found" both admin, and rest_framework static files. When im doing "docker-compose up" it says that `0 static files were copied to '/static', 165 unmodified

So far i have checked paths which are configured in STATIC_ROOT and STATIC URL. Also, modified my docker-compose file:

version: '3.11'

      - static:/static/
      - .env
      context: .
      - "8000:8000"
    build: ./nginx
      - static:/static/
      - "80:80"
      - django_gunicorn
      - db
    image: postgres
      - 5432
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=postgres

Also i have inspected nginx config file one more time:

upstream django {
    server django_gunicorn:8000;

server {
    listen 80;

    location / {
        proxy_pass http://django;
    location /static/ {
        autoindex on;
        alias /static/;

i have also tried different approaches to setting up root and url of staticfiles, but left it like this for now:

STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'

Also there were suggestions to run "collectstatic" first, which i have already done, but no effect, so ive deleted static folder for now, because i didnt see 'static' folder in other applications as well.



python migrate --no-input
python collectstatic --no-input

gunicorn config.wsgi:application --bind

Nginx must map STATIC_URL to STATIC_ROOT with respect to volume mapping in docker-compose.

docker-compose volume mapping for Nginx:

- static:/static/

virtual volume "static" to root directory /static/

Nginx location

location /static/ {
    autoindex on;
    alias /static/;

Maps STATIC_URL = /static/ to directory inside nginx container /static/ which matches docker-compose volume mapping we just mentioned. Location is supposed to match STATIC_URL because Django is generating those URLs and it does use STATIC_URL so static files URLs will begin with STATIC_URL and to catch those URLs nginx must use the same location. But alias value is not supposed to match STATIC_ROOT because we "deliver" static files to nginx via virtual volume mapping in docker-compose and they (static files) will appear for nginx just where we tell them to - - static:/static/ - nginx container will see them under /static/ path. Django settings do not affect this path.

Django project docker image

COPY ./backend /app

which means that BASE_DIR inside the container will be /app and this is exactly what collectstatic is reporting to you: '165 static files copied to '/app/static'

now we need to share those files with nginx via virtual volume by mapping virtual volume to the directory inside django project container with collected static files. Static files were collected to /app/static. Virtual volume shared with nginx is static, so

docker-compose volume mapping for Django project

- static:/app/static/

enter image description here

Back to Top