502 Bad Gateway with Nginx and Gunicorn - how to fix
Answered
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
Asked by
web_developer
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
Silver
•
415 rep
Your Answer
You need to be logged in to answer questions.
Log In to Answer