[Django] #35185: Daphne: websocket works locally with ws protocol, but it connects and quickly disconnects in production

7 views
Skip to first unread message

Django

unread,
Feb 11, 2024, 11:18:55 PMFeb 11
to django-...@googlegroups.com
#35185: Daphne: websocket works locally with ws protocol, but it connects and
quickly disconnects in production
-------------------------------------+-------------------------------------
Reporter: mawaki | Owner: nobody
Type: | Status: new
Uncategorized |
Component: | Version: 5.0
Uncategorized |
Severity: Release | Keywords: Django, Daphne,
blocker | NGINX
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Hello,

Thanks for taking the time to read the issue below.

In fact I have been struggling for two weeks trying to resolve the issue
below.

**Issue:**
We have a Django project which uses Daphne and Django Channels. Everything
works well locally (ws protocol), but only the Http requests work in
production, but the wss requests do not work. In production, we use NGINX
and docker.

The JS console is showing connection to 'wss://domain.tld/ws/inbox/.../'
failed, and the error code is 1006.

Below is what the log shows on the server:


{{{
"**WSCONNECTING** /ws/inbox/948252324-169626-QvepuQVR2z9odML8joreJT/" - -
DEBUG Upgraded connection ['192.168.16.1', 60258] to WebSocket
INFO test: 948252324-169626-QvepuQVR2z9odML8joreJT
"**WSCONNECT** /ws/inbox/948252324-169626-QvepuQVR2z9odML8joreJT/" - -
DEBUG WebSocket ['192.168.16.1', 60258] open and established
DEBUG WebSocket ['192.168.16.1', 60258] accepted by application
DEBUG Get address info redis:6379, type=<SocketKind.SOCK_STREAM: 1>
DEBUG Getting address info redis:6379, type=<SocketKind.SOCK_STREAM: 1>
took 0.430ms: [(<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>,
6, '', ('192.168.16.2', 6379))]
DEBUG <asyncio.TransportSocket fd=15, family=AddressFamily.AF_INET,
type=SocketKind.SOCK_STREAM, proto=6, laddr=('192.168.16.6', 56826),
raddr=('192.168.16.2', 6379)> connected to redis:6379:
(<_SelectorSocketTransport fd=15 read=polling write=<idle, bufsize=0>>,
<asyncio.streams.StreamReaderProtocol object at 0xffff88452670>)
DEBUG Sent WebSocket packet to client for ['192.168.16.1', 60258]
DEBUG WebSocket closed for ['192.168.16.1', 60258]
"**WSDISCONNECT** /ws/inbox/948252324-169626-QvepuQVR2z9odML8joreJT/" - -
INFO close code: 1006
DEBUG Get address info redis:6379, type=<SocketKind.SOCK_STREAM: 1>
DEBUG Getting address info redis:6379, type=<SocketKind.SOCK_STREAM: 1>
took 0.523ms: [(<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>,
6, '', ('192.168.16.2', 6379))]
DEBUG <asyncio.TransportSocket fd=12, family=AddressFamily.AF_INET,
type=SocketKind.SOCK_STREAM, proto=6, laddr=('192.168.16.6', 56830),
raddr=('192.168.16.2', 6379)> connected to redis:6379:
(<_SelectorSocketTransport fd=12 read=polling write=<idle, bufsize=0>>,
<asyncio.streams.StreamReaderProtocol object at 0xffff88452f70>)

}}}


We used Daphne for http and ws requests, but due to this issue and to
better troubleshoot, we started using Gunicorn for http.

Below is our NGINX conf:


{{{
server {
listen ***:443 ssl http2;
server_name sandbox-test.festishare.com ;
error_log /var/log/apache2/domains/domain.error.log error;

ssl_certificate ***.pem;
ssl_certificate_key ***.key;
ssl_stapling on;
ssl_stapling_verify on;

# TLS 1.3 0-RTT anti-replay
if ($anti_replay = 307) { return 307 https://$host$request_uri; }
if ($anti_replay = 425) { return 425; }

location ~ /\.(?!well-known\/|file) {
deny all;
return 404;
}

location /ws/ {
proxy_pass http://localhost:6002/ws/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 86400s;
proxy_send_timeout 86400s;
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_set_header Accept-Encoding gzip;
}

location / {
proxy_pass http://localhost:4000/;
}
}
}}}

We can add more information if requested.

Thanks again for the help.
--
Ticket URL: <https://code.djangoproject.com/ticket/35185>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 12, 2024, 1:16:33 AMFeb 12
to django-...@googlegroups.com
#35185: Daphne: websocket works locally with ws protocol, but it connects and
quickly disconnects in production
-------------------------------------+-------------------------------------
Reporter: mawaki | Owner: nobody
Type: Uncategorized | Status: closed
Component: Uncategorized | Version: 5.0
Severity: Release blocker | Resolution: invalid
Keywords: Django, Daphne, | Triage Stage:
NGINX | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => invalid

Comment:

Please don't use Trac as a support channel. Also, `daphne` is a separate
package that has it's own [https://github.com/django/daphne/issues issue
tracker].

Closing per TicketClosingReasons/UseSupportChannels.
--
Ticket URL: <https://code.djangoproject.com/ticket/35185#comment:1>

Reply all
Reply to author
Forward
0 new messages