Serving media files in production mode

Want to serve media files when Debug is turned to False. I am kinda new in all this thing.
Currently I am using django as DRF backend with svelte frontend and nginx that acts like proxy between all this. Everything is dockerised. There is not any particular problems with files in STATIC folder with admin styles etc, but MEDIA folder with uploaded images etc having problem serving images when Debug is disabled.
Can somebody help me with it please? There is my code just in case:

urls.py

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

settings.py:

LOCALE_PATHS = (os.path.join(BASE_DIR, "locale"),)
STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "static")

MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media")

nginx.conf

worker_processes 1;

events {
  worker_connections 1024;
}

http {
    upstream backend {
        server back:7000;
    }

     server {
        listen 80;

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

        location /sitemap.xml {
            proxy_pass http://front:8080/sitemap.xml;
            # proxy_pass http://front:3000;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }

        location / {
            proxy_pass http://front:8080;
            # proxy_pass http://front:3000;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }

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

        location /static/ {
            alias /back/static/;
        }

       location /media/ {
            proxy_pass http://backend/media/;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
        }
    }
}

docker-compose.yml

version: '3.9'

services:
  back:
    container_name: back
    restart: always
    build:
      context: .
      dockerfile: ./compose/back/Dockerfile
    volumes:
      - ./back:/back
    ports:
      - 7000:7000
    expose:
      - 7000

  front:
    container_name: front
    restart: always
    build:
      context: .
      dockerfile: ./compose/front/Dockerfile
    volumes:
      - ./front:/front
      - /front/node_modules
    ports:
      - 8080:8080
    expose:
      - 8080

  nginx:
    container_name: nginx
    restart: always
    build:
      context: .
      dockerfile: ./nginx/Dockerfile
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
    ports:
      - 80:80
      - 443:443
    depends_on:
      - front

and django Dockerfile having this by default:

FROM python:3.11.4-bookworm

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

ARG PROJ_DIR=/back

RUN mkdir -p ${PROJ_DIR}
COPY ./back ${PROJ_DIR}

WORKDIR ${PROJ_DIR}

RUN pip install -U pip \
    pip install -r requirements.txt

RUN apt-get update \
    && apt-get -y install gettext

CMD ["python", "manage.py", "runserver", "0.0.0.0:7000", "--insecure"]

Its already quite a long time I am bashing my head against this wall, but still can’t solve it. It seems I am missing something, but don’t really understand what exactly.

You should be serving both your Django and Media files with nginx. Nginx should be serving the files directly from a storage volume, not some other handler.

Ok so after destroying everything and remaking from scratch, I was kinda able to solve it. Thanks.

1 Like

Hello, could you explain how you fixed it? I’m doing something similar and struggling with it.

Welcome @hemanth-sharma !

If you’re having an issue with which you would like assistance, I’d recommend that you open a new topic for it, and explain the details of the problem(s) you are facing. Deployment does tend to be a problematic area, and while in many cases the symptoms are similar to what other people are experiencing, frequently the underlying causes are different, and so are best handled in separate threads.