Django不提供图片,因为Debug设置为True-如何让Nginx提供图片?

时间:2019-09-08 13:46:37

标签: django nginx

所以我只是在部署第二个网站。 我有一个包含CSS,JS文件的静态位置,一切正常。

现在我才意识到我也有一些用户上传的图像。 这是模型:

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)

问题是,当我尝试将DEBUG设置为True时访问这些图像时,会得到

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

我在互联网上发现将Debug设置为True会使django不再提供文件。因此,我必须配置nginx来提供这些服务。

我该怎么做?

我必须使其也提供默认的/ static /文件夹,并且这些照片没有固定名称。 (%Y /%m /%d)。

绝对路径应类似于/ etc / USERNAME / main-website / media / photo /%Y /%m /%d。

这是我启用了站点的nginx文件。

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


}

您能帮我解决这个问题,也可以帮助我了解如何解决此问题吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

对于与我一样处境的每个人-基本上,想知道如何通过nginx同时提供媒体文件和静态文件-您可以在此处找到答案:

How to serve django media files via nginx ?