Django + gunicorn + nginx website not serving static css, js and images

Hello I am having the same issue here, I have tried everything I have found online. The only thing loading is the HTML. I was running it on my dev PC with whitenoise, DEBUG disabled, everything works properly. Then I uploaded it to a Ubuntu server on the cloud and configured it with gunicorn and nginx. The website runs, just not loading the images, css and js files (static files). I don’t know what else to do.

in the ubuntu VPS
Static files are living at /var/www/site/static/website

Ran the collectstatic and moved all static files to the above location

(env) ubuntu@localhost:~$ python project/manage.py collectstatic

You have requested to collect static files at the destination
location as specified in your settings:

/var/www/site/static

This will overwrite existing files!
Are you sure you want to do this?

Type ‘yes’ to continue, or ‘no’ to cancel: yes

125 static files copied to ‘/var/www/site/static’.

my static tag in the template
{% load static %} at the begining
ex: href=“{% static ‘website/images/logo.png’ %}”

settings.py
STATIC_URL = ‘/static/’
STATIC_ROOT = ‘/var/www/site/static/’

my nginx config for this site
server {
listen 80;

server_name website.com;

location = /favicon.ico { access_log off; log_not_found off; }

location /static/ {
root /var/www/site/static/website/;
}

location /media/ {
root /var/www/site/static/website/;
}

location / {

include proxy_params;

proxy_pass http://unix:/run/site.sock;

}

}

First, a side note: When you’re posting code (or config files) here, please enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted.

You say:

However, you show:

as:

But then you have:

additionally, you show:

You really don’t want this. You want to ensure that your media files are stored in a different directory than your static files.

Also, look at your nginx error log - it’ll show you what’s trying to be retrieved.

I removed the location /media/ as I only need static files. I fixed the location /static to the below inside the static folder I created a folder website for the app. and in my html code I have the static coded like in the below. it works beautifully with whhitenoise on a Windows PC, but when using a Linode ubuntu VPS with nginx it does not server the css, js and images.

changed static files location

root /home/user/project/static/;
}

HTML static
<link href="{% static 'website/css/style.css' %}" rel="stylesheet" />

project structure
parent folder/project, manage.py, etc
project/settings.py, etc
static/website/css, js, images
app/templates/index.html, etc

Does nginx have permissions to that directory?

(Typically, I recommend that STATIC_ROOT reside in a non-home directory, such as /var/www/html. By default, that directory is usually assigned to group www-data, but owned by the account used to run collectstatic. There are many ways to handle this, this is just one way.)

Thank you for the responses. I am new at this and still trying to figure it out.
I moved the static folder where all the css, js and images to /var/www/site/static, I then ran manage.py collectstatic and gave the prompt that the static files were moved. restarted nginx but same thing. Below is my settings.py static file config. I am still using whitenoise, not sure if this can be causing something to it. WTH am I doing wrong.

STATIC_URL = 'static/'
STATIC_ROOT = '/var/www/site/static'
STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage' 

What’s your nginx paragraph for this now?

What errors are you seeing in the nginx errors.log file?

These, also I used this tutorial to setup gunicorn and nginx

https://www.youtube.com/watch?v=koo3bF2EPqk&list=PLSZvGOR-kmtFPqRiA4CEvFhnOjr9fPZ5I&index=6&t=473s&ab_channel=SelmiTech

108.162.238.135 - - [21/Nov/2023:19:31:57 +0000] "GET /static/website/css/bootstrap.css HTTP/1.1" 403 195 "https://website.com/index.html" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML>
108.162.237.124 - - [21/Nov/2023:19:31:57 +0000] "GET /static/website/js/bootstrap.js HTTP/1.1" 403 195 "https://website.com/index.html" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,

okay got most of it to work by placing the entire project on /var/ww/
but still some of the images are not serving. I think is a permission issue. Going to looking into it more.

Please do not do this. You do not want your Django code directly visible to nginx.

ok, moved the project back to /home/username/project. moved static files(css, js and images) to /var/www/sitename/static. www-data has full permission to /var/www/sitename and /home/username/project. still missing a lot of images.

nginx sites-available site config

server {
    listen 80;

    server_name website.com;

    location = /favicon.ico { access_log off; log_not_found off; }


location /static/ {
        root /var/www/site;
}


location / {

    include proxy_params;

    proxy_pass http://unix:/run/site.sock;

    }
}

Permissions on those images, all of those are not being served, along with may more.

-rw-rw-r-- 1 www-data www-data 1727 Nov 22 04:22 SS-10.png
-rw-rw-r-- 1 www-data www-data 876 Nov 22 04:22 SS-11.png
-rw-rw-r-- 1 www-data www-data 1547 Nov 22 04:22 SS-12.png
-rw-rw-r-- 1 www-data www-data 1136 Nov 22 04:22 SS-13.png
-rw-rw-r-- 1 www-data www-data 1066 Nov 22 04:22 SS-14.png
-rw-rw-r-- 1 www-data www-data 1463 Nov 22 04:22 SS-15.png
-rw-rw-r-- 1 www-data www-data 682 Nov 22 04:22 SS-16.png
-rw-rw-r-- 1 www-data www-data 863 Nov 22 04:22 SS-17.png
-rw-rw-r-- 1 www-data www-data 2493 Nov 22 04:22 SS-18.png
-rw-rw-r-- 1 www-data www-data 3011 Nov 22 04:22 SS-19.png
-rw-rw-r-- 1 www-data www-data 1457 Nov 22 04:22 SS-1.png
-rw-rw-r-- 1 www-data www-data 3051 Nov 22 04:22 SS-20.png
-rw-rw-r-- 1 www-data www-data 2781 Nov 22 04:22 ss-21.png

Still, no. Your web server (nginx) has no need to have access to your source code at all. It communicates with your project through the socket file to your wsgi container, it does not access files directly.

What does your error log look like for the missing files? That should always be your first place to look.

And what is the full directory path for those files you listed?

108.162.238.137 - - [22/Nov/2023:15:02:44 +0000] “GET /static/website/images/ss-3.png HTTP/1.1” 404 134 “https://website.com/index.html” "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/60>108.162.237.148 - - [22/Nov/2023:15:02:44 +0000] “GET /static/website/images/ss-4.png HTTP/1.1” 404 134 “https://website.com/index.html” "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/60>108.162.237.120 - - [22/Nov/2023:15:02:44 +0000] “GET /static/website/fonts/fontawesome-webfont.ttf?v=4.7.0 HTTP/1.1” 404 134 “https://website.com/service.html” "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like >108.162.238.110 - - [22/Nov/2023:15:02:44 +0000] “GET /static/website/images/ss-5.png HTTP/1.1” 404 134 “https://website.com/index.html” "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/60>108.162.238.81 - - [22/Nov/2023:15:02:44 +0000] “GET /static/website/images/ss-6.png HTTP/1.1” 404 134

That looks more like an access.log file and not the error.log file.

(And since I think we may have been cross-posting, what is the full directory path of those files you listed?)

2023/11/22 15:51:31 [error] 10623#10623: *37 open() “/var/www/site/static/website/images/ss-8.png” failed (2: No such file or directory), client: IP, server: website.com, request: "GET /static/we>
2023/11/22 15:51:31 [error] 10623#10623: *38 open() “/var/www/site/static/website/images/ss-7.png” failed (2: No such file or directory), client: IP, server: website.com, request: "GET /static/web>
2023/11/22 15:51:31 [error] 10623#10623: *39 open() “/var/www/site/static/website/images/ss-13.png” failed (2: No such file or directory), client: IP, server: website.com, request: "GET /static/we>
2023/11/22 15:51:31 [error] 10623#10623: *40 open() “/var/www/site/static/website/images/ss-9.png” failed (2: No such file or directory), client: IP, server: website.com, request: "GET /static/web>

And what information can you get from that?

That the files are not there, but they are. I just copied the path and did an ls path and the files are showing there.

Show the commands you’ve issued and the output that you get from those commands. (Copy/paste the contents of the terminal window into your post, surrounded by lines of the three backtick - ` characters.)

Show the permissions for those files (ls -l) and for each directory up to (and including) /var/www.

ls /var/www/kas/static/website/images

Output

nginx/sites-availabe/kas

location /static/ {
        root /var/www/kas;
}

settings.py

STATIC_URL = 'static/'
STATIC_ROOT = '/var/www/kas/'
ls -l /var/www/
drwxr-xr-x  2 root     root     4096 Nov 18 05:26 html
drwxr-xr-x+ 3 www-data www-data 4096 Nov 22 14:56 kas
ls -l /var/www/kas/
drwxr-xr-x+ 4 www-data www-data 4096 Nov 22 04:22 static
ls -l /var/www/kas/static/
drwxr-xr-x+ 5 www-data www-data 4096 Nov 22 04:22 admin
drwxr-xr-x+ 6 www-data www-data 4096 Nov 22 04:22 website
ls -l /var/www/kas/static/website/
drwxr-xr-x+ 2 www-data www-data 4096 Nov 22 04:22 css
drwxr-xr-x+ 2 www-data www-data 4096 Nov 22 04:22 fonts
drwxr-xr-x+ 2 www-data www-data 4096 Nov 22 04:22 images
drwxr-xr-x+ 2 www-data www-data 4096 Nov 22 04:22 js
ls -l /var/www/kas/static/website/images/
-rw-rw-r-- 1 www-data www-data   1727 Nov 22 04:22 SS-10.png
-rw-rw-r-- 1 www-data www-data    876 Nov 22 04:22 SS-11.png
-rw-rw-r-- 1 www-data www-data   1547 Nov 22 04:22 SS-12.png
-rw-rw-r-- 1 www-data www-data   1136 Nov 22 04:22 SS-13.png
-rw-rw-r-- 1 www-data www-data   1066 Nov 22 04:22 SS-14.png
-rw-rw-r-- 1 www-data www-data   1463 Nov 22 04:22 SS-15.png
-rw-rw-r-- 1 www-data www-data    682 Nov 22 04:22 SS-16.png
-rw-rw-r-- 1 www-data www-data    863 Nov 22 04:22 SS-17.png
-rw-rw-r-- 1 www-data www-data   2493 Nov 22 04:22 SS-18.png
-rw-rw-r-- 1 www-data www-data   3011 Nov 22 04:22 SS-19.png
-rw-rw-r-- 1 www-data www-data   1457 Nov 22 04:22 SS-1.png
-rw-rw-r-- 1 www-data www-data   3051 Nov 22 04:22 SS-20.png
-rw-rw-r-- 1 www-data www-data   2781 Nov 22 04:22 ss-21.png
-rw-rw-r-- 1 www-data www-data   1646 Nov 22 04:22 ss-22.png

there are a lot more images with the same permissions, but I didn’t want to have a long post.

nginx erro log

[error] 10623#10623: *122 open() "/var/www/kas/static/website/images/ss-18.png" failed (2: No such file or directory),
[error] 10622#10622: *123 open() "/var/www/kas/static/website/images/ss-16.png" failed (2: No such file or directory)
etc

This file:

is not the same as:

File names are case-sensitive.

I had the same problem with my flask app when i was trying deployement and i solved it by changing the user in /etc/nginx/nginx.conf to your current user.
user www-data; by user your_username;