How to serve django media files via nginx ?

Here’s a example of how I have my nginx servers setup

server {
    server_name example.com www.example.com;
    location /static {
        autoindex on;
        alias /home/myusername/myproject/static/;
    }
    location /media {
        autoindex on;
        alias /home/myusername/myproject/media/;
    }
    location / {
        proxy_pass http://127.0.0.1:8000;
    }
}

I serve django with Gunicorn on localhost port 8000. (that’s what the proxy_pass is for)

The Nginx wiki example configuration may help you too. Notice in their static file serving they specify allowed filetypes and use ‘root’ instead of ‘alias’ but they are similar.

This ServerFault question may help.

Leave a Comment