By aminsyed
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.
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.
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!
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:
location BlockThe = 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.
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.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.