static files on nginx wont work

Hey,

I’m trying to deploy my website and I used an nginx server with gunicorn. The server works as expected but the static files wont work, I guess I messed something up in my settings.py

STATIC_URL = '/static/'

STATIC_ROOT = "/root/static"

I already moved my static files with collectstatic to my new directory on my server and they are in there but it doesnt work on the website.

this is in my sites-available from NGINX

location /static/ {
	root /root/static;
}

I set something up wrong but I dont know where the error is.

Check first to ensure that nginx has permissions to those files. (Likely not. Note that while you might start nginx as root, the nginx process will downgrade itself to a non-root account by default. You definitely don’t want nginx itself running as root.)

In the common case, you want to deploy your static files to a directory already owned / accessible to nginx, which usually means something under /var/www. You almost certainly don’t want to expose /root to the outside world under any circumstances.

See: Pitfalls and Common Mistakes | NGINX

Also see:
https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#root-inside-location-block

and note the distinction between what’s served with root vs alias.

2 Likes

so this is my settings.py now:

STATIC_URL = '/static/'
STATIC_ROOT = "/var/www/divusx.com/static/"

and this is my sites-avalable:

location /static/ {
	root /var/www/divusx.com/static/;
}

and after collectstatic command the files were moved to the var directory but on my site there still isnt any static content.

How can I check that nginx has the permission that it needs? What should I do that it runs correctly?

1 Like

I’m thinking in this case it’s the “root” vs “alias” situation. Change the directive from root to alias.