Want a node app on host A to talk to another node app on host B using a websocket (no web app involved). The host B sits behind an SSL configured reverse proxy (nginx).
Below is the configuration I have. Am using the 'ws' module for websockets.
I get a successful connection using curl from host A. When I run the node app on host A it returns an error. Therefore at present I assume my nginx.conf and the node app on host B are OK and the issue is the node app client on host A.
How to make it work?
node server app
var web = (function() {
var express = require('express');
return {
app: express(),
express: express
};
})();
var server = web.app.listen(3000, function(){
console.log('Example app listening on port 3000!');
});
const wss = new SocketServer({ server });
wss.on('connection', function connection(ws) {
console.log("connection ...");
ws.on('message', function incoming(message) {
console.log('received: %s', message);
connectedUsers.push(message);
});
ws.send('message from server at: ' + new Date());
});
nginx proxy
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name any.com;
ssl_certificate "/etc/pki/nginx/ssl/cert/SSL_Certificate.crt";
ssl_certificate_key "/etc/pki/nginx/ssl/private/any.com.key";
ssl_protocols TLSv1.1 TLSv1.2;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
location /app/wss/ {
proxy_pass http://localhost:3000/;
proxy_http_version 1.1;
proxy_set_header Upgrade websocket;
proxy_set_header Connection upgrade;
}
}
node client app
const WebSocket = require('ws');
wsClient = new WebSocket('wss://any.com/app/wss');
wsClient.on('open', function open() {
console.log('open connection, sending message to server...');
wsClient.send('something from client');
});
wsClient.on('message', function incoming(data) {
console.log('clientreceived message from server:'+data);
});
node client app error
node app
events.js:160
throw er; // Unhandled 'error' event
^
Error: Unexpected server response: 301
at ClientRequest.req.on (/home/xxx/projects/app/node_modules/ws/lib/websocket.js:542:5)
at emitOne (events.js:96:13)
at ClientRequest.emit (events.js:188:7)
at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:474:21)
at HTTPParser.parserOnHeadersComplete (_http_common.js:99:23)
at TLSSocket.socketOnData (_http_client.js:363:20)
at emitOne (events.js:96:13)
at TLSSocket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at TLSSocket.Readable.push (_stream_readable.js:134:10)
successful curl and response
curl --include --no-buffer --header "Connection: Upgrade" --header "Upgrade: websocket" --header "Host: localhost:3000"
--header "Origin: http://example.com:80" --header "Sec-WebSocket-Key: SGVsbG8sIHdvcmxkIQ=="
--header "Sec-WebSocket-Version: 13" https://any.com/app/wss/
HTTP/1.1 101 Switching Protocols
Server: nginx/1.12.1
Date: Fri, 26 Oct 2018 23:25:05 GMT
Connection: upgrade
Upgrade: websocket
Sec-WebSocket-Accept: qGEgH3En71di5rrssAZTmtRTyFk=
�?message from server at: Fri Oct 26 2018 23:25:05 GMT+0000 (UTC)^C
I found after adding a / to the URL in the client app it worked, changed this:
wsClient = new WebSocket('wss://any.com/app/wss');
To this:
wsClient = new WebSocket('wss://any.com/app/wss/');
Not sure what objects to this upstream. It's either the reverse proxy nginx or the server app. It also fails when I replicate the same URL into the (slimmed down) curl command:
curl --include --no-buffer --header "Connection: Upgrade" --header "Upgrade: websocket" --header "Sec-WebSocket-Key: SGVsbG8sIHdvcmxkIQ==" --header "Sec-WebSocket-Version: 13" https://any.com/app/wss
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Sat, 27 Oct 2018 17:19:22 GMT
Content-Type: text/html
Content-Length: 185
Location: https://any.com/app/wss/
Connection: keep-alive
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>
So by a chance mistake I had a different URL in the curl command versus the node client app which was initially made the former method work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.