Static files not loading nginx, gunicorn

Hello once again Django community,

I am trying to deploy a project into production using nginx and gunicorn. I have created another test project called donuts for lack of a better name to troubleshoot the problem. Still getting the same results with the test project - gunicorn serving the application but nginx not serving static css files. I will include the details below… Any assistance would be appreciated.

PS - static files are displayed correctly with css via runserver but not with nginx/gunicorn

Getting the following nginx /var/log/nginx/error_log …

2024/07/30 21:03:14 [error] 6967#6967: *1 open() "/home/marc/webdev/donuts/static/main/css/main.css" failed (13: Permission denied), client: 192.168.1.1, server: mmbella.org, request: "GET /static/main/css/main.css HTTP/1.1", host: "mmbella.org", referrer: "http://mmbella.org/main/"

Permissions on css file are good for read…

-rw-r--r-- 1 marc marc 52 Jul 30 20:59 /home/marc/webdev/donuts/static/main/css/main.css

Thanks

Marc

SETTINGS

STATIC_DIR = os.path.join(BASE_DIR,"static")
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')

NGINX

server {
listen 80;
server_name mmbella.org;

location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
    root /home/marc/webdev/donuts;
}

location /media/ {
root /home/marc/webdev/donuts;
}


location / {
    include         proxy_params;
    proxy_pass      http://unix:/run/gunicorn.sock;
}
}

  1. Are you sure that your permissions are correct?

The permissions of the directory containing the static files, not just the file itself. Nginx needs to have read access to the directories that contain the static files.
Assuming you are on linux:

Try this:

sudo chmod 755 /home/marc
sudo chmod 755 /home/marc/webdev
sudo chmod 755 /home/marc/webdev/donuts
sudo chmod 755 /home/marc/webdev/donuts/static

  1. Did you try something like this: gunicorn --bind 0.0.0.0:8000 myproject.wsgi ?

  2. Where is your gunicorn settings file?

  3. Paste here the output of : sudo systemctl status gunicorn

that reason by nginx use “www-data” user.
my solution is add www-data add in ubuntu group.

$ sudo gpasswd -a www-data ubuntu
$ sudo systemctl restart nginx

The reason for not changing the user used in nginx is to manage access rights separately.
I use the method of adding www-data to the Ubuntu group and then taking away the group’s write permission.

For more details, please check the article I wrote.

Thank you I will take a look at the groups

Hello Anefta,

I neglected to look at the prior directory permissions. Your recommendations worked. Thank you so much.

1 Like