By gouskova
Trying to deploy a Django/Nginx/Gunicorn project, I cannot resolve a 502 Bad Gateway issue. Looking at nginx logs, I see “104: unknown error” and “111: unknown error”-- which seems kind of uninformative. How do I debug this further?
p.s. the most recent error recorded goes like this: "*28 connect() to unix:/home/username/gunicorn.socket failed (2: No such file or directory) while connecting to upstream, client: IP_ADDRESS, server: _, request: “GET / HTTP/1.1”, upstream: “http://unix:/home/username/gunicorn.socket:/”, host: “MY_DROPLET_IP_ADDRESS:80” "
I can post some details of my gunicorn.service and nginx configuration if this isn’t enough… been fighting with this for hours, coming from Apache2 and WSGI it’s a lot more moving pieces.
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!
Hi there,
Sounds like your Gunicorn socket isn’t being created or Nginx isn’t pointing at it correctly.
This DigitalOcean guide on setting up Django with Postgres, Nginx & Gunicorn walks through creating the systemd socket, service files, and Nginx proxy config, double check those steps:
Look into your Gunicorn systemd
logs (e.g. journalctl -u gunicorn.socket
) and your Nginx error logs, you’ll usually find clues like missing socket files, permission issues, or path problems.
If everything matches the tutorial but it’s still failing, post your gunicorn.socket
, gunicorn.service
, and Nginx site config. That way the community could advise you better.
- Bobby
Heya,
connect() to unix:/home/username/gunicorn.socket failed (2: No such file or directory)
This means Nginx is trying to connect to a Unix socket at /home/username/gunicorn.socket, but that socket file doesn’t exist, which almost always means Gunicorn is either not running, crashed, or is configured to place the socket somewhere else.
Check if your gunicorn is working at all:
sudo systemctl status gunicorn
Look for signs of failure, such as exit codes or tracebacks.
You can also check the logs for more info
journalctl -u gunicorn.service --since "10 minutes ago"
Ensure that your gunicorn.service
file has the correct --bind
path:
ExecStart=/path/to/venv/bin/gunicorn \
--workers 3 \
--bind unix:/home/username/gunicorn.socket \
myproject.wsgi:application
And that /home/username/
is writable by the user running Gunicorn, and the directory exists.
If it doesn’t, either:
Create the directory (and give it proper permissions), or
Change the socket location to something like /run/gunicorn.sock
or /tmp/gunicorn.sock
Thanks, everyone–I actually had been following that tutorial, but there were some conflicts between the instructions given there and the default django_project setup that was pre-installed in the Droplet. It took a while to sort everything out but my project is up and running now! For posterity, here are the things I had to change:
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.