How to serve media files?

August 16, 2017 573 views
Nginx Django Ubuntu

I have an Django application and I trying deploy this, but I have problem, in this moment I can see my media files because I add media and static into urls.py (as must be done in development), but I trying change to production mode and I use nginx, but I don't understand how to server my media files with nginx, I have configured nginx this way

server {
    listen 80;
    server_name ip_server

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/ubuntu/tracker-web/trackerServer;
    location /media/ {
        alias /home/ubuntu/tracker-web/trackerServer/media/;
    location / {
        include proxy_params;
        proxy_pass http://unix:/home/ubuntu/tracker-web/trackerServer/trackerServer.sock;

Before I only use /static/ and all works fine, but I don't understand how to use the same way my media folder to serve files, I want to clarify that I use as an angular 2 interface , so I do not use Django templates, and also use gunicorn.

1 Answer

Sorry to see that your question hasn't received an answer yet. It's unclear exactly what problem you are facing without more information. Unfortunately, after this much time, it is unlikely an answer for this specific question will be provided.

For people still landing here via search, the path used for /media/ in the Nginx configuration should match what the project's settings.py file uses for MEDIA_ROOT. You might also want to check out this tutorial on getting up and running with Django and Nginx:

Django is a powerful web framework that can help you get your Python application or website off the ground. Django includes a simplified development server for testing your code locally, but for anything even slightly production related, a more secure and powerful web server...
Have another answer? Share your knowledge.