Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Ratismal opened this issue Jan 28, 2017 · 32 comments
Closed

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

Ratismal opened this issue Jan 28, 2017 · 32 comments

Comments

@Ratismal
Copy link

Ratismal commented Jan 28, 2017

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 <script> tag, or another means to use this module in a browser?

@lpinca
Copy link
Member

lpinca commented Jan 28, 2017

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?

@Ratismal
Copy link
Author

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?

@lpinca
Copy link
Member

lpinca commented Jan 28, 2017

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?

@Ratismal
Copy link
Author

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 <script> tag, or would I have to browserify it myself?

@lpinca
Copy link
Member

lpinca commented Jan 28, 2017

Oh sorry didn't understand that part.

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

No, there isn't and there are parts which can't be "browserified" as there is no way to have access to raw TCP sockets from the browser using JavaScript.

@lpinca
Copy link
Member

lpinca commented Feb 2, 2017

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

@lpinca lpinca closed this as completed Feb 2, 2017
@Ratismal
Copy link
Author

Ratismal commented Feb 2, 2017

Fair enough. Thank you for your support.

@mklimek
Copy link

mklimek commented Oct 19, 2017

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;
}

@venkateshthallam
Copy link

@mklimek This worked great for my setup. Thanks.

@PassTheMayo
Copy link

@mklimek Worked for me! Thank you!

@naknode
Copy link

naknode commented Mar 1, 2018

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

@PassTheMayo
Copy link

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

@lf1029698952
Copy link

nginx don't support websocket,but it can proxy websocket connetions,you 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

@xingchen19
Copy link

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

@memstone
Copy link

@mklimek This worked great for my setup. Thanks.

@leeroya
Copy link

leeroya commented Apr 14, 2019

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

@joviqiao
Copy link

I think your server used sockjs, client not use.

@kjr247
Copy link

kjr247 commented Jul 22, 2019

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

@aleksandar-kandzhichki
Copy link

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

@tmacharia
Copy link

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!

@AdamZaczek
Copy link

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.

@RamyTayseer
Copy link

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.

@rdefulio
Copy link

proxy_set_header Host $host;

this saved my life tonight. Thank you!

@leefsmp
Copy link

leefsmp commented Mar 11, 2020

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";
    }
}

@PrakashLakhara
Copy link

Any one can provide the same solution for apache ?

@devboosters
Copy link

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 :)

@NFDeap
Copy link

NFDeap commented Apr 9, 2020

How I can create this header in apache?

@theavuthnhel
Copy link

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

@akinsikuoluwafemi
Copy link

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

@gerroon
Copy link

gerroon commented Jun 22, 2021

Does anyone have a reverse proxy setup for Apache to make the websocket work?

@shagunbihal
Copy link

shagunbihal commented Jul 14, 2021

Hlo everyone please solve my issue found in my apache live server regarding websocket connetion. I am using wss + ssl on live server but connection failed. If you have any solution so let me know ASAP. If any proxy to use so let me know.

@jmbeuken
Copy link

HI,

works for me :-)

apche2 config

<VirtualHost *:443>
  ServerName example.com
      
  ProxyPreserveHost On
  RewriteEngine On
     
  # If header Upgrade: websocket found, proxy to ws
  RewriteCond %{HTTP:Upgrade} =websocket [NC]  
  RewriteRule /(.*)           ws://localhost:8080/$1 [P,L]  
  
  # If header not found use http
  RewriteCond %{HTTP:Upgrade} !=websocket [NC]
  RewriteRule /(.*)           http://localhost:8080/$1 [P,L]

  
  # SSL config
  ...
  
</VirtualHost>

https://stackoverflow.com/questions/65102311/convert-nginx-conf-to-apache-conf-for-proxypass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests