django : Serving static files through nginx django : Serving static files through nginx nginx nginx

django : Serving static files through nginx


I think using root in location block is incorrect. I use alias and it works fine, even without re-configuring django.

# django settings.pyMEDIA_URL = '/static/'# nginx server configserver {       ...    location /static {            autoindex on;            alias /opt/aa/webroot/;        }}

Hope this makes things simpler.


  1. server_name must match hostname in link/script URLs. Either declare your configuration as default for this interface:port pair (listen 8000 default)
  2. Nginx must listen on the interface where your host's IP is bound (seems ok in your case)


I also struggled with this. However, following trick worked for me:

server {        listen   8000;        server_name  localhost;     access_log  /var/log/nginx/aa8000.access.log;         error_log  /var/log/nginx/aa8000.error.log;           location / {              index  index.html index.htm;           }         location ^/static/ {            autoindex on;            root   /opt/aa/webroot/;         }     } 

I just marked static as a regex with ^ and nginx started serving static files. No modification on Django side was needed.