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 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 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:, 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:, 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.

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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/;

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).