Node.js 连接建立中的WebSocket错误:网络::错误\u连接\u已关闭
当我尝试与我的服务器建立Node.js 连接建立中的WebSocket错误:网络::错误\u连接\u已关闭,node.js,apache,https,websocket,wss,Node.js,Apache,Https,Websocket,Wss,当我尝试与我的服务器建立wss连接时,我遇到此错误: WebSocket连接到'wss://mydomain:3000/'失败:中出现错误 连接建立:网络::错误\u连接\u已关闭 我目前有一个apache2虚拟主机配置设置,用于侦听端口443和80上的请求: <VirtualHost *:80> ServerName otherdomainname.co.uk ServerAlias www.otherdomainname.co.uk
wss
连接时,我遇到此错误:
WebSocket连接到'wss://mydomain:3000/'失败:中出现错误
连接建立:网络::错误\u连接\u已关闭
我目前有一个apache2虚拟主机配置设置,用于侦听端口443和80上的请求:
<VirtualHost *:80>
ServerName otherdomainname.co.uk
ServerAlias www.otherdomainname.co.uk
RewriteEngine On
RewriteRule ^/(.*)$ /app/$1 [l,PT]
JkMount /* worker2
</VirtualHost>
<VirtualHost _default_:443>
ServerName otherdomainname.co.uk
ServerAlias www.otherdomainname.co.uk
RewriteEngine On
RewriteRule ^/(.*)$ /app/$1 [l,PT]
SSLEngine On
SSLCertificateFile /etc/apache2/ssl/apache.crt
SSLCertificateKeyFile /etc/apache2/ssl/apache.key
<Location />
SSLRequireSSL On
SSLVerifyClient optional
SSLVerifyDepth 1
SSLOptions +StdEnvVars +StrictRequire
</Location>
JkMount /* worker2
</VirtualHost>
为什么我无法通过
https
使用wss
协议成功连接到WebSocket服务器?问题是我没有为https/wss配置WebSocket服务器
下面是我的不安全WebSocket服务器的安全版本,它使用node.js中的“ws”
var WebSocketServer = require('ws').Server,
fs = require('fs');
var cfg = {
ssl: true,
port: 3000,
ssl_key: '/path/to/apache.key',
ssl_cert: '/path/to/apache.crt'
};
var httpServ = ( cfg.ssl ) ? require('https') : require('http');
var app = null;
var processRequest = function( req, res ) {
res.writeHead(200);
res.end("All glory to WebSockets!\n");
};
if ( cfg.ssl ) {
app = httpServ.createServer({
// providing server with SSL key/cert
key: fs.readFileSync( cfg.ssl_key ),
cert: fs.readFileSync( cfg.ssl_cert )
}, processRequest ).listen( cfg.port );
} else {
app = httpServ.createServer( processRequest ).listen( cfg.port );
}
var wss = new WebSocketServer( { server: app } );
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
ws.send(message);
});
ws.send('something');
});
我遇到了一个类似的问题,原来我使用的是CloudFlare,它只允许非常特定的端口通过 因此,运行在3000端口的流星立即被拦截 重新配置反向代理设置并在允许的端口上运行Meteor解决了我的问题 但是,最后,我关闭了Meteor部署上的插座。它似乎没有影响性能。祝你好运 4年后更新LOL 因此,我们使用Apache2并侦听端口80上的域,但在本例中,我们将获取端口80流量并将其重定向到本地主机端口3020。它可以是任何端口。希望这有帮助!如果你想看,请访问www.StarLordsOnline.com:)
服务器管理员info@starlordsonline.com
ServerName starlordsonline.com
ServerAlias www.starlordsonline.com
重新启动发动机
RewriteCond%{HTTP:UPGRADE}^WebSocket$[NC]
RewriteCond%{HTTP:CONNECTION}^升级$[NC]
重写规则。*ws://localhost:3020%{REQUEST_URI}[P]
代理请求关闭
命令拒绝,允许
通融
ProxyPasshttp://localhost:3020/
ProxyPassReversehttp://localhost:3020/
我在哪里配置?我是Node.JS新手,遇到此问题。您必须设置Web服务器进行公钥加密,并提供密钥和证书信息。太谢谢你了,我花了一整天的时间在这上面。谢谢,很高兴它起作用了。发布lol后4年。我会用我的vhost的一个例子更新我的答案。
var WebSocketServer = require('ws').Server,
fs = require('fs');
var cfg = {
ssl: true,
port: 3000,
ssl_key: '/path/to/apache.key',
ssl_cert: '/path/to/apache.crt'
};
var httpServ = ( cfg.ssl ) ? require('https') : require('http');
var app = null;
var processRequest = function( req, res ) {
res.writeHead(200);
res.end("All glory to WebSockets!\n");
};
if ( cfg.ssl ) {
app = httpServ.createServer({
// providing server with SSL key/cert
key: fs.readFileSync( cfg.ssl_key ),
cert: fs.readFileSync( cfg.ssl_cert )
}, processRequest ).listen( cfg.port );
} else {
app = httpServ.createServer( processRequest ).listen( cfg.port );
}
var wss = new WebSocketServer( { server: app } );
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
ws.send(message);
});
ws.send('something');
});
<VirtualHost *:80>
ServerAdmin info@starlordsonline.com
ServerName starlordsonline.com
ServerAlias www.starlordsonline.com
RewriteEngine on
RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC]
RewriteRule .* ws://localhost:3020%{REQUEST_URI} [P]
ProxyRequests off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
<Location />
ProxyPass http://localhost:3020/
ProxyPassReverse http://localhost:3020/
</Location>