Django: failed: Error during WebSocket handshake - django

I'm getting below error
WebSocket connection to 'ws://localhost/ws/testNoti?subscribe-broadcast&publish-broadcast&echo' failed: Error during WebSocket handshake: Unexpected response code: 500
Websocket connection is broken!
supervisor conf file
[unix_http_server]
username = ubuntu
password = password
[program:uwsgi]
command=/home/ubuntu/bxd-life/venv/bin/uwsgi --ini /home/ubuntu/bxd-life/bxd/bxd.ini
autostart=true
user=ubuntu
autorestart=true
stderr_logfile = /home/ubuntu/bxd-life/logs/err.log
stdout_logfile = /home/ubuntu/bxd-life/logs/out.log
stopsignal=INT
[program:uwsgi_ws]
command = /home/ubuntu/bxd-life/venv/bin/uwsgi --http :8080 --gevent 1000 --http-websockets --workers=2 --master --module bxd.wsgi_websockets
#directory=/home/ubuntu/bxd-life/bxd
autostart=true
autorestart=true
starttries=5
user=ubuntu
environment=DJANGO_SETTINGS_MODULE='bxd.settings'
nginx conf file
upstream app_server {
server localhost:8000;
}
upstream web_socket_server {
server localhost:8080 fail_timeout=0;
}
server {
listen 80;
server_name _;
location /static/ {
alias /home/ubuntu/bxd-life/bxd/static/;
expires 30d;
}
location /ws/ {
proxy_pass http://web_socket_server;
proxy_http_version 1.1;
#proxy_redirect ws://$server_name;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
location / {
try_files $uri #proxy_to_app;
}
location #proxy_to_app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
}
}
wsgi_websockets.py
import os
import gevent.socket
import redis.connection
redis.connection.socket = gevent.socket
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "bxd.settings")
from ws4redis.uwsgi_runserver import uWSGIWebsocketServer
application = uWSGIWebsocketServer()
The above is working fine with ./manage.py runserver but not with nginx!
Any help would be very much appreciated.

Related

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;
}
...
}
I had the same problem with my setup. changing the the proxy_pass setting to HTTPS instead of http fixed it for me.
...
proxy_pass https://127.0.0.1:8001;
...
Since you're using WSS, thats WS over SSL protocol so HTTP doesn't cut it and I guess Nginx or some other service terminates the connection

Why does my websocket keep disconnecting in Django Channels App?

I have been on this for a month now without a working solution. Everything works fine in production but I have been trying to deploy my django-channels application using nginx as reverse proxy, supervisor to keep servers running, gunicorn to serve http requests and I am stuck at the weboscket request part using daphne to process http requests.
I am bindig with unix sockets: gunicorn.sock and daphne.sock
The Console returns:
WebSocket connection to 'ws://theminglemarket.com/ws/chat/undefined/' failed:
Error during WebSocket handshake: Unexpected response code: 500
My supervisor config:
directory=/home/path/to/src
command=/home/path/to/venv/bin/gunicorn_start
user=root
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/path/to/log/gunicorn/gunicorn-error.log
[program:serverinterface]
directory=/home/path/to/src
command=/home/path/to/venv/bin/daphne -u /var/run/daphne.sock chat.asgi:application
autostart=true
autorestart=true
stopasgroup=true
user=root
stdout_logfile = /path/to/log/gunicorn/daphne-error.log
Redis server is up and Running, Sure of that, using redis-server
my nginx configurations:
upstream channels-backend {
# server 0.0.0.0:8001;
server unix:/var/run/daphne.sock fail_timeout=0;
}
upstream app_server {
server unix:/var/run/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
listen [::]:80;
server_name theminglemarket.com www.theminglemarket.com;
keepalive_timeout 5;
client_max_body_size 4G;
access_log /home/path/to/logs/nginx-access.log;
error_log /home/path/to/logs/nginx-error.log;
location /static/ {
alias /home/path/to/src/static/;
# try_files $uri $uri/ =404;
}
location / {
try_files $uri #proxy_to_app;
}
location /ws/ {
try_files $uri #proxy_to_ws;
}
location #proxy_to_ws {
proxy_pass http://channels-backend;
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;
}
location #proxy_to_app {
proxy_pass http://app_server;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
# we don't want nginx trying to do something clever with
# redirects, we set the Host: header above already.
proxy_redirect off;
}
}
Please ask for any other thing needed, I'll update as quickly as I can. Thank You.
It's a chatting application, do you think I should use only Daphne, I'm considering the scalability, and that's why I used gunicorn to serve http requests. Hosting on Ubuntu Server
Try putting socket=tcp://0.0.0.0:8001 or socket=tcp://localhost:8001 in your [program:serverinterface] part of supervisord.conf. After that read your supervisor_log.log file to find out how it behaves. I had similar problems with it too. I hope that this helps. Use socket=tcp://localhost:8001 if it's inside of docker container. And make sure that nginx container is on the same docker network as that container.

django-channels nginx settings

My django app uses django-channels.
I was able to configure django to run using gunicorn and nginx.
The app run if i use python manage.py runserver and redis-server sends notification etc but i am unable to configure it using nginx.
server {
listen 80;
server_name IP;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/amir/clientcode;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/amir/clientcode/adminpanel.sock;
}
}
However when I try to configure it for django-channels it is giving me status 502
upstream channels-backend {
server localhost:8000;
}
server {
listen 80;
server_name IP;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/amir/clientcode;
}
location / {
try_files $uri #proxy_to_app;
include proxy_params;
proxy_pass http://unix:/home/amir/clientcode/adminpanel.sock;
}
location #proxy_to_app {
proxy_pass http://channels-backend;
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;
}
}
My asgi.py file
import os
import django
from channels.routing import get_default_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "adminpanel.settings")
django.setup()
application = get_default_application()
``
First of all, install Daphne in your app:
Here I use daphne==1.3.0
To start Daphne server, I use this command:
export DJANGO_SETTINGS_MODULE="config.settings"
exec daphne -b 0.0.0.0 --proxy-headers config.asgi:channel_layer
Besides Daphne, you have to start a worker:
python manage.py runworker
With this, you can use the sockets in the same URL's Projects.
Take a look in this article: https://medium.com/labcodes/introduction-to-django-channels-d1047e56f218
Regards

Trouble with deploy django channels using Daphne and Nginx

I got a 502 error when I'm trying to open a website. I used the instructions from the official website link
Added new file lifeline.conf at /etc/supervisor/conf.d/
lifeline.conf
[fcgi-program:asgi]
# TCP socket used by Nginx backend upstream
socket=tcp://localhost:8000
# Directory where your site's project files are located
directory=/home/ubuntu/lifeline/lifeline-backend
# Each process needs to have a separate socket file, so we use process_num
# Make sure to update "mysite.asgi" to match your project name
command=/home/ubuntu/Env/lifeline/bin/daphne -u /run/daphne/daphne%(process_num)d.sock --fd 0 --access-log - --proxy-head$
# Number of processes to startup, roughly the number of CPUs you have
numprocs=4
# Give each process a unique name so they can be told apart
process_name=asgi%(process_num)d
# Automatically start and recover processes
autostart=true
autorestart=true
# Choose where you want your log to go
stdout_logfile=/home/ubuntu/asgi.log
redirect_stderr=true
Setup nginx conf
upstream channels-backend {
server localhost:8000;
}
server {
listen 80;
server_name staging.mysite.com www.staging.mysite.com;
client_max_body_size 30M;
location = /favicon.ico { access_log off; log_not_found off; }
location / {
try_files $uri #proxy_to_app;
}
location #proxy_to_app {
proxy_pass http://channels-backend;
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;
}
}
I checked the asgi log file and it contains an error .
daphne: error: the following arguments are required: application
I'm guessing a mistake in lifeline.conf.
I am assuming you are not passing asgi application to daphne, because configuration you pasted in question has missing line. You have to pass it correctly. Assuming you have conf package with asgi.py module inside it containing asgi application instance, you have to do
command=/home/ubuntu/Env/lifeline/bin/daphne -u /run/daphne/daphne%(process_num)d.sock conf.asgi:application
conf.asgi:application should be at the end.

Cannot deploy django-channels in production

I am trying to deploy django-channels in production using Gunicorn,Nginx,Postgres and Supervisor.Though i have been able to serve http requests properly but i cannot configure websocket configuration
Here is nginx configuration
upstream app_server {server unix:/home/datasleek/tracker/run/gunicorn.sock fail_timeout=0;}
upstream websocket {server ip-address:80;}
server
{
listen 80;
server_name ip-address;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ { alias /home/datasleek/tracker/staticfiles/; }
location /media/ { alias /home/datasleek/tracker/media/; }
client_max_body_size 4G;
access_log /home/datasleek/tracker/logs/nginx-access.log;
error_log /home/datasleek/tracker/logs/nginx-error.log;
location /ws/ {
proxy_pass http://websocket;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location / {
proxy_pass http://app_server;
proxy_set_header X-Forwarded-For
$proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
}
}
This is supervisor configuration
[program:tracker]
command=/home/datasleek/trackervenv/bin/gunicorn_start
user=datasleek
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/home/datasleek/tracker/logs/gunicorn.log
[program:serverinterface]
directory=/home/datasleek/tracker/
command= /home/datasleek/trackervenv/bin/daphne -b 0.0.0.0 -p 80 tracker.asgi:channel_layer
autostart=true
autorestart=true
stopasgroup=true
user=datasleek
stdout_logfile = /home/datasleek/tracker/logs/daphne.log
redirect_stderr=true
#[program:tracker_asgi_daphne]
#directory=/home/datasleek/tracker/
#command=/home/datasleek/trackervenv/bin/daphne -u /home/datasleek/tracker/daphne.sock --root-path= home/datasleek/tracker tracker.asgi:channel_layer
#stdout_logfile = /home/datasleek/tracker/logs/daphne.log
[program:tracker_asgi_workers]
command=/home/datasleek/trackervenv/bin/python /home/datasleek/tracker/manage.py runworker
stdout_logfile = home/datasleek/tracker/logs/worker.log
process_name=asgi_worker%(process_num)s
numprocs=3
environment=LANG=en_US.UTF-8,LC_ALL=en_US.UTF-8 ; Set UTF-8 as default encoding
autostart=true
autorestart=true
redirect_stderr=True
stopasgroup=true
These are some websocket paths that i am trying to connect
ws://ip-address/agent-presence/
ws://ip-address/stream/
And i am getting the following error;
websocketbridge.js:118 WebSocket connection to 'ws://ip-address/agent-presence/' failed: Error during WebSocket handshake: Unexpected response code: 404
P.S: I know that questions about this error have been asked multiple times but i am really helpless to get out of this problem. I have been trying since last week but could not solve this problem despite applying different methods from google and youtube.