502 Bad Gateway on AWS ELB with Nginx + Django + Gunicorn

Summary of Issue: 502 Bad Gateway from ELB to Django app behind Nginx + Gunicorn on EC2 Environment:

Hi, I wonder if anyone can assist. I've been banging my head against a wall for over a week now, I've tried two different AI's and reviewed everything I can find here and on AWS. I have an Elastic Load Balancer (Application) and Auto Scaling Group. Everything is set up in line with best practice, html and php pages are served up fine. However django is not served to the public side of the ELB, it returns a 502 Bad Gateway.

• AWS Elastic Load Balancer (ELB) in front of EC2 instance • ELB terminates HTTPS, forwards HTTP (port 80) to Nginx on EC2 • Nginx configured as reverse proxy forwarding /app/ requests to Gunicorn via Unix socket • SELinux is set as Permissible • Django app served by Gunicorn, running on EC2 • Nginx version 1.28.0, Gunicorn serving Django app on Unix socket /tmp/gunicorn_.sock • Django app uses virtual environment with dependencies installed per deployment script Observed Behavior: • curl -vk https:///test (simple Nginx endpoint) returns HTTP 200 OK correctly • curl --unix-socket /tmp/gunicorn_.sock http:///app/ returns HTTP 200 OK correctly — Gunicorn + Django working locally on EC2 • curl -vk https:///app/ returns 502 Bad Gateway from Nginx behind ELB • /app (no trailing slash) returns 301 redirect to /app/ • /app/ returns 502 Bad Gateway • Nginx debug logs indicate connection attempts to Gunicorn socket, but no successful proxy response • Gunicorn service is running and socket permissions are set to 777 (for testing) • No errors when running Django management commands inside the virtual environment • Deployment script sets up virtualenv, installs dependencies, copies latest code — no missing dependencies reported during startup • Nginx configuration verified to proxy_pass to unix socket, but ELB → Nginx → Gunicorn flow results in 502

Additional info: • ELB is an Application Load Balancer (ALB) terminating TLS and forwarding on port 80 • Nginx proxying via proxy_pass, tried http://unix:/tmp/gunicorn_.sock:/; and http://unix:/tmp/gunicorn_.sock; • Gunicorn command line uses --bind unix:/tmp/gunicorn_.sock • Django version and Gunicorn versions are up to date

I would be hugely grateful for any pointers on what could be causing this and how to resolve.

Вернуться на верх