Ws_safe_read: Web socket closed abruptly

We have a problem when we proxy the asterisk socket via NGINX. After 30 seconds the connection closes and reopens. (it almost looks like this is happening when the UA is trying to send a keep alive?)

When we connect directly to the socket everything works fine (wss://fqdn:2053/ws) and the connection does not close

Running Asterisk 18.13.0

Error
[Oct 24 22:44:25] WARNING[13858]: res_http_websocket.c:559 ws_safe_read: Web socket closed abruptly

http.conf

[general]
enabled=yes
bindaddr=127.0.0.1
bindport=8088
tlsenable=yes
tlsbindaddr=0.0.0.0:2053
tlscertfile=/etc/letsencrypt/live/xxx/fullchain.pem
tlsprivatekey=/etc/letsencrypt/live/xxx/privkey.pem

nginx

# the server
server {
    listen 443 ssl http2; 
    server_name xxx.xxx;
  
    ssl_certificate /etc/letsencrypt/live/xxx/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/xxx/privkey.pem;

    client_max_body_size 100M;
    
    root   /var/www/xxx/public_html;
    index  index.php index.html index.htm;
	
    #root
    location / {
	try_files $uri $uri/ /index.php$is_args$args;
    }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    # asterisk web socket
    location /asteriskWss {
        proxy_pass http://localhost:8088/ws;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header Host $host;
     }
}

jssip log

janus*CLI> pjsip set logger on
PJSIP Logging enabled
<--- Received SIP request (625 bytes) from WS:127.0.0.1:52586 --->
REGISTER sip:xxx.xxx.xxx SIP/2.0
Via: SIP/2.0/WSS plmg4iv11lub.invalid;branch=z9hG4bK8761157
Max-Forwards: 69
To: <sip:D0F460EC-E02D-47EF-B39F-88BC8DE7DFB0@xxx.xxx.xxx>
From: <sip:D0F460EC-E02D-47EF-B39F-88BC8DE7DFB0@xxx.xxx.xxx>;tag=va3pjiqq2i
Call-ID: ilds10applhvbedk3itc44
CSeq: 1 REGISTER
Contact: <sip:4h42agdb@plmg4iv11lub.invalid;transport=ws>;+sip.ice;reg-id=1;+sip.instance="<urn:uuid:3f1298e7-d841-47b3-9da6-7365610ccf91>";expires=600
Expires: 600
Allow: INVITE,ACK,CANCEL,BYE,UPDATE,MESSAGE,OPTIONS,REFER,INFO,NOTIFY
Supported: path,gruu,outbound
User-Agent: JsSIP 3.9.1
Content-Length: 0

<--- Transmitting SIP response (529 bytes) to WS:127.0.0.1:52586 --->
SIP/2.0 401 Unauthorized
Via: SIP/2.0/WSS plmg4iv11lub.invalid;rport=52586;received=127.0.0.1;branch=z9hG4bK8761157
Call-ID: ilds10applhvbedk3itc44
From: <sip:D0F460EC-E02D-47EF-B39F-88BC8DE7DFB0@xxx.xxx.xxx>;tag=va3pjiqq2i
To: <sip:D0F460EC-E02D-47EF-B39F-88BC8DE7DFB0@xxx.xxx.xxx>;tag=z9hG4bK8761157
CSeq: 1 REGISTER
WWW-Authenticate: Digest realm="asterisk",nonce="1666644205/a7c2ea8aaf24f8382180b40f795e4e5d",opaque="4373bc042c6b62a0",algorithm=MD5,qop="auth"
Server: Asterisk PBX 18.13.0
Content-Length:  0


<--- Received SIP request (926 bytes) from WS:127.0.0.1:52586 --->
REGISTER sip:xxx.xxx.xxx SIP/2.0
Via: SIP/2.0/WSS plmg4iv11lub.invalid;branch=z9hG4bK8379696
Max-Forwards: 69
To: <sip:D0F460EC-E02D-47EF-B39F-88BC8DE7DFB0@xxx.xxx.xxx>
From: <sip:D0F460EC-E02D-47EF-B39F-88BC8DE7DFB0@xxx.xxx.xxx>;tag=va3pjiqq2i
Call-ID: ilds10applhvbedk3itc44
CSeq: 2 REGISTER
Authorization: Digest algorithm=MD5, username="9F37BA9D-AA0C-430E-82A5-EE26411EA559", realm="asterisk", nonce="1666644205/a7c2ea8aaf24f8382180b40f795e4e5d", uri="sip:xxx.xxx.xxx", response="fb3dfd9fb7640385bed009dc78389c71", opaque="4373bc042c6b62a0", qop=auth, cnonce="2gttba01tied", nc=00000001
Contact: <sip:4h42agdb@plmg4iv11lub.invalid;transport=ws>;+sip.ice;reg-id=1;+sip.instance="<urn:uuid:3f1298e7-d841-47b3-9da6-7365610ccf91>";expires=600
Expires: 600
Allow: INVITE,ACK,CANCEL,BYE,UPDATE,MESSAGE,OPTIONS,REFER,INFO,NOTIFY
Supported: path,gruu,outbound
User-Agent: JsSIP 3.9.1
Content-Length: 0

<--- Transmitting SIP response (495 bytes) to WS:127.0.0.1:52586 --->
SIP/2.0 200 OK
Via: SIP/2.0/WSS plmg4iv11lub.invalid;rport=52586;received=127.0.0.1;branch=z9hG4bK8379696
Call-ID: ilds10applhvbedk3itc44
From: <sip:D0F460EC-E02D-47EF-B39F-88BC8DE7DFB0@xxx.xxx.xxx>;tag=va3pjiqq2i
To: <sip:D0F460EC-E02D-47EF-B39F-88BC8DE7DFB0@xxx.xxx.xxx>;tag=z9hG4bK8379696
CSeq: 2 REGISTER
Date: Mon, 24 Oct 2022 20:43:25 GMT
Contact: <sip:4h42agdb@plmg4iv11lub.invalid;transport=ws>;expires=599
Expires: 600
Server: Asterisk PBX 18.13.0
Content-Length:  0

[Oct 24 22:44:25] WARNING[13858]: res_http_websocket.c:559 ws_safe_read: Web socket closed abruptly

Other topic on the forums is also referring to this error Websocket closed abruptly

But this is happening before SIP traffic is flowing. Our problem occurs after a successful registration

Any idea what could cause this problem ?

WebSockets are still affected by proxy_read_timeout which defaults to 60 seconds. This means that if you have an application using WebSockets but not sending any data more than once per 60 seconds you either need to increase the timeout or implement a ping message to keep the connection alive.

I would increase the timeout to about 150 seconds, and implement a qualify of about 120 seconds. (leaving you with a buffer of 30 seconds.)

1 Like

Thank you for the suggestion. After testing the qualify increase it worked (https://wiki.kolmisoft.com/index.php/How_to_change_Qualify_packets_frequency)

The other one is adding proxy_read_timeout 150s; to the nginx configuration. Which also solves the problem;

    # asterisk web socket
    location /asteriskWss {
        proxy_pass http://localhost:8088/ws;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header Host $host;
        proxy_read_timeout 150s;
    }

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.