简体   繁体   中英

serve static files from Django Docker to nginx

I'm dockerizing Django application but static files are not being served.

settings.py

STATIC_URL = '/static/'

STATICFILES_DIRS = [
    os.path.join(os.path.dirname(BASE_DIR), 'static_my_project')
]

STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static_cdn', 'static_root')

docker-compose.yml

services:
  nginx:
    image: nginx:alpine
    container_name: "originor-nginx"
    ports:
      - "10080:80"
      - "10443:43"
    volumes:
      - .:/app
      - ./config/nginx:/etc/nginx/conf.d
      - originor_static_volume:/app/static_cdn/static_root
      - originor_media_volume:/app/static_cdn/media_root
    depends_on:
      - web
  web:
    build: .
    container_name: "originor-web"
    command: ["./wait-for-it.sh", "db:5432", "--", "./start.sh"]
    volumes:
      - .:/app
      - originor_static_volume:/app/static_cdn/static_root
      - originor_media_volume:/app/static_cdn/media_root
    ports:
      - "9010:9010"
    depends_on:
      - db
  db:
    image: postgres:11
    container_name: "originor-postgres-schema"
    volumes:
      - originor_database:/var/lib/postgresql/data
    ports:
      - "5432:5432"
  pgadmin:
    image: dpage/pgadmin4
    container_name: "originor_pgadmin"
    volumes:
      - originor_pgadmin:/var/lib/pgadmin

volumes:
  originor_database:
  originor_static_volume:
  originor_media_volume:
  originor_pgadmin:

and nginx.conf

error_log   /var/log/nginx/error.log;

include /etc/nginx/conf.d/proxy.conf;
proxy_headers_hash_bucket_size 128;

upstream dweb {
    ip_hash;
    server web:9010 fail_timeout=0;
}

server {
    listen 10080;
    server_name localhost;
    access_log /var/log/nginx/localhost.access.log combined;

    location /static/ {
        autoindex on;
        alias /app/static_cdn/static_root/;
    }

    location /media/ {
        alias /app/static_cdn/media_root/;
    }

    location / {
        proxy_pass http://dweb/;
    }
}

But on access /admin/ in browser, it consoles

f032d416bce1_originor-web | Not Found: /static/admin/css/login.css
f032d416bce1_originor-web | Not Found: /static/admin/css/responsive.css
f032d416bce1_originor-web | Not Found: /static/admin/css/base.css
f032d416bce1_originor-web | Not Found: /static/admin/css/base.css

I can verify the files there in /app/static_cdn/static_root directory by executing

docker exec -it <container_id> ls -la /app/static_cdn/static_root

Edit 2: docker logs <container>

wait-for-it.sh: waiting 15 seconds for db:5432
wait-for-it.sh: db:5432 is available after 0 seconds
--: Starting application build


--: Creating migration
No changes detected
------: makemigrations complete


--: Running migration
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  No migrations to apply.
------: migrate complete


--: load initial user data


--: load initial oauth app data


--: Running collectstatic

0 static files copied to '/app/static_cdn/static_root', 119 unmodified.
------: collectstatic complete


--: Starting Gunicorn.
[2019-01-11 13:26:47 +0000] [21] [INFO] Starting gunicorn 19.9.0
[2019-01-11 13:26:47 +0000] [21] [INFO] Listening at: http://0.0.0.0:9010 (21)
[2019-01-11 13:26:47 +0000] [21] [INFO] Using worker: sync
[2019-01-11 13:26:47 +0000] [23] [INFO] Booting worker with pid: 23
[2019-01-11 13:26:47 +0000] [24] [INFO] Booting worker with pid: 24
[2019-01-11 13:26:47 +0000] [25] [INFO] Booting worker with pid: 25
Not Found: /static/admin/css/fonts.css

Edit 3: nginx log

While running docker-compose up it gives the following log

在此处输入图片说明

But running docker logs originor-nginx it gives nothing

Somewhere in your 'web' initialization you must call manage.py collectstatic to put static files of application in your volume. More info https://docs.djangoproject.com/en/2.1/howto/static-files/

UPD: nginx conf for uwsgi proxy:

location / {
    uwsgi_pass uwsgi://web:9010;
    include uwsgi_params;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM