wss: Error during WebSocket handshake: Unexpected response code: 200

Created on 28 Jan 2017  路  29Comments  路  Source: websockets/ws

Salutations,

I'm running a WebSocket server using this module, but am having issues connecting with certain browsers. The server is served through ssl, so I connect to it using wss.

The Issue

When I connect to the website using Google Chrome, everything works as intended. However, when I try to connect using Chromium, I get the following error:

WebSocket connection to 'wss://domain.name/' failed: Error during WebSocket handshake: Unexpected response code: 200

It should be noted that the client connects perfectly fine on my test instance, connecting to ws://localhost:8085. This seems to be an issue only with wss protocols. Obviously, using ws protocols over an ssl connection is not a viable option.

I have tried directly connecting via IP and port, but get the following error:

WebSocket connection to 'wss://ip.addr.he.re:8190/' failed: Error in connection establishment: net::ERR_CONNECTION_CLOSED

My Code

Server:

const WebSocketServer = require('ws').Server;
const wss = new WebSocketServer({
    server: server // express server object, running on port 8085
  //port: 8190 // used when testing direct connections
});

Browser:

var wss = new WebSocket("wss://domain.name");

Sidenotes

I suspect this issue is due to chromium's websocket support itself. I noticed that there is client support available. Is there a browserified version of this module available for me to load via a

All 29 comments

This is strange. Did you try with other browsers? Does it work with Firefox?
The strange thing is that you get a 200 status code. It seems that you are not hitting the WebSocket server as it will never answer with 200. Is there a proxy in the middle?

The site itself is rerouted through a nginx reverse proxy. Firefox and Chrome work fine, I've only seen this issue on Chromium. My project is open-source; would it help if I provided you my source code + wss url to help you debug?

Maybe, but if you have already isolated the issue to be Chromium specific I think there is not much I can do.
Does it work if you connect directly to the server via Chromium bypassing nginx?

As I've said, directly connecting results in this:

WebSocket connection to 'wss://ip.addr.he.re:8190/' failed: Error in connection establishment: net::ERR_CONNECTION_CLOSED

Regarding my sidenotes, is there already a system to include this module in a browser using a

Closing this as I think there is not much we can do. @Ratismal please reopen if needed.

Fair enough. Thank you for your support.

A fix for me was setting these response headers: _Host_, _Connection_ and _Upgrade_.
For nginx case (socket.io server is behind the proxy):

```nginx
location /foo/ {
proxy_pass http://foobar:3005/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}

@mklimek This worked great for my setup. Thanks.

@mklimek Worked for me! Thank you!

@mklimek @venkateshthallam @PassTheMayo You guys are commenting on ws library whereas @mklimek mentioned socket.io. What libraries are you guys using?

@naknode Read the previous comments, this issue was resolved.

nginx don't support websocket锛宐ut it can proxy websocket connetions锛寉ou neet config it:
location / {
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

}

Reference link:
http://nginx.org/en/docs/http/websocket.html

A fix for me was setting these response headers: _Host_, _Connection_ and _Upgrade_.
For nginx case (socket.io server is behind the proxy):

location /foo/ {
    proxy_pass http://foobar:3005/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
}
`

resolved my issue

@mklimek This worked great for my setup. Thanks.

@mklimek , that worked 100%.
I am using signalR with Angular and that was the final piece of the configuration.

I think your server used sockjs, client not use.

Any note on how to solve this in IIS using nodeiis?

Any note on how to solve this in IIS using nodeiis?

What we use is:
in <system.webServer> add
<webSocket enabled="false" />

then in add
<rule name="WSockets"> <match url="/socket.*" /> <conditions> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/> </conditions> <action type="Rewrite" url="backend/server.js"/> </rule>
this makes the IIS proxy the WS request to your backend. We are sending open request to domain/socket and handling it in node.js (or whatever you use) server on the backend

A fix for me was setting these response headers: _Host_, _Connection_ and _Upgrade_.
For nginx case (socket.io server is behind the proxy):

location /foo/ {
    proxy_pass http://foobar:3005/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
}

I think this was a general web socket issue, i experienced the same error while working with SignalrR for an asp.net core site hosted on linux.

I changed nginx config file specifying 'proxy_set_header Connection "upgrade"' and everything worked.

Thank you for your suggestion sir!

A fix for me was setting these response headers: _Host_, _Connection_ and _Upgrade_.
For nginx case (socket.io server is behind the proxy):

location /foo/ {
    proxy_pass http://foobar:3005/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
}

A hero can be anyone, even a man doing something as simple as putting a working nginx config in Github issues.

A fix for me was setting these response headers: _Host_, _Connection_ and _Upgrade_.
For nginx case (socket.io server is behind the proxy):

location /foo/ {
    proxy_pass http://foobar:3005/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
}

Kindly, referring to your solution above, I need to apply it using apache as I have the same issue, so please feedback me about the needed code for apache.

proxy_set_header Host $host;

this saved my life tonight. Thank you!

I not able to solve the issue on my AWS ElasticBean instance... maybe someone could provide some much appreciated insights on what is the problem with my nginx config file below:
files:
"/etc/nginx/conf.d/upload.conf" :
mode: "000755"
owner: root
group: root
content: |
client_max_body_size 500M;

server {
    listen 8081;

    #server_name xxx.elasticbeanstalk.com

    # prevents 502 bad gateway error
    large_client_header_buffers 8 32k;

    # enables WS support
    location / {
      proxy_pass http://localhost:5000;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
    }
}

Any one can provide the same solution for apache ?

A fix for me was setting these response headers: _Host_, _Connection_ and _Upgrade_.
For nginx case (socket.io server is behind the proxy):

location /foo/ {
    proxy_pass http://foobar:3005/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
}

Amazing, worked for me too like a charm, as i was banging my hand since a couple of days on account of setting this reverse proxy configuration in nginx,
Thanks a lot man you made my day :)

How I can create this header in apache?

This document is for those who use laravel-echo-server & Nginx & socket.io & Redis-server with the separated server between client project and Redis-server.

Please follow the link here.

Thanks

Is there any fix for those who are hosting on Heroku and not Nginx?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nodesocket picture nodesocket  路  4Comments

jorenvandeweyer picture jorenvandeweyer  路  4Comments

anonaka picture anonaka  路  5Comments

makc picture makc  路  4Comments

cmnstmntmn picture cmnstmntmn  路  4Comments