Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error 404 Nginx can't find media files. +Django

I was setting up basic Django site with nginx. When i navigate to the url of the file i get error 404 Not Found nginx/1.18.0 (Ubuntu)

This is the conf file

upstream django {
    server unix:///home/jo/testproject/testproject.sock;
}
# configuration of the server
server {
    listen      80;
    server_name _;
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;

    # Django media and static files
    location /media/  {
    alias /home/jo/testproject/media/;
    }
    location /static {
        alias /home/jo/testproject/static;
    }

    # Send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /home/jo/testproject/uwsgi_params;
    }
}

the media folder is in /home/jo/testproject/media and inside it i have media.gif, but ip/media/media.gif doesnt work

when i write the ip in the browser i get

Welcome to nginx! If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org. Commercial support is available at nginx.com.

Thank you for using nginx.

I think everything else i setup correctly

like image 214
Josif Hamed Avatar asked Sep 07 '25 00:09

Josif Hamed


1 Answers

Docker + NGINX + Gunicorn + Django

Django project:

djangoapp
 - ...
 - media
 - static
 - manage.py
 - requirements.txt  
nginx
 - Dockerfile
 - nginx.conf 
docker-compose.yml  
Dockerfile

Dockerfile:

FROM ubuntu:20.04

# Prevents Python from writing pyc files to disc
ENV PYTHONDONTWRITEBYTECODE 1
# Prevents Python from buffering stdout and stderr
ENV PYTHONUNBUFFERED 1
ENV DEBUG 0

# https://hub.docker.com/r/fnndsc/ubuntu-python3/dockerfile
RUN apt-get update \
  && apt-get install -y python3-pip gunicorn

RUN mkdir /app

COPY djangoapp/requirements.txt requirements.txt
COPY djangoapp /app/

RUN pip3 install -r requirements.txt
WORKDIR /app/

EXPOSE 8000

CMD ["gunicorn3", "-b", "0.0.0.0:8000", "djangoapp.wsgi:application", "--workers=2"]

docker-compose.yml:

version: '3'

services:
  djangoapp:
    build: .
    container_name: djangoapp1
    volumes:
      - .:/djangoapp
      - static:/app/static
      - media:/app/media

  nginx:
    build: ./nginx
    container_name: nginx
    environment:
      - SERVER_NAME=8.43.162.54
    ports:
      - "80:80"
    volumes: 
      - .:/djangoapp
      - static:/app/static
      - media:/app/media
    restart: always

networks: 
  default:
    driver: bridge

volumes:
  media:
  static:

nginx/Dockerfile:

FROM nginx

RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d/

nginx/nginx.conf:

server {
    listen 80;
    server_name $SERVER_NAME;

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

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

    location / {
        proxy_set_header Host $host;
        proxy_pass http://djangoapp1:8000;
    }
}

settings.py:

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

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
like image 154
Timeless Avatar answered Sep 08 '25 21:09

Timeless