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 domain.tld ;
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.