I’m trying to serve in production (not development) media files for my Django-project with a Nginx-server. The media-files are not located within the django-folder.
The folder structure is like:
|- django_user
| |- media
| |- Myproject
| |- static
| |- myproject
| |- settings.py
Nginx-Server:
upstream websocket{
server 127.0.0.1:6379;
}
server {
server_name myproject.com;
client_max_body_size 5M;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/django_user/Myproject;
}
location /media/ {
root /home/django_user/media;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/myproject.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/myproject.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
location /ws {
proxy_pass http://websocket;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
server {
if ($host = myproject.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name myproject.com
return 404; # managed by Certbot
}
Here is a part of my settings.py:
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
MEDIA_ROOT = '/home/django_user/media'
MEDIA_URL = '/media/'
Saving files in the media folder is working. I’ve set up an image field like this:
myicon = models.ImageField(default="static/images/default_icon.png" , blank=True, upload_to="images/")
I’ve checked it and the images (e.g. test.png) are correctly saved in /home/django_user/media/images. My problem is that the images in the media-folder cannot be shown on my website (myproject.com/images/test.png), it always shows me:
Not Found
The requested resource was not found on this server.
I’ve done already "manage.py collectstatic", and static files are loaded correctly. Only the media-files are not found.
I’ve also tried the following variations of my nginx-server, all without success:
location /media/ {
root /home/django_user;
}
location /media {
root /home/django_user/media/;
}
location /media {
root /home/django_user/;
}
location /media/ {
alias /home/django_user/media/;
}
Can you help me with this problem? What am I missing here or where is my mistake?
2
Answers
Try alias method:
I think you are trying with the wrong URL.
If you define:
MEDIA_URL = '/media/
andlocation /media/
Then the URL should be
myproject.com/media/images/test.png
instead ofmyproject.com/images/test.png
in both scenarios (development and production)