DEV Community

sium_hossain
sium_hossain

Posted on

How to serve media files on Django production environment?

I repeat this is not an absolute solution or recommended solution for a large-scale web app. It's just for personal project or low traffic web application.

For high traffic web applications, recommended solution use cloud services like amazon s3 bucket etc for serving media file.

OK! I assume that you know about little bit about nginx. If want to know all about nginx configuration let me know in comment.

Go to your nginx domain file configuration -

sudo nano /etc/nginx/sites-available/<your domain file configuration>
Enter fullscreen mode Exit fullscreen mode
location /static/ {
        #############
    }
location /media/ {
        root #path of your project directory where your media folder exist- example: /home/sium/nmedia_backend/nMedia;
    }
Enter fullscreen mode Exit fullscreen mode

And then save this file and check is there any error in nginx config file by sudo nginx -t and then restart nginx service by sudo systemctl restart nginx

Voila! your media file will serve via nginx. Because DEBUG=FALSE mode means in production mode django won't serve your media file by itself.

Top comments (2)

Collapse
 
oscar404m profile image
Linux_Mint • Edited

when i deploy my django app i get this issue how can i solve it please ?

Collapse
 
siumhossain profile image
sium_hossain • Edited

django doesn't take care of your media file when debug mode is turn off i mean False. You have to use 3rd party like CDN service or AWS for large application. But if you have low traffic web or personal app you can use above solution.