Easily configure a performant, secure, and stable NGINX server.Learn More
Alternate between not serving static files and 502 bad gateway error for Django app
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 = (
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: 184.108.40.206, 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: 220.127.116.11, 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.
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.×