# Serving Media files from nginx

**URL:** <https://forum.djangoproject.com/t/serving-media-files-from-nginx/30922>\
**Category:** Deployment\
**Created:** [May 7, 2024, 10:27am UTC](https://forum.djangoproject.com/t/serving-media-files-from-nginx/30922 "2024-05-07T10:27:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Okkie14](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/okkie14/32/17425_2.png) [@Okkie14](https://forum.djangoproject.com/u/Okkie14)\
**Post date:** [May 7, 2024, 10:27am UTC](https://forum.djangoproject.com/t/serving-media-files-from-nginx/30922/1 "2024-05-07T10:27:57Z")

</div>

Hi,

My django app is running on digital ocean and I am using nginx,

I cannot get my media files to my react frontend  
I get a 403 Forbidden Error, even after running the chown and chmod command

my nginx/sites-available/ScoringWiz-Backend looks like this  
location /media/ {  
alias /home/okkertjoubert/pyapps/ScoringWiz-Backend/media/;  
}

```
location /media/school_logos/ {
    alias /home/okkertjoubert/pyapps/ScoringWiz-Backend/media/school_logos/;
}

```

In my settings.py I have this  
MEDIA\_URL = ‘media/’  
MEDIA\_ROOT = os.path.join(BASE\_DIR, ‘media’)

Regards

---

<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:** [May 7, 2024, 12:10pm UTC](https://forum.djangoproject.com/t/serving-media-files-from-nginx/30922/2 "2024-05-07T12:10:49Z")

</div>

Does the nginx account have access to that directory?

That’s one of the primary reasons why I always suggest that `MEDIA_ROOT` and `STATIC_ROOT` get located outside the scope of the project directory, it makes managing permissions easier and safer.

---

<div class="post-metadata">

**Author:** ![Okkie14](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/okkie14/32/17425_2.png) [@Okkie14](https://forum.djangoproject.com/u/Okkie14)\
**Post date:** [May 8, 2024, 6:58am UTC](https://forum.djangoproject.com/t/serving-media-files-from-nginx/30922/3 "2024-05-08T06:58:18Z")

</div>

Could you please elaborate on where to store the media files?

When I ran sudo nginx -t all the test were successful. Don’t know if that matters. I have also change ownership and permissions.

---

<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:** [May 8, 2024, 12:29pm UTC](https://forum.djangoproject.com/t/serving-media-files-from-nginx/30922/4 "2024-05-08T12:29:39Z")

</div>

> [@Okkie14](#):
>
> Could you please elaborate on where to store the media files?

The precise answer is that it’s a SysAdmin-type decision, that may depend upon factors outside Django’s control.

For example, for user-uploaded files, you may want that on a volume allocated for that purpose to ensure the total quantity of space used for those files is limited; or is using a file system that may be better suited for the characteristics of the files being uploaded; or is even a file system mounted on a different server.

Or, you may want it to be in a directory that is backed-up by a different process.

Or, you may want this deployed in a Docker environment using a specific data volume for it.

Or, you may be planning to move that directory to something like an AWS S3 bucket, or deploying to a CDN, and you want to ensure that that migration is as painless as possible.

In any event, where you _don’t_ want it, is in your project.

In our case, our common standard is the `MEDIA_ROOT` is set to `/opt/media/<project_name>/`.

> [@Okkie14](#):
>
> When I ran sudo nginx -t all the test were successful.

That’s only verifying that the syntax of the conf files are correct.

What do your nginx logs show for the requests for these files?

---

<div class="post-metadata">

**Author:** ![Okkie14](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/okkie14/32/17425_2.png) [@Okkie14](https://forum.djangoproject.com/u/Okkie14)\
**Post date:** [May 10, 2024, 9:41am UTC](https://forum.djangoproject.com/t/serving-media-files-from-nginx/30922/5 "2024-05-10T09:41:40Z")

</div>

So the error logs showed Permission denied,  
The journal logs were empty

I ended up running the command chmod -R and the path/to/media as well as chown 644 path/to/media

Rebooted my entire system and restarted Nginx and now it is working

I did try to move the media files to outside of the Django directory and I got the same problem haven’t tried after the reboot.

---

<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:** [May 10, 2024, 2:02pm UTC](https://forum.djangoproject.com/t/serving-media-files-from-nginx/30922/6 "2024-05-10T14:02:28Z")

</div>

> [@Okkie14](#):
>
> I ended up running the command chmod -R and the path/to/media as well as chown 644 path/to/media

Yes, you would need to do something like this when media is outside your project directory as well.

The issue is that you’re granting nginx direct access to your project, which creates a security and system-management vulnerability.

As far as the permissions are concerned, our opinion of the best option is that you create your media directory to be owned by the uid of your Django project, with the nginx user (frequently `www-data`) as the gid for that directory, with the sticky-bit set. You then assign the permissions as 750 for that directory and its contents. (Or 640 for files)

---

<div class="post-metadata">

**Author:** ![mozby69](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mozby69/32/21436_2.png) [@mozby69](https://forum.djangoproject.com/u/mozby69)\
**Post date:** [May 22, 2024, 8:07am UTC](https://forum.djangoproject.com/t/serving-media-files-from-nginx/30922/7 "2024-05-22T08:07:21Z")

</div>

@KenWhitesell can you teach me how serve media files using apache and gunicorn with debug set to false?

---

<div class="post-metadata">

**Author:** ![leandrodesouzadev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leandrodesouzadev/32/8981_2.png) [@leandrodesouzadev](https://forum.djangoproject.com/u/leandrodesouzadev)\
**Post date:** [May 22, 2024, 11:19am UTC](https://forum.djangoproject.com/t/serving-media-files-from-nginx/30922/8 "2024-05-22T11:19:23Z")

</div>

If you need assistante for a different topic, open a new question and post what are your configurations.
