Media files on production

Hello everyone,

Opening a new topic because could not find one related to a deployment on production for the same case.

I’ve deployed an app running on Ubuntu 22.04 with Gunicorn and NGINX. Everything works perfectly, even the static files. Already collected static and debug is set to False.

The problem is that my app has options to upload media files like profile pictures and logos. However, when I upload a new one it will not be displayed on the pages where it is supposed to be.

The models that have these ImageField are using the usual upload_to=“my_app/media_folder/” and on my DB I can see the file was saved on a path at https://mydomain.com/media/app_name/image.png

If I check the HTML on the browser, on the element I see the same path. On my actual HTML template code I load the image using {{ object.image.url }} which on development works fine. And if I open the link on the browser I get redirected to my 404.html template.

So, I am wondering where the issue is and what I need to change for production. If it is related to something I need to add to nginx, to settings.py or change where my images get uploaded for production.

I’m not sure what info should I share but here is my settings.py config for static files and media and nginx config:

settings.py

STATIC_URL = "static/"
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')

# Media files

MEDIA_URL = "/media/"
MEDIA_ROOT = Path.joinpath(BASE_DIR, "media")

nginx conf

server {
    server_name my_site.com www.my_site.com;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /var/www/my_project;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    } 
}

Any guidance will be appreciated.

Thanks!

Welcome @paperplane !

Side note: As a general case, we recommend to always open a new topic for every issue to be resolved. It gets confusing if there are multiple conversations being discussed in the same topic, even if the symptoms are essentially identical.

The first thing is that you need an nginx configuration section for the /media/ location.

I’m also a little confused by what you’re showing here with your configuration. In the normal case, you would have your STATIC_ROOT='/var/www/my_project' to match the directory that nginx is going to search for the static files. (This allows you to use the collectstatic management command to gather the files to their accessible location.)

Third, I recommend that your MEDIA_ROOT also be set to a different directory under /var/www.

Hello @KenWhitesell,

Adding a media location in nginx indeed fixed the problem. Did that, restarted nginx and worked like a charm. Thanks!

Regarding your observation about STATIC_ROOT I got to say I really did not get that either. For the record I followed a recipe provided by DigitalOcean here, and since worked I did not think much about it but what you say makes sense.

I will try your recommendations.

Again thank you very much for your help and the recommendations!