[Answered ]-Cannot server static files with nginx in django

2đź‘Ť

You seem to have guessed what should work for nginx, but don’t understand how alias and root work:

root sets the parent directory for the location. A physical directory with the same name must exist in the parent directory:

root    /var/www
directory     |----- /static
file                    |----- image.jpg

location /static with root /var/www will now serve files from /var/www/static. The URL http://example.com/static/image.jpg will serve /var/www/static/image.jpg.

Same structure, but location /s with alias /var/www/static will now serve files from /var/www/static. The URL http://example.com/s/image.jpg will serve /var/www/static/image.jpg.

So your simple config would be:

server {
    root /var/www; # Set root at server level

    location /static {
        expires max;
    }

    location / {
        uwsgi_pass django;
    }
 }

I don’t know what you need the regex for. If you use Django’s static tag in your templates consistently, there is no need to match “/images” and so forth.

And finally, since static is in the regex and regex patterns have priority over string matches, all your references to /static/ in the URL are being looked for in app-backend/static/static.

👤user1600649

Leave a comment