server icon indicating copy to clipboard operation
server copied to clipboard

Nginx won't load static files. Django opens just fine

Open timonoj opened this issue 10 months ago • 5 comments

Hi guys!

I'm trying to setup my nginx for the first time. Somehow...I don't get nginx to load static content. But if I open the 8001 django URL, that one works just fine. I'm sure I must be missing something incredibly obvious, but I'm at my wits end right now, after several hours trying.

So...this is my systemd service file:

[Unit]
Description=Execute the etebase server.

[Service]
WorkingDirectory=/home/localuser/etebase
#ExecStart=/home/localuser/etebase/.venv/bin/uvicorn etebase_server.asgi:application --host 0.0.0.0 --port 8001
ExecStart=/home/localuser/etebase/.venv/bin/uvicorn etebase_server.asgi:application --uds /tmp/etebase_server.sock

[Install]
WantedBy=multi-user.target

this is my etebase-server.ini:

[global]
secret_file = secret.txt
debug = false
;Set the paths where data will be stored at
static_root = /home/localuser/etebase/static
media_root = /home/localuser/etebase/media


[allowed_hosts]
allowed_host1 = test.url
allowed_host2 = *

[database]
engine = django.db.backends.sqlite3
name = db.sqlite3

....and this is the nginx file.


# the upstream component nginx needs to connect to
upstream etebase {
    server unix:///tmp/etebase_server.sock; # for a file socket
    #server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}

# configuration of the server
server {
    # the port your site will be served on
    listen      8000;
    # the domain name it will serve for
    server_name 192.168.0.15 # substitute your machine's IP address or domain name
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    location /static/ {
        alias /home/localuser/etebase/static; # Project's static files
    }

    location / {
        proxy_pass http://etebase;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host $server_name;
    }
}

Do you guys see whatever I might have missed?

Thanks a lot!

timonoj avatar Feb 04 '25 12:02 timonoj

Hi,

in the line where you configured alias /home/localuser/etebase/static; Could you try the following instead?

root /home/localuser/etebase;

I'm not sure how "alias" behaves exactly, but I tested a bit and it seemed to cause trouble.

lfuerderer avatar Feb 04 '25 16:02 lfuerderer

Both media and or just static?

In addition to @lfuerderer's suggestion: make sure you actually generated the static files (see the README) - so that they are there. Though I think it's only ever useful for the admin panel, so in most cases it shouldn't matter for normal operations.

tasn avatar Feb 04 '25 17:02 tasn

Hi,

in the line where you configured alias /home/localuser/etebase/static; Could you try the following instead?

root /home/localuser/etebase;

I'm not sure how "alias" behaves exactly, but I tested a bit and it seemed to cause trouble.

Thanks! I tried changing it to root instead, and restarted nginx...no change. Still looks unable to reach the static content, the css and the images (but the version running from port 8001 loads fine).

Both media and or just static?

In addition to @lfuerderer's suggestion: make sure you actually generated the static files (see the README) - so that they are there. Though I think it's only ever useful for the admin panel, so in most cases it shouldn't matter for normal operations.

Yup, I did run ./manage.py collectstatic, and the files are there. /media...Should populate once I start uploading stuff calendars/contacts? I guess I didn't get to that part just yet.

timonoj avatar Feb 05 '25 02:02 timonoj

Hi, in the line where you configured alias /home/localuser/etebase/static; Could you try the following instead?

root /home/localuser/etebase;

I'm not sure how "alias" behaves exactly, but I tested a bit and it seemed to cause trouble.

Thanks! I tried changing it to root instead, and restarted nginx...no change. Still looks unable to reach the static content, the css and the images (but the version running from port 8001 loads fine).

Both media and or just static? In addition to @lfuerderer's suggestion: make sure you actually generated the static files (see the README) - so that they are there. Though I think it's only ever useful for the admin panel, so in most cases it shouldn't matter for normal operations.

Yup, I did run ./manage.py collectstatic, and the files are there. /media...Should populate once I start uploading stuff calendars/contacts? I guess I didn't get to that part just yet.

Your webserver user is probably unable to reach the directory in /home/localuser You can try to check that by issuing sudo -u your_webserver_user ls -l /home/localuser/etebase/static your_webserver_user is probably www or www-data depending on your OS.

An easy way to fix this is by putting your static dir in a reachable path, maybe /srv/http/etebase_static or /var/www/html/etebase_static, again depending on what's the recommended way on your OS.

daftaupe avatar Feb 05 '25 12:02 daftaupe

Great stuff! I think you're onto something, I"m really getting permission denied when trying to ls under www-data. I'll try to move the folders and change the location in the configs. Thanks for the help!

timonoj avatar Feb 05 '25 13:02 timonoj