Alternate between not serving static files and 502 bad gateway error for Django app

December 1, 2017 180 views
Deployment Django Nginx Ubuntu 16.04

I've been trying to get this Django website deployed for a week. However, I'm brand new to servers, the internet, Django, Nginx, etc. I'm using the Django one-click-image and I have followed all the tutorials, instructions, stackoverflow question etc. I've gotten so close to having the settings right, I can taste it.

I can get the html template deployed but it won't deploy the static files (well, the css or an img) according to many hours on the internet I'm not serving them properly. Some searches i did wanted me to adjust gunicorn.conf... but it doesn't seem to exist on my image. I'm going to give a lot more info than maybe I should sorry, but there's a lot of common mistakes and I need you to know I've already covered that territory

If I just leave the settings.py to have STATIC_URL = '/static/' my basic html template (a couple of links to other pages in the site and a title that do work) comes up but it will not load the css... or even an image in that folder when I tried that. If I give it any other STATIC related constants I get a 502 Bad Gateway.

Things I put in were like:

STATICROOT = os.path.join(os.path.dirname(BASEDIR), 'static')
STATICFILESDIRS = (
os.path.join(BASE
DIR, 'static'),
)

STATICFILES_FINDERS = ( im not typing all this out)

And every combination thereof.

my urls.py files (I have two) both import settings and include the following after the urlpatterns list: (when the constants are visible in settings):

] + static(settings.STATICURL, documentroot=settings.STATIC_ROOT)

I always run collectstatic and systemctl restart gunicorn.service after any changes.

The latest 502 gateway error produced this in the Nginx log:

2017/12/01 14:00:16 [error] 2151#2151: 1135 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 96.234.167.16, server: _, request: "GET / HTTP/1.1", upstream: "http://unix:django/unicorn.socket:/", host: "my ip*"

2017/12/02 14:00:18 [error] 2151#2151: 1135 connect() to unix:/home/django/gunicorn.socket failed (111: Connection refused) while connecting to upstream, client: 96.234.167.16, server: _, request: "GET / HTTP/1.1", upstream: "http://unix:/home/django/gunicorn.socket:/", host: "my ip*"

the first looks like a timeout, but I don't know. Please let me know if you need more information. I'm tearing my hair out.

2 Answers

If all your static files are in one directory you can tell nginx to try_files first and then proxy to your application if nothing on disk matches.

As long as your css/js are static (not generated by your django app on a per-request bassis) this should also mean your resources are served faster (nginx is better at serving static files than your django app is).

DELETE the + static(settings.STATICURL, documentroot=settings.STATIC_ROOT) thing. Your application is not supposed to serve static files. Add location entries in nginx instead BEFORE the location / enty.

    location /static {
        expires 90d;
        alias /PLACETOYOUR/static/;
    }

    location /media {
        expires 30d;
        alias /PLACETOYOUR/media/;
    }
Have another answer? Share your knowledge.