skip to Main Content

I have tried everything to serve my media file but yet getting same 404 error. Please guide.

My docker-compose file:

version: "3.9"
services:
  nginx:
     container_name: realestate_preprod_nginx_con
     build: ./nginx
     volumes:
       - static_volume:/home/inara/RealEstatePreProd/static
       - media_volume:/home/inara/RealEstatePreProd/media
     networks:
       glory1network:
         ipv4_address: 10.1.1.8
     expose:
       - 8000
     depends_on:
       - realestate_frontend
       - realestate_backend
  real_estate_master_db:
    image: postgres:latest
    container_name: realestate_master_db_con
    env_file:
      - "./database/master_env"
    restart: "always"
    networks:
      glory1network:
        ipv4_address: 10.1.1.5
    expose:
      - 5432
    volumes:
      - real_estate_master_db_volume:/var/lib/postgresql/data
  real_estate_tenant1_db:
    image: postgres:latest
    container_name: realestate_tenant1_db_con
    env_file:
      - "./database/tenant1_env"
    restart: "always"
    networks:
      glory1network:
        ipv4_address: 10.1.1.9
    expose:
      - 5432
    volumes:
      - real_estate_tenant1_db_volume:/var/lib/postgresql/data
      
  realestate_frontend:
    image: realestate_web_frontend_service
    container_name: realestate_frontend_con
    restart: "always"
    build: ./frontend
    command: bash -c "./realestate_frontend_ctl.sh"
    expose:
      - 8092
    networks:
      glory1network:
        ipv4_address: 10.1.1.6
    depends_on:
      - real_estate_master_db
      - real_estate_tenant1_db

  realestate_backend:
    image: realestate_web_backend_service
    container_name: realestate_backend_con
    restart: "always"
    build: ./backend
    command: bash -c "./realestate_backend_ctl.sh"
    expose:
      - 8091
    volumes:
      - static_volume:/home/inara/RealEstatePreProd/static
      - media_volume:/home/inara/RealEstatePreProd/media
    networks:
      glory1network:
        ipv4_address: 10.1.1.7
    env_file:
      - "./database/env"
    depends_on:
      - realestate_frontend
      - real_estate_master_db
      - real_estate_tenant1_db

networks:
    glory1network:
        external: true

volumes:
  real_estate_master_db_volume:
  real_estate_tenant1_db_volume:
  static_volume:
  media_volume:

My nginx configuration file:

upstream realestate_frontend_site {
    server realestate_frontend:8092;
}

server {
    listen 8000;
    access_log /home/inara/RealEstatePreProd/realestate_frontend-access.log;
    error_log /home/inara/RealEstatePreProd/realestate_frontend-error.log;
    client_max_body_size 0;
    location / {
        proxy_pass http://realestate_frontend_site;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
        client_max_body_size 0;

    }
}

upstream realestate_backend_site {
    server realestate_backend:8091;
}

server {
    listen 8000;
    access_log /home/inara/RealEstatePreProd/realestate_backend-access.log;
    error_log /home/inara/RealEstatePreProd/realestate_backend-error.log;
    location / {
        proxy_pass http://realestate_backend_site;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }
     location /static {
        root /home/inara/RealEstatePreProd;
    }

    location /media/ {      
        alias /home/inara/RealEstatePreProd/media/;
    }
}


All the APIs are working fine but any media file gives 404.

I have checked the volume and validated that files being accessed are present there.
I logged in my docker container and validated file presence in media folder there too.

Please guide what did I miss ??

I expect to access my media files but getting 404

Edit: My settings.py file:

MEDIA_ROOT = os.path.join(MAIN_DIR, 'media')
MEDIA_URL = '/media/'

where MAIN_DIR = '/home/inara/RealEstatePreProd'

I have also tried BASE_DIR

Files are being uploaded to media directory as well as volume but I am unable to access them via browser. Exact error in nginx is:

100.64.6.10 – – [05/Dec/2022:08:46:28 +0500] "GET /media/project_logos/logo_Testimages660.png HTTP/1.1" 404 179 "http://dev-realestate-frontend.inara.tech/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"

2

Answers


  1. First step is to define /static location directive above / location directive.

    Because as per your code /static url is also routed to / , as / is above /static. Anything for /static should go to /static and not to /, so you can try this and let us know if the error is resolved or not.

    Login or Signup to reply.
  2. Make sure you specify MEDIA_URL and MEDIA_ROOT in RealEstatePreProd.settings:

    MEDIA_URL = '/media/'
    MEDIA_ROOT = BASE_DIR / 'media'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search