Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Gunicorn not load static files

i'm trying to deploy my django project with gunicorn and nginx, but i need some help. when i code gunicorn myproject.wsgi:application I manage to see my website in the localhost page but without any css. Why gunicorn does not load my css files that are into the static folder of my project?

Guinicorn_start script: https://dpaste.de/TAc4 Gunicorn output: https://dpaste.de/C6YX

like image 530
Anubis4815 Avatar asked Nov 24 '13 13:11

Anubis4815


People also ask

Can Uvicorn serve static files?

Uvicorn doesn't seem to support static file serving, so you might need something else too (see e.g. https://www.uvicorn.org/deployment/#running-behind-nginx).

How do you serve static files in Django in production?

Serving static files in production. The basic outline of putting static files into production consists of two steps: run the collectstatic command when static files change, then arrange for the collected static files directory ( STATIC_ROOT ) to be moved to the static file server and served.

Does Gunicorn do load balancing?

Gunicorn relies on the operating system to provide all of the load balancing when handling requests.


1 Answers

Gunicorn will only serve the dynamic content, i.e. the Django files. So you need to setup a proxy server such as nginx to handle the static content (your CSS files). I assume you are starting Gunicorn the right way, so you just need to configure nginx to serve the static files. You can use a configuration like the following, where you just need to change the path to your static files:

server {
    listen       80;
    server_name  localhost;

    location / {
        root   html;
        index  index.html index.htm;
        proxy_pass http://127.0.0.1:8000;
    }
    location /static {
        autoindex on;
        alias /path/to/staticfiles;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}

Even if this configuration is setup, you have to call "./manage.py collectstatic" to make your css work

like image 157
aldo_vw Avatar answered Oct 11 '22 13:10

aldo_vw