Serving media files using nginx

Ive been working a project and have not been able to serve media files to the production in aws. Im using nginx and gunicorn for the server. When I turned debug = false it loaded the css and static images but the images from the database didnt show up. I am using MEDIA_ROOT = BASE_DIR / ‘images’ and im pretty sure im using the propper ImageField object to add it from the admin panel. if anyone could lead me in the right direction on this it would be greatly appreciated.

Django doesn’t serve media by default. Do you have an entry in your urlpatterns for serving this media? If you’re using nginx to serve media (which will be better than having Django do it), is it pointing at the correct directory?

1 Like

Welcome @Vic-ui !

There have been multiple threads here discussing this type of issue.

Note that from the perspective of nginx, serving media files is no different from serving static files, other than you generally need a separate nginx paragraph for doing so and sometimes managing permissions takes a little more work.

In all cases, I recommend setting MEDIA_ROOT completely outside your project directory.

For us to provide more specific assistance, please post your nginx configuration for your media files, and show your intended directory structure for them. Also post your nginx access and error logs for the requests for a media file. That will get us started.

Also, see the Serving files docs. While that section is written for Apache, the concepts presented there are still valid for nginx…