502 Bad Gateway with Nginx and Gunicorn - how to fix

Answered
Jan 05, 2026 775 views 1 answers
1

I'm getting 502 Bad Gateway error when accessing my Django site through Nginx. Gunicorn seems to be running. How do I fix this?

W
Platinum 285 rep

1 Answer

8

502 errors usually indicate Nginx cannot communicate with Gunicorn. Here's how to fix:

Check Gunicorn is running

sudo systemctl status gunicorn
# Check if socket exists
ls -la /run/gunicorn.sock

Fix socket permissions

# In gunicorn.service
[Service]
User=www-data
Group=www-data

# Restart gunicorn
sudo systemctl restart gunicorn

Check Nginx upstream configuration

upstream django {
    server unix:/run/gunicorn.sock fail_timeout=0;
}

server {
    location / {
        proxy_pass http://django;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}
D
Answered by db_admin 1 week, 2 days ago
Silver 415 rep

Your Answer

You need to be logged in to answer questions.

Log In to Answer