# Media files on production

**URL:** <https://forum.djangoproject.com/t/media-files-on-production/39007>\
**Category:** Deployment\
**Created:** [February 22, 2025, 2:07am UTC](https://forum.djangoproject.com/t/media-files-on-production/39007 "2025-02-22T02:07:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![paperplane](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/paperplane/32/27041_2.png) [@paperplane](https://forum.djangoproject.com/u/paperplane)\
**Post date:** [February 22, 2025, 2:07am UTC](https://forum.djangoproject.com/t/media-files-on-production/39007/1 "2025-02-22T02:07:52Z")

</div>

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](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

```auto
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

```auto
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!

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 22, 2025, 2:26am UTC](https://forum.djangoproject.com/t/media-files-on-production/39007/2 "2025-02-22T02:26:43Z")

</div>

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`.

---

<div class="post-metadata">

**Author:** ![paperplane](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/paperplane/32/27041_2.png) [@paperplane](https://forum.djangoproject.com/u/paperplane)\
**Post date:** [February 22, 2025, 2:47am UTC](https://forum.djangoproject.com/t/media-files-on-production/39007/3 "2025-02-22T02:47:26Z")

</div>

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](https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu), 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!
