Django Gunicorn not load static files
Asked Answered
N

4

19

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

Neils answered 24/11, 2013 at 13:48 Comment(1)
Possible duplicate of Django static files under gunicornParenthood
U
24

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

Urdu answered 27/11, 2013 at 13:53 Comment(5)
Not really an answer. I think the originator wants to know, as do I, how to get gunicorn to serve these static files. Also it is not an answer because it does not give a reasonably full nginx configuration for working with gunicorn.Lipchitz
Where should I paste this?Admeasure
Where we should add these configurations?Brie
@Brie Try sudo nano /etc/nginx/sites-available/myprojectUrdu
I found this to be more helpful https://mcmap.net/q/187549/-how-to-make-django-serve-static-files-with-gunicornDirections
G
4

Gunicorn is only responsible for serving the django aspect of your site.

The static files need to be served by nginx. See: How exactly do I server static files with nginx and gunicorn for a Django app?. This should be configured in the nginx.conf file. If you post it here we can have a look at it.

Gamali answered 24/11, 2013 at 14:56 Comment(1)
I followed this guide goo.gl/wrRJPa and i add my site to the site-available of nginx (code: dpaste.de/q5Qa) and i have not changed nginx.conf file in according to the guide.Neils
S
0

Gunicorn reloads static content on recent versions (before December 27, 2023).

There was an issue about reload-extra-file that Gunicorn maintainers solved recently (December 27, 2023).

One question was made on Github and was about json files: Opened issue.

The --reload-extra-file parameter intent to reload extra files when they are changed, besides Python files (as made by --reload = True). One of the team decided to open a pull request to solve the problem: Opened pull.

Thus, since recent versions, maybe after the 20.1.x, there is the option below (my case example on a gunicorn.config.py):

reload_extra_file = ['static/css/*.css', 'static/js/*.js', 'templates/*.html']

There is another detail that cause some problems to me. The documentation has a reference to reload-extra-files (on plural). But the right parameter is on singular: reload-extra-file as on documentation example, just below the reference.

It's worth to note that workers can cause some confusion as consequence from delays.

updated March, 28th 2023

I'm giving up to use gunicorn for development environment. After reading this, my environment wasn't working as expected yet.

Surrealism answered 26/3, 2024 at 18:29 Comment(0)
F
-1

I have got this issue before. Try set static path in your Nginx configuration then grant permission of your project.

sudo chmod -R 777 /webapps/yourweb/

Try starting your server again. Hope it help.

Fain answered 2/12, 2016 at 7:58 Comment(1)
777 you kidding me?Ere

© 2022 - 2025 — McMap. All rights reserved.