Report this

What is the reason for this report?

Django + nginx + gunicorn - Media Files Not Working

Posted on March 22, 2019

Hello, I have deployed a Django project on Ubuntu 16.04 with Nginx and Gunicorn. I have gotten everything, including the static files but my media files will not serve properly.

settings.py

MEDIA_URL = '/media/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

In my project I allow users to upload a file through a Model, which gets passed into a ModelForm and the code for the model field is below.

models.py

resume_upload = models.FileField(blank=False, upload_to='resumes', null=True, validators=[FileExtensionValidator(allowed_extensions=['pdf']), validate_file_size])

What I have listed in /etc/nginx/sites-available/ is


server {
    listen 80;
    server_name website.com www.website.com ;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/syed/btawebsite;
    }

    location = /media/ {
        root /home/syed/btawebsite;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/syed/myproject.sock;
    }
}

I would like go on the admin, click on the uploaded file and be able to view the file in the browser. Anyone have any ideas on how I can achieve this? I have verified through using the terminal and looking through the directories that the files are in fact adding to ~btawebsite/media/resumes but I am unable to view them when I click the admin url.

When I click on that url I get an error:

Not Found The requested resource was not found on this server.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Greetings!

I think it may be the trailing slashes. Try changing this:

    location /static/ {
        root /home/syed/btawebsite;
    }

To this:

    location /static {
        root /home/syed/btawebsite;
    }

Jarland

The issue lies in how the location block for serving media files is configured in your Nginx configuration file. Nginx needs the correct configuration to map URLs to the filesystem paths where your media files are stored. Let’s address the problem and refine your configuration:

1. Issue with Nginx location Block

The = operator in Nginx location directives means “exact match,” so the line:

location = /media/ {
    root /home/syed/btawebsite;
}

only matches requests that are exactly /media/. It will not match requests for files inside /media/, such as /media/resumes/myfile.pdf.

2. Correct Configuration

To serve media files correctly, you should use a prefix match for /media/:

location /media/ {
    root /home/syed/btawebsite;
    autoindex on;  # Optional: Enables directory listing for debugging purposes
}

The absence of = ensures that requests for /media/resumes/myfile.pdf will match and serve the corresponding file located in /home/syed/btawebsite/media/resumes/myfile.pdf.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.