Nginx does not load css files

August 10, 2018 501 views
Django Nginx Ubuntu 16.04

I made a new Project in Django. On my local Computer it is working just fine so I tried to make it public here on DigitalOcean. I found a Tutorial which I followed. Now everything works just fine, but one little thing. Somehow the static CSS-files are not loading, which is really strange because the static images and javascript files are loading. In addition the admin page CSS files are all loaded correctly. I looked up the files on the server, which i uploaded via SFTP and all the static CSS files are on the correct place. I think it could be an Nginx Problem, but I am not sure, beacuse i am new to Nginx. I asked this Question on 2 Websites now and nobody seems to be able to help me. I hope someone can help me here.

(Projectname: bhitweb2)
static files settings.py:

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

STATIC_FILES = [
    os.path.join(BASE_DIR, 'accounts/static/'),
    os.path.join(BASE_DIR, 'daten/static/'),
]

My Nginx:

server {
    listen 80;
    server_name <myserverip>;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
       root /home/djangodeploy/bhitweb2;
    }

    location /media/  {
       root /home/djangodeploy/bhitweb2;
    }

    location /css {
       root /home/djangodeploy/bhitweb2/static;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/djangodeploy/bhitweb2/bhitweb2.sock;
    }
}

error Message of every CSS file

<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.10.3 (Ubuntu)</center>
</body>
</html>

link to any stylesheet in HTML:

{% load static %}
<link rel="stylesheet" type="text/css" media="screen" href="{% static 'css/main.css' %}/" />
1 Answer

Change this block

location /css {
       root /home/djangodeploy/bhitweb2/static;
    }

to this one

location /static {
       root /home/djangodeploy/bhitweb2/static;
    }

Then restart Nginx and it should work.

Hope this helps

Have another answer? Share your knowledge.