So I'm just deploying my second website. I have a static location with css, js files and everything works alright.
Now I just realised that I also have some images uploaded by the users. Here is the model:
photo_1 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True)
photo_2 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True)
photo_3 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True)
photo_4 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True)
photo_5 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True)
photo_6 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True)
photo_7 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True)
photo_8 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True)
photo_9 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True)
The problem is that when I try to access those images with DEBUG set to True, I get this
Not Found
The requested resource was not found on this server.
I found on the internet that setting Debug to True makes django not serve the files anymore. So I have to configure nginx to serve those.
How can I do that?
I have to make it also serve the default /static/ folder, and these photos which don't have a fixed name. (%Y/%m/%d).
The absolute path would be something like /etc/USERNAME/main-website/media/photo/%Y/%m/%d.
Here is my nginx file from sites-enabled.
server {
server_name bestdavnic73.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/octavian56/main-website;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/octavian56/main-website/bestDAVNIC73.sock;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/bestdavnic73.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/bestdavnic73.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
}
server {
server_name www.bestdavnic73.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/octavian56/main-website;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/octavian56/main-website/bestDAVNIC73.sock;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/bestdavnic73.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/bestdavnic73.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
}
server {
if ($host = bestdavnic73.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name bestdavnic73.com;
return 404; # managed by Certbot
}
server {
if ($host = www.bestdavnic73.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name www.bestdavnic73.com;
return 404; # managed by Certbot
}
Can you help me fix this and also help me understand how I should approach this problem?
Thanks.