Server 403 error when serving static files with Nginx

Hi there,

During deployment of Django (Gunicorn+Nginx) I have encountered the following issue:

  1. Site loads correctly, and debug default page comes on. The terminal annouces the first two errors:
(django_env) user@mysite.com:$ Not Found: /error/403.html
Not Found: /favicon.ico
  1. When navigating to mysite.com/admin none of the static files are loading, and multiple “Not Found: /error/403.html” are returned on the terminal.

  2. I have checked nginx error log, finding no server 403 errors (just a warning relating ssl stapling):

2021/08/28 00:08:00 [warn] 4549#4549: "ssl_stapling" ignored, issuer certificate not found...
  1. I have checked the permissions on my static folder (/home/user/myproject/static) and are set to 755:
    Captura de pantalla 2021-08-28 011244

This is what my settings.py static section looks like:

STATIC_URL = '/static/'
STATIC_ROOT = "/home/user/myproject/static"

This is what my nginx myproject.conf file looks like

server {
        listen xxxxxxxxxx:443 ssl;
        server_name site.com;
        ssl_certificate      /usr/local/xxxxxx.crt;
        ssl_certificate_key  /usr/local/xxxxx.key;
        #access_log  /var/log/ngin/xxxxxxxxx.log main;
       
       location /static/ {
            alias /home/user/myproject/static;
        }

        location / {
                proxy_pass http://xxxxxxxx:8000/;
        }
}

User set in nginx.conf is ‘nginx’ and the rest of relevant configurations go as follow:

# Server globals
    user   nginx;
################
# Error pages
    error_page          403          /error/403.html;
    error_page          404          /error/404.html;
    error_page          502 503 504  /error/50x.html;
###############
# Wildcard include
    include       /etc/nginx/sites-enabled/myproject;

I am certain I might have missed several things here (maybe I should pay attention to the whole permission hierarchy, setting permissions elsewhere, adequately configure nginx…), forgive my limited experience.

Thank you in advance for your help!

1 Like

The issue could be caused by a lack of permissions higher up. Check the permissions at /home, /home/user, and /home/user/myproject.

But, I think you’d be better off (easier and with fewer chances of side effects) by moving STATIC_ROOT to something like /var/www/html/static.

If you need more details, check the nginx logs - don’t rely solely on the console output. You can also run nginx with more detailed logging.

3 Likes