简体   繁体   English

Socket.io Apache反向代理无法连接

[英]Socket.io apache reverse proxy unable to connect

I'm using Socket.io ver 1.0.6. 我正在使用Socket.io 1.0.6版。 I have problem with using socket.io and apache proxy. 我在使用socket.io和apache代理时遇到问题。

Proxy have defined in vhots and look like this: 代理已在vhots中定义,如下所示:

    ServerAdmin webmaster@localhost
    ServerName site.loc
    DocumentRoot /home/site/Private/dev/site

    ProxyPass /node http://localhost:5000
    ProxyPassReverse /node http://localhost:5000

so url http://site.loc/node should run http://site.loc:5000 因此网址http://site.loc/node应该运行http://site.loc:5000

node server look like this: 节点服务器如下所示:

var port = 5000;
var server = require('http').Server();
var io = require('socket.io')(server);

io.on('connection', function(socket){

});

server.listen(port);
console.log('Listening on port: ' + port);

and client: 和客户:

var socket = io.connect('ws://site.loc', {path: '/node/socket.io'});

And I receive message with 我收到消息

Error during WebSocket handshake: Unexpected response code: 400 WebSocket握手期间出错:意外的响应代码:400

I don't want to use in node server namespace, but only route all data from url with /node to port 5000. 我不想在节点服务器名称空间中使用,而只将所有数据从具有/ node的url路由到端口5000。

Thanks for help 感谢帮助

The formatting of my comment does not look too good, so I'll take a few guesses and try to answer your request for assistance. 我的评论的格式看起来不太好,因此我会做一些猜测,并尝试回答您的帮助请求。

My guess is that your are using apache 2.2 which is not too aware of the ws protocol. 我的猜测是您使用的Apache 2.2不太了解ws协议。 socket.io, however is still capable of failing over AFAIK. socket.io,但是仍然可以故障转移AFAIK。

So in the end, you should be able to get your setup working despite the presence of the aforementioned error. 因此,最后,尽管存在上述错误,您仍然应该能够进行设置。

For that, as stated in my comment, I would suggest you change your io.connect as follows: 为此,正如我的评论中所述,建议您按以下方式更改io.connect:

var socket = io({path: '/node/socket.io'});

You will still get this error, but your server and client should still be able to communicate. 您仍然会收到此错误,但是您的服务器和客户端仍然应该能够通信。

Next, if you wish to get rid of this error, I would suggest the use of mod_proxy_wstunnel, which is available for apache 2.4 (apparently some people managed to build it for 2.2 as well so you may try that too) 接下来,如果您希望摆脱此错误,我建议您使用mod_proxy_wstunnel,它可用于Apache 2.4(显然有些人也设法将其构建为2.2,因此您也可以尝试这样做)

Update: looks like the newer socket.io has changed the path for http and ws (used to be different, but are now based on the same path followed by a specific query) Look at this thread that discusses just that: https://community.rackspace.com/developers/f/7/t/3477 It appears that nginx has better support for websockets and should be the prefered solution here. 更新:看起来较新的socket.io更改了http和ws的路径(以前是不同的,但是现在基于相同的路径,后面是特定的查询)。请看下面讨论以下内容的线程: https:// community.rackspace.com/developers/f/7/t/3477看来,nginx对websocket具有更好的支持,应该是这里的首选解决方案。 It might be worth looking at ways to detect the "transport" parameter in the socket.io query in order to let apache know which protocol to use for the ProxyPass. 可能值得研究一下在socket.io查询中检测“ transport”参数的方法,以便让apache知道对ProxyPass使用哪种协议。

EDIT: correct the path, add some info. 编辑:更正路径,添加一些信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM