Invalid websocket upgrade

27 Views Asked by At

I've set up a server with the help of NiceGUI and Nginx on a VPS. The requests are coming through a subdomain and routed correctly: The server receives the request and prints the html elements.

However, upon making use of the websockets, I can see the following error on my server's out: ERROR:engineio.server:Invalid websocket upgrade (further occurrences of this error will be logged with level INFO).

Conversely, my browser shows manager.js:108 WebSocket connection to 'wss://{I-hid-this}/_nicegui_ws/socket.io/?client_id=ce259c07-9781-4739-9faa-051f24e911bd&EIO=4&transport=websocket' failed.

The same setup, running it on_air works perfectly.

Here's my nginx config for my subdomain:

server {
    listen 80;
    server_name {I-hid-this};

    location / {
        proxy_pass http://127.0.0.1:8080;
        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-Proto $scheme;
    }
}
server{
        # SSL configuration
        listen 443 ssl;
        server_name {I-hid-this};
        ssl_certificate /etc/nginx/ssl/{I-hid-this}_ssl_certificate.cer;
        ssl_certificate_key /etc/nginx/ssl/_.{I-hid-this}_private_key.key;

        location / {
                proxy_pass http://127.0.0.1:8080;
                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-Proto $scheme;
        }

}

I tried adding sockets on extra ports, but it made no difference.

The main problem I have with this is that I can't find any specific documentation on the posted error.

Update: I logged the headers received and it's quite notable that the transport is set to websocket, however there's no HTTP_UPGRADE passed, and that's what kills the logic. Moreover, it seems my local working server recives ws as scheme whereas my non working VPS receives https. I hope this helps.

1

There are 1 best solutions below

0
j4nSolo On

I solved it by adding the following configuration to my nginx config file:

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";

thanks to this source: https://www.nginx.com/blog/websocket-nginx/