docker-compose can not serve static files with nginx

137 Views Asked by At

I have a django project. I am trying to implement it into a server with nginx and docker. Here is so far I got. The static files are served at 8000 port but default does not serve it. That's why I am thinking the problem is with nginx. Here is my code:

docker-compose.yml

version: '3'

services:
  db:
    image: postgres
    environment:
      POSTGRES_DB: db
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
    volumes:
      - db-data:/var/lib/postgresql/data

  web:
    build: .
    command: >
      sh -c "python manage.py collectstatic --noinput && python manage.py runserver 0.0.0.0:8000"
    volumes:
      - .:/usr/src/app
      - static-data:/usr/src/app/static
      - media-data:/usr/src/app/media
    ports:
      - "8000:8000"
    depends_on:
      - db

  nginx:
    image: nginx
    ports:
      - "80:80"
    volumes:
      - ./nginx.conf:/etc/nginx/conf.d/default.conf
      - static-data:/usr/src/app/static
      - media-data:/usr/src/app/media
    depends_on:
      - web

volumes:
  db-data:
  static-data:
  media-data:

nginx.conf

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://web:8000;
        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;
    }

    location /static/ {
        alias /usr/src/app/static/;
    }

    location /media/ {
        alias /usr/src/app/media/;
    }
}

settings.py

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    BASE_DIR / "static",
    '/var/www/',
]

MEDIA_URL = '/images/'

error logs:

Starting myproject_db_1 ... done
Starting myproject_web_1 ... done
Starting myproject_nginx_1 ... done
Attaching to myproject_db_1, myproject_web_1, myproject_nginx_1
db_1     | 
db_1     | PostgreSQL Database directory appears to contain a database; Skipping initialization
db_1     | 
nginx_1  | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
nginx_1  | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
nginx_1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
db_1     | 2023-05-01 18:54:56.853 UTC [1] LOG:  starting PostgreSQL 15.2 (Debian 15.2-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
db_1     | 2023-05-01 18:54:56.853 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1     | 2023-05-01 18:54:56.854 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1     | 2023-05-01 18:54:56.858 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1     | 2023-05-01 18:54:56.864 UTC [29] LOG:  database system was interrupted; last known up at 2023-04-30 15:35:42 UTC
db_1     | 2023-05-01 18:54:57.134 UTC [29] LOG:  database system was not properly shut down; automatic recovery in progress
db_1     | 2023-05-01 18:54:57.138 UTC [29] LOG:  redo starts at 0/195C940
db_1     | 2023-05-01 18:54:57.138 UTC [29] LOG:  invalid record length at 0/195C978: wanted 24, got 0
db_1     | 2023-05-01 18:54:57.138 UTC [29] LOG:  redo done at 0/195C940 system usage: CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s
db_1     | 2023-05-01 18:54:57.143 UTC [27] LOG:  checkpoint starting: end-of-recovery immediate wait
db_1     | 2023-05-01 18:54:57.160 UTC [27] LOG:  checkpoint complete: wrote 3 buffers (0.0%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.005 s, sync=0.003 s, total=0.019 s; sync files=2, longest=0.002 s, average=0.002 s; distance=0 kB, estimate=0 kB
db_1     | 2023-05-01 18:54:57.170 UTC [1] LOG:  database system is ready to accept connections
nginx_1  | 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
nginx_1  | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf differs from the packaged version
nginx_1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
nginx_1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
nginx_1  | /docker-entrypoint.sh: Configuration complete; ready for start up
nginx_1  | 2023/05/01 18:54:57 [notice] 1#1: using the "epoll" event method
nginx_1  | 2023/05/01 18:54:57 [notice] 1#1: nginx/1.23.4
nginx_1  | 2023/05/01 18:54:57 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
nginx_1  | 2023/05/01 18:54:57 [notice] 1#1: OS: Linux 5.19.0-41-generic
nginx_1  | 2023/05/01 18:54:57 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
nginx_1  | 2023/05/01 18:54:57 [notice] 1#1: start worker processes
nginx_1  | 2023/05/01 18:54:57 [notice] 1#1: start worker process 28
nginx_1  | 2023/05/01 18:54:57 [notice] 1#1: start worker process 29
nginx_1  | 2023/05/01 18:54:57 [notice] 1#1: start worker process 30
nginx_1  | 2023/05/01 18:54:57 [notice] 1#1: start worker process 31
nginx_1  | 2023/05/01 18:54:57 [notice] 1#1: start worker process 32
nginx_1  | 2023/05/01 18:54:57 [notice] 1#1: start worker process 33
nginx_1  | 2023/05/01 18:54:57 [notice] 1#1: start worker process 34
nginx_1  | 2023/05/01 18:54:57 [notice] 1#1: start worker process 35
nginx_1  | 2023/05/01 18:54:57 [notice] 1#1: start worker process 36
nginx_1  | 2023/05/01 18:54:57 [notice] 1#1: start worker process 37
nginx_1  | 2023/05/01 18:54:57 [notice] 1#1: start worker process 38
nginx_1  | 2023/05/01 18:54:57 [notice] 1#1: start worker process 39
nginx_1  | 2023/05/01 18:54:57 [notice] 1#1: start worker process 40
nginx_1  | 2023/05/01 18:54:57 [notice] 1#1: start worker process 41
nginx_1  | 2023/05/01 18:54:57 [notice] 1#1: start worker process 42
nginx_1  | 2023/05/01 18:54:57 [notice] 1#1: start worker process 43
web_1    | 
web_1    | 0 static files copied to '/var/www/example.com/static', 1381 unmodified.
web_1    | Watching for file changes with StatReloader
web_1    | [01/May/2023 21:55:11] "GET / HTTP/1.0" 200 9998
nginx_1  | 172.21.0.1 - - [01/May/2023:18:55:11 +0000] "GET / HTTP/1.1" 200 9998 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36" "-"
nginx_1  | 172.21.0.1 - - [01/May/2023:18:55:11 +0000] "GET /static/vendor/fontawesome-free/css/all.min.css HTTP/1.1" 404 555 "http://127.0.0.1/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36" "-"
nginx_1  | 2023/05/01 18:55:11 [error] 28#28: *1 open() "/usr/src/app/static/vendor/fontawesome-free/css/all.min.css" failed (2: No such file or directory), client: 172.21.0.1, server: example.com, request: "GET /static/vendor/fontawesome-free/css/all.min.css HTTP/1.1", host: "127.0.0.1", referrer: "http://127.0.0.1/"
nginx_1  | 2023/05/01 18:55:11 [error] 29#29: *2 open() "/usr/src/app/static/css/sb-admin-2.min.css" failed (2: No such file or directory), client: 172.21.0.1, server: example.com, request: "GET /static/css/sb-admin-2.min.css HTTP/1.1", host: "127.0.0.1", referrer: "http://127.0.0.1/"
nginx_1  | 172.21.0.1 - - [01/May/2023:18:55:11 +0000] "GET /static/css/sb-admin-2.min.css HTTP/1.1" 404 555 "http://127.0.0.1/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36" "-"
nginx_1  | 172.21.0.1 - - [01/May/2023:18:55:11 +0000] "GET /static/vendor/bootstrap/js/bootstrap.bundle.min.js HTTP/1.1" 404 555 "http://127.0.0.1/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36" "-"
nginx_1  | 172.21.0.1 - - [01/May/2023:18:55:11 +0000] "GET /static/vendor/jquery/jquery.min.js HTTP/1.1" 404 555 "http://127.0.0.1/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36" "-"
nginx_1  | 2023/05/01 18:55:11 [error] 29#29: *2 open() "/usr/src/app/static/vendor/jquery/jquery.min.js" failed (2: No such file or directory), client: 172.21.0.1, server: example.com, request: "GET /static/vendor/jquery/jquery.min.js HTTP/1.1", host: "127.0.0.1", referrer: "http://127.0.0.1/"
nginx_1  | 2023/05/01 18:55:11 [error] 28#28: *1 open() "/usr/src/app/static/vendor/bootstrap/js/bootstrap.bundle.min.js" failed (2: No such file or directory), client: 172.21.0.1, server: example.com, request: "GET /static/vendor/bootstrap/js/bootstrap.bundle.min.js HTTP/1.1", host: "127.0.0.1", referrer: "http://127.0.0.1/"
nginx_1  | 2023/05/01 18:55:11 [error] 30#30: *4 open() "/usr/src/app/static/vendor/jquery-easing/jquery.easing.min.js" failed (2: No such file or directory), client: 172.21.0.1, server: example.com, request: "GET /static/vendor/jquery-easing/jquery.easing.min.js HTTP/1.1", host: "127.0.0.1", referrer: "http://127.0.0.1/"
nginx_1  | 172.21.0.1 - - [01/May/2023:18:55:11 +0000] "GET /static/vendor/jquery-easing/jquery.easing.min.js HTTP/1.1" 404 555 "http://127.0.0.1/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36" "-"
nginx_1  | 2023/05/01 18:55:11 [error] 31#31: *5 open() "/usr/src/app/static/js/sb-admin-2.min.js" failed (2: No such file or directory), client: 172.21.0.1, server: example.com, request: "GET /static/js/sb-admin-2.min.js HTTP/1.1", host: "127.0.0.1", referrer: "http://127.0.0.1/"
nginx_1  | 172.21.0.1 - - [01/May/2023:18:55:11 +0000] "GET /static/js/sb-admin-2.min.js HTTP/1.1" 404 555 "http://127.0.0.1/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36" "-"
nginx_1  | 2023/05/01 18:55:11 [error] 32#32: *6 open() "/usr/src/app/static/img/undraw_profile.svg" failed (2: No such file or directory), client: 172.21.0.1, server: example.com, request: "GET /static/img/undraw_profile.svg HTTP/1.1", host: "127.0.0.1", referrer: "http://127.0.0.1/"
nginx_1  | 172.21.0.1 - - [01/May/2023:18:55:11 +0000] "GET /static/img/undraw_profile.svg HTTP/1.1" 404 555 "http://127.0.0.1/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36" "-"
nginx_1  | 2023/05/01 18:55:11 [error] 32#32: *6 open() "/usr/src/app/static/image/favicon.ico" failed (2: No such file or directory), client: 172.21.0.1, server: example.com, request: "GET /static/image/favicon.ico HTTP/1.1", host: "127.0.0.1", referrer: "http://127.0.0.1/"
nginx_1  | 172.21.0.1 - - [01/May/2023:18:55:11 +0000] "GET /static/image/favicon.ico HTTP/1.1" 404 555 "http://127.0.0.1/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36" "-"
0

There are 0 best solutions below