static files don't work with NGINX

Hi everybody,

I clone my project on server ubuntu 22.04 from digitalocean and then I followed all the steps to configure django with postgresql, gunicorn and nginx. Finally when I open mi domain on chrome, it doesn’t show all the static files.

This is my settings.py

DEBUG = False

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

#STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
STATICFILES_FINDERS=[
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]

nginx file:

server {
    server_name 159.203.14.228 bearcreektechnology.com www.bearcreektechnology.com;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/bearcry/bcmdir;
    }

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

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/bearcreektechnology.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/bearcreektechnology.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
    if ($host = www.bearcreektechnology.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    if ($host = bearcreektechnology.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80;
    server_name bearcreektechnology.com www.bearcreektechnology.com;
    return 404; # managed by Certbot
}

I run python3 manage.py collectstatic and reset gunicorn and nginx but static files don’t work.
Any idea ?

This is one common source of problems. Having your static files in your home directory is not a good practice. You want your static files to be located in an areas generally and typically available to nginx - something like /var/www/static or /var/www/project/static depending upon whether you’re running multiple applications in this instance.

Other things to check are to verify that collectstatic has copied the files in the right directory structure to the right target and verifying that your references to those static files are coded properly using the static tag in your templates. (This is particularly useful in those cases where some files work and some don’t.)

1 Like