502 Bad Gateway Nginx | Django | Gunicorn on Load Balancer

I have an AWS Elastic Load Balancer (ELB) and listeners which redirect to port 80 internally (certificates and SSL termination at the ELB). I'm running nginx on the EC2 instances, along with php, gunicorn and django/python. The cluster is being set up to host multiple domain names with distinct websites/apps for each.

External http/https requests work fine for html and php, returning the pages. For django requests using sockets I'm getting a 502 Bad Gateway error externally, but internally via curl (eg. curl --unix-socket /tmp/gunicorn_.sock http:///app/) it works fine.
Externally https:///test (simple Nginx endpoint) works fine

None of the logs I can find show errors. eg:

[2025-07-31 00:05:07 +0000] [776767] [INFO] Handling signal: term
[2025-07-31 00:05:07 +0000] [776771] [INFO] Worker exiting (pid: 776771)
[2025-07-31 00:05:07 +0000] [776769] [INFO] Worker exiting (pid: 776769)
[2025-07-31 00:05:07 +0000] [776768] [INFO] Worker exiting (pid: 776768)
[2025-07-31 00:05:08 +0000] [778670] [INFO] Starting gunicorn 23.0.0
[2025-07-31 00:05:08 +0000] [778670] [INFO] Listening at: unix:/tmp/gunicorn_<domain>.sock (778670)
[2025-07-31 00:05:08 +0000] [778670] [INFO] Using worker: sync
[2025-07-31 00:05:08 +0000] [778672] [INFO] Booting worker with pid: 778672
[2025-07-31 00:05:08 +0000] [778673] [INFO] Booting worker with pid: 778673
[2025-07-31 00:05:08 +0000] [778674] [INFO] Booting worker with pid: 778674

My server conf

    server {
        listen 80;
        server_name <domain> www.<domain> localhost;

        root /mnt/efs/www/<domain>;
        index index.php index.html;

        location ~ /app/ {
            error_log /mnt/efs/log/nginx-app-debug.log debug;
            proxy_pass http://unix:/tmp/gunicorn_<domain>.sock/;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }

        location / {
            try_files $uri $uri/ /index.php?$args;
        }

        location ~ \.php$ {
            include /etc/nginx/fastcgi_params;
            fastcgi_pass unix:/var/run/php-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param SCRIPT_NAME $fastcgi_script_name;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_intercept_errors on;
        }

        location = /test {
            default_type text/plain;
            return 200 "Nginx test";
        }
    }

My gunicorn service file

[Unit]
Description=Gunicorn instance for Django app
After=network.target

[Service]
User=ec2-user
Group=ec2-user
WorkingDirectory=/mnt/efs/www-private/<domain>/<ver>
Environment="PATH=/usr/local/bin:/usr/bin:/bin"
ExecStart=/usr/local/bin/gunicorn --workers 3 --bind unix:/tmp/gunicorn_<domain>.sock hello_world.wsgi:application --access-logfile /mnt/efs/log/django_<domain>.log --error-logfile /mnt/efs/log/django_<domain>.log
Restart=always

[Install]
WantedBy=multi-user.target

Other relevant info:

  • SELinux is set to permissible
  • Gunicorn service is running and socket permissions are set to 777 (for testing)
  • No missing dependencies reported during startup

I've reviewed all the similar issues here but none of the listed solutions work. Would be grateful for any assistance!

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