Home > Net >  nginx unable to load media files - 404 (Not found)
nginx unable to load media files - 404 (Not found)

Time:12-05

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

CodePudding user response:

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.

CodePudding user response:

Make sure you specify MEDIA_URL and MEDIA_ROOT in RealEstatePreProd.settings:

MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'
  • Related