Report this

What is the reason for this report?

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

Posted on December 1, 2017

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:

STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), ‘static’) STATICFILES_DIRS = ( 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.STATIC_URL, document_root=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.

The developer cloud

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

Start building today

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.