Static files return 404 codes

Hi everyone I am relatively new to deploying websites with Django. I’m in the process of deploying my 2nd website on the same server as my 1st website. I’m using this tutorial: https://www.youtube.com/watch?v=Td3lirXIeRI and they show you how to use the following setup:

  • Django
  • Gunicorn
  • Nginx
  • Supervisor

My first website works perfectly fine. The static files are served and the user does not receive any 404’s in the console. I set up my 2nd website the exact same way as my first, and the user receives 404’s for the static files.

Here is some of my code for both sites:

site1.settings.py

STATIC_URL = 'static/'

MEDIA_URL = 'media/'
MEDIA_ROOT = BASE_DIR / 'media'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'site1/static/')
]

DEBUG = False

ALLOWED_HOSTS = ['www.site1.com', 'site1.com']

CSRF_TRUSTED_ORIGINS = ['https://site1.com']

site1-nginx.conf

upstream site1_app_server {
        server unix:/webapps/site1/run/gunicorn.sock fail_timeout=0;
}

server {
        server_name site1.com www.site1.com;

        access_log /webapps/site1/logs/access.log;

        location /static/ {
                alias /webapps/site1/static/;
        }

        location /media/{
                alias /webapps/site1/media;
        }

        location / {
                proxy_pass http://127.0.0.1:8000;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_redirect off;
                if (!-f $request_filename) {
                        proxy_pass http://site1_app_server;
                }
        }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/www.site1.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/www.site1.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 = site1.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot
        listen 80;
        server_name site1.com;
    return 404; # managed by Certbot
}

site2 settings.py

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'site2/static/')
]
STATIC_ROOT = BASE_DIR / "staticfiles"

DEBUG = False

ALLOWED_HOSTS = ['*']

CSRF_TRUSTED_ORIGINS = ['https://site2.com']

site2-ngnix.conf

pstream site2_app_server {
        server unix:/webapps/site2/run/gunicorn.sock fail_timeout=0;
}

server {
        server_name site2.com www.site2.com;

        access_log /webapps/site2/logs/access.log;
        error_log /webapps/site2/logs/error.log;

        location /static/ {
                autoindex on;
                alias /webapps/site2/static/;
        }

        location /media/{
                alias /webapps/site2/media;
        }
        location / {
                proxy_pass http://127.0.0.1:8000;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_redirect off;
                if (!-f $request_filename) {
                        proxy_pass http://site2_app_server;
                }
        }
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/www.site1.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/www.site1.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.site2.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


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

Let me know if you need more information. Any help would be much appreciated.

Thank you.

Please show the messages in the nginx access and error log files for the site 2 requests having errors.

site2 error log

2024/06/12 12:00:45 [error] 329005#329005: *4945 open() "/webapps/site2/static/css/style.css" failed (2: No such file or directory), client: [IP], server: site2.com, request: "GET /static/css/style.css HTTP/1.1", host: "www.site2.com", referrer: "https://www.site2.com/"

site2 access log

[IP] - - [12/Jun/2024:12:00:45 +0000] "GET /static/css/style.css HTTP/1.1" 404 196 "https://www.site2.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36"

Verify that this file exists as described, and that nginx has access to it - check the permmissions.

I found the issue. I had a typo in the site2-nginx.conf file and it was pointing to a folder that did not exist. My static files are visible now. Thanks for the help @KenWhitesell !