Django media images not loading in production using Nginx , with Debug=False
I'm deploying my Django project with DEBUG=False
, and media files like images are not loading.
Nginx Configuration
server {
listen 80;
listen [::]:80;
server_name <my_server_ip>;
location /media/ {
alias /home/ubuntu_server02/cdo_portal/media/;
autoindex on;
}
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Django settings.py
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
What are the details of your problem?
I deployed my Django project with DEBUG=False
, and I'm trying to serve uploaded media files (images) using Nginx. However, when I access a media file through the browser (e.g., http://<server_ip>/media/sample.jpg
), I get a 404 Not Found error.
The media files exist in the correct folder, Nginx is running, and permissions are correct. I'm not sure if the issue is with my Nginx config or something else.
What did you try and what were you expecting?
I tried accessing media files through the browser directly, like:
http://<my_server_ip>/media/sample.jpg
I expected the image to load, but it returned a 404. I checked that the file exists, the path in Nginx is correct, and the alias points to the right folder. Still, Nginx doesn't seem to serve the media files.