'Websocket 502 Bad Gateway

I've two container wsgi and asgi.

wsgi server is running on 127.0.0.8000:

gunicorn app.wsgi:application --bind 127.0.0.1:8000

Also asgi server is running on 127.0.0.1:8001 using of daphne:

daphne -b 127.0.0.1 -p 8001 app.asgi:application

I have a websocket request like this:

wss://app.example.com/ws/chat/f770eef/

But unfortunately these errors occur in the system:

i) nginx log says:

2022/05/22 13:15:29 [error] 463129#463129: *9 upstream prematurely closed connection while reading response header from upstream, client: 11.198.111.11, server: app.example.com, request: "GET /ws/chat/f770eef/

ii) Requests do not reach daphne.


asgi.py

import os
from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings')
application = get_asgi_application()

nginx cofing:

server {
listen 443;
listen [::]:443;
server_name app.example.com;
root /var/www/html;
...
location /ws/ {
    proxy_pass http://127.0.0.1:8001;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_redirect off;
    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-Host $server_name;
    proxy_read_timeout 300s;
    proxy_connect_timeout 75s;
  }  
  ...
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source