How to serve media files on Django production environment?

sium_hossain - Jul 16 '22 - - Dev Community

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.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player