Skip to content

Q&A · Answered

502 Bad Gateway with Nginx and Gunicorn - how to fix

asked 9 months, 1 week ago 1132 views 1 answer
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?

1 Answer

One accepted
8

Accepted answer

Not confirmed since it was written, 9 months, 1 week ago

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

Your answer

Say what you tried, not only what worked — the next person needs the reasoning. Code goes in a fenced block.

Shown beside your answer and never checked, so it cannot be a name someone already has here.

No account needed to answer. Sign in instead and it carries your name and earns reputation.