nginx uwsgi websockets 502 Bad Gateway upstream prematurely closed connection while reading response header from upstream nginx uwsgi websockets 502 Bad Gateway upstream prematurely closed connection while reading response header from upstream nginx nginx

nginx uwsgi websockets 502 Bad Gateway upstream prematurely closed connection while reading response header from upstream


I found the issue.

My [runserver] socket (app.sock) should be pointed under upstream django and my [wsserver] socket (django.sock) should be pointed under location /ws/ like so:

upstream django {    server unix:/opt/django/app.sock;}server {    listen 80 default_server;    charset utf-8;    client_max_body_size 20M;    sendfile on;    keepalive_timeout 0;    large_client_header_buffers 8 32k;location /media  {    alias /opt/django/app/media/media;  }location /static {    alias /opt/django/app/static;}location / {    include /opt/django/uwsgi_params; }location /ws/ {        proxy_http_version 1.1;        proxy_set_header Upgrade $http_upgrade;        proxy_set_header Connection "upgrade";        proxy_pass http://unix:/opt/django/django.sock;        proxy_buffers 8 32k;        proxy_buffer_size 64k;    }}


I had same issue but it wasn't my NGINX configuration, it was my UWSGI processes causing timeout errors when I posted JSONs from client side to server. I had processes as 5, I changed it to 1 and it solved the issue. For my application, I only needed to have 1 process run at time since AWS didn't need to be overloaded with multiple processes.

Here is the working UWSGI configuration ini file that solved the timeout issue and thus the 502 gateway issue.

autoboot.ini

#!/bin/bash[uwsgi]socket          = /tmp/app.sockmaster          = truechmod-socket    = 660module          = app.wsgichdir           = home/appclose-on-exec = true # Allow linux shell via uWSGIprocesses = 1threads = 2vacuum = truedie-on-term = true

Here's my nginx config too.

nginx.conf

# the upstream component nginx needs to connect toupstream django {    server unix:///app/tmp/app.sock; # for a file socket    # server 127.0.0.1:6000; # for a web port socket (we'll use this first)}# configuration of the serverserver {    # the port your site will be served on    listen      80;    # the domain name it will serve for    server_name XXX.XXX.XX.X #actual IP in here    charset     utf-8;    # max upload size    client_max_body_size 75M;   # adjust to taste    error_log /var/log/nginx/error.log;    access_log /var/log/nginx/access.log;    # Finally, send all non-media requests to the Django server.    location / {        uwsgi_pass  django;        include     uwsgi_params;    }    location /static {        autoindex on;        alias app/static; # your Django project's static files - amend as required    }    error_page 502 /502.html;    location = /502.html {        alias app/templates/502autoreload.html;    }    client_body_timeout 100s;    uwsgi_read_timeout 500s;    keepalive_timeout 300;}