Report this

What is the reason for this report?

Nginx does not load css files

Posted on August 10, 2018

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' %}/" />


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

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

use python manage.py collectstatic to server the file. It will move the files into staticfiles and will load the staticfiles from that folder.

This happened to me and I simply had to clear my browser’s cache for it to work since everything else was properly configured.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.