Fix Nginx Not Connecting to Gunicorn (Connection Refused)
Learn how to troubleshoot and fix the 'Connection Refused' error when Nginx fails to connect to Gunicorn. This guide covers common causes and solutions to get your web application running smoothly.
If you're seeing errors like:
connect() failed (111: Connection refused)- Nginx returns 502 Bad Gateway
- Your Django app is not reachable
👉 Then Nginx cannot connect to Gunicorn.
This guide will help you identify and fix the issue step-by-step.
⚡ Quick Fix (Try This First)
Run:
sudo systemctl restart gunicorn
sudo systemctl restart nginx
Then reload your site.
👉 If still broken, continue below.
🧠 What “Connection Refused” Means
This error means:
Nginx tried to connect to Gunicorn, but nothing was listening
Common reasons:
- Gunicorn is not running
- Wrong port/socket
- Misconfiguration
🧪 Step-by-Step Diagnosis
Follow these steps carefully.
1. Check if Gunicorn is running
sudo systemctl status gunicorn
Expected:
active (running)
If NOT running:
Start it:
sudo systemctl start gunicorn
2. Check Gunicorn logs
journalctl -u gunicorn --no-pager -n 50
Look for:
- Python errors
- Import failures
- Missing packages
3. Check what Gunicorn is listening on
If using TCP:
ss -tulnp | grep 8000
Expected:
LISTEN 0 128 127.0.0.1:8000
If using socket:
ls /var/www/myproject/
Look for:
gunicorn.sock
4. Check Nginx configuration
Open:
sudo nano /etc/nginx/sites-available/myproject
For TCP setup:
proxy_pass http://127.0.0.1:8000;
For socket setup:
proxy_pass http://unix:/var/www/myproject/gunicorn.sock;
👉 These must match Gunicorn exactly
5. Test Gunicorn directly
curl http://127.0.0.1:8000
If fails:
- Gunicorn is not running correctly
If works:
- Problem is Nginx configuration
6. Restart Nginx
sudo systemctl restart nginx
Then check your site again.
7. Check Nginx logs
sudo tail -f /var/log/nginx/error.log
Look for:
- connection refused
- no such file
- permission denied
🔥 Common Causes (and Fixes)
🔴 Gunicorn not running
Fix:
sudo systemctl start gunicorn
sudo systemctl enable gunicorn
🔴 Wrong port or socket
Mismatch between:
- Gunicorn bind
- Nginx proxy_pass
Fix by aligning both
🔴 Gunicorn crashed
Check:
journalctl -u gunicorn
Common issues:
- Syntax errors
- Missing dependencies
🔴 Socket file missing
Fix:
- Ensure Gunicorn creates socket
- Check working directory
🔴 Permission denied on socket
Fix:
sudo chown -R www-data:www-data /var/www/myproject
sudo chmod 755 /var/www/myproject
🔴 Firewall blocking connection
If using TCP:
sudo ufw allow 8000
🧠 Debugging Tips
Check listening ports
ss -tulnp
Check Gunicorn process
ps aux | grep gunicorn
Restart everything cleanly
sudo systemctl restart gunicorn
sudo systemctl restart nginx
✅ Quick Fix Checklist
Compare your socket setup with the full deploy guide and check the dedicated 502 Bad Gateway guide if the error changes.
🔗 Related Guides
❓ FAQ
What causes “connection refused”?
- Gunicorn not running
- Wrong port/socket
- Misconfiguration
Should I use socket or TCP?
- Socket → better performance
- TCP → easier debugging
Why does it work locally but not in production?
Because:
- Production uses Nginx + Gunicorn
- Local uses Django dev server
🎯 Final takeaway
“Connection refused” means:
Nginx cannot reach Gunicorn
Fix it by checking:
- Gunicorn status
- Ports/sockets *Configuration alignment
If you deploy often…
A repeatable, tested setup saves hours of debugging.