0

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.

  • See the [answer here](https://stackoverflow.com/a/42110885/5198805) for the question: https://stackoverflow.com/questions/42069130/django-imagefield-upload-store-and-serve-image-in-development-server – Ryu S. Sep 08 '19 at 13:55
  • The problem is that I don't need the development server. I want to deploy it, so debug = false. – Octavian Niculescu Sep 08 '19 at 14:26

1 Answers1

0

For everyone in the same situation as me - basically, wondering how to serve both media and static files via nginx - you can find the answer here:

How to serve django media files via nginx ?