Home:ALL Converter>Django, how serve media(users) files using nginx?

Django, how serve media(users) files using nginx?

Ask Time:2018-11-27T00:04:09         Author:Pepik

Json Formatter

I'am struggling with my nginx configuration. Using docker-compose I deployed my Django app to vps. Static files works fine but I cant set up correctly media files. Below are my conf files:

setting.py:

STATIC_URL = '/static/'
STATIC_ROOT = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = '/media/'

nginx conf file:

upstream web {
  ip_hash;
  server web:8000;
}

# portal
server {
    location /static/ {
        autoindex on;
        alias /static/;
    }
    location /media/ {
        autoindex on;
        alias /home/www/book_store/media/;
    }
    location / {
        proxy_pass http://web/;
    }
    listen 8000;
    server_name localhost;
}

docker-compose.yml

version: '2.1'

services:
  nginx:
    #other commands
    volumes:
      - .:/code
      - ./nginx:/etc/nginx/conf.d
      - /static:/static
      - /media:/media
    depends_on:
      - web
 web:
    #other commands
    volumes:
      - .:/code
      - /static:/static
      - /media:/media

error message when app trying to access media file (message from nginx container):

ng01 | 2018/11/26 15:52:37 [error] 5#5: *6 open() "/home/www/book_store/media/default.png" failed (2: No such file or directory), client: XX.XX.XXX.XXX, server: localhost, request: "GET /media/default.png HTTP/1.1", host: "XXX.XXX.XX.XXX:8000", referrer: "http://XXX.XXX.XX.XXXX:8000/users/user/profile/"

Author:Pepik,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/53484929/django-how-serve-mediausers-files-using-nginx
yy