简体   繁体   English

Socket.io + Node.js + Apache

[英]Socket.io + Node.js + Apache

I have my app written in node on a Debian 7 server launched with forever and working with apache proxy via a url (without port like http.//subdomain.domain.com) with this config: 我在永久启动的Debian 7服务器上的节点上编写了我的应用程序,并使用以下配置通过网址(没有端口,例如http.//subdomain.domain.com)使用apache代理:

<VirtualHost *:80>
    ServerName subdomain.domain.com

    ProxyRequests off

    <Proxy *>
            Order deny,allow
            Allow from all
    </Proxy>

    <Location />
            ProxyPass http://localhost:8081/
            ProxyPassReverse http://localhost:8081/
    </Location>

 </VirtualHost>

The app is running ok but the problem comes with socket.io, I´m getting this error: 该应用程序运行正常,但问题出在socket.io上,我收到此错误:

GET http://192.168.1.1:8081/socket.io/?EIO=2&transport=polling&t=1424074094677-75 net::ERR_CONNECTION_TIMED_OUT

This is my server.js: 这是我的server.js:

var server = express.createServer();
server.configure(function(){
    server.set('views', __dirname + '/views');
    server.set('view options', { layout: false });
    server.use(connect.bodyParser());
    server.use(express.cookieParser());
    server.use(express.session({ secret: "ezdoknahi!!"}));
    server.use(connect.static(__dirname + '/static'));
});


server.configure('development', function(){
    server.use(express.errorHandler());
});

//setup the errors
server.error(function(err, req, res, next){
    if (err instanceof NotFound) {
        res.render('404.jade', { locals: {
                  title : '404 - Not Found'
                 ,description: ''
                 ,author: ''
                 ,analyticssiteid: 'XXXXXXX'
                },status: 404 });
    } else {
        res.render('500.jade', { locals: {
                  title : 'The Server Encountered an Error'
                 ,description: ''
                 ,author: ''
                 ,analyticssiteid: 'XXXXXXX'
                 ,error: err
                },status: 500 });
    }
});
server.listen( port);

//Setup Socket.IO
var io = io.listen(server);
io.sockets.on('connection', function(socket){
  console.log('Client Connected');
  socket.on('message', function(data){
//    socket.broadcast.emit('server_message',data);
//    socket.emit('server_message',data);
      console.log(data);
  });
  socket.on('disconnect', function(){
    console.log('Client Disconnected.');
  });
});


///////////////////////////////////////////
//              Routes                   //

And this is my client app (Angular.js): 这是我的客户端应用程序(Angular.js):

produccionApp.factory('socket', function () {
    var socket = io.connect('http://192.168.1.1:8081');
    return socket;
});

(192.168.1.1 is my subdomain.domain.com IP Address (enterprise server)) (192.168.1.1是我的subdomain.domain.com IP地址(企业服务器))

In my localhost works perfectly (how not!) but in production not. 在我的本地主机工作完美(怎么不行!),但在生产中却没有。 I suspect the problem is connecting through apache proxy. 我怀疑问题是通过apache代理连接的。 How to solve it? 怎么解决呢?

Thanks in advance 提前致谢

UPDATE: 更新:

I installed nginx 1.6 on this server ( Debian 7 ) and I configured like this: 我在此服务器(Debian 7)上安装了nginx 1.6,并进行了如下配置:

server {
    listen 80;
    server_name subdomain.domain.com;

    access_log /var/log/nginx/subdomain.domain.com.log;
    error_log  /var/log/nginx/subdomain.domain.com.error.log;

    location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_set_header X-NginX-Proxy true;

      proxy_pass http://127.0.0.1:8081/;
      proxy_redirect off;

              proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
    }

}

And still the same error: 还是同样的错误:

GET http://192.168.1.1:8081/socket.io/?EIO=2&transport=polling&t=1424074094677-75 net::ERR_CONNECTION_TIMED_OUT

I also tried to connect socket.io to http://subdomain.domain.com but still same error. 我也尝试将socket.io连接到http://subdomain.domain.com,但仍然是相同的错误。

any help? 有什么帮助吗?

Kioko Kiaza, 起子千子

  1. The set-up is the hardest part getting thing going - you need to make your whole server code a-lot simpler - first get websockets going and then build your server. 设置是最困难的部分-您需要使整个服务器代码更简单-首先使websockets运行,然后构建服务器。

  2. Socket.io is a bad idea, primus is much better choice all around (you can use socket.io) inside primus if you like them so much. Socket.io是一个坏主意,如果您非常喜欢primus,可以在primus内各处(最好使用socket.io)选择primus。

  3. Apache ain't a good choice for what your trying to achieve either, you should go with either nginx, or haproxy. 对于您想要达到的目标,Apache都不是一个很好的选择,您应该使用nginx或haproxy。

If you want you, can contact me on skype/email and I can help you set the whole thing up for you will later write a summary of what you learned here. 如果您需要,可以通过Skype /电子邮件与我联系,我可以帮助您设置整个过程,稍后您将在此处写下您学到的内容的摘要。

I am making 2 observations here: 我在这里进行2个观察:

  1. In ngnix and Apache you are redirecting to localhost, in your code you are using IP adress. 在ngnix和Apache中,您将重定向到localhost,在代码中,您正在使用IP地址。 Try to keep it consistent to make bugg-finding easier. 尝试保持一致,以使错误查找更加容易。

  2. The code is bugging even though you aren't using the proxy, so it does not look like it is the redirects that is not working. 即使您没有使用代理,代码也会出错,因此它看起来好像是重定向无效。

Suggestions: 建议:

  • Have you tried different approaches like using 127.0.0.1:8081? 您是否尝试过使用127.0.0.1:8081等其他方法? Sometimes when using the servers IP adress (specially if it is the same as the gateway), you are getting a loop-back and it can confuse the system. 有时,当使用服务器IP地址时(特别是与网关相同),您会得到环回,这会使系统感到困惑。 Eg. 例如。 LAN => WAN => LAN. 局域网=>广域网=>局域网。

  • Did you check your hosts file, to make sure that the IP addresses is right and spot on? 您是否检查了主机文件,以确保IP地址正确无误? Sometimes Linux and windows gets a stroke and dumps the hosts file and custom names such as localhost does not work anymore, i suggest you open it with vim or similar - if the file contains characters like ¤#%¤# in it, it is most likely corrupt. 有时Linux和Windows会中风并转储主机文件,而自定义名称(如localhost)不再起作用,我建议您使用vim或类似名称打开它-如果文件中包含¤#%¤#之类的字符,则是大多数情况可能已损坏。 If not just paste all the text, delete the hosts file, create a new with the same name and paste the text in there again. 如果不只是粘贴所有文本,请删除主机文件,创建一个具有相同名称的新文件,然后将文本再次粘贴到该文件中。 Worked for me on several occasions. 为我工作过几次。

  • Try and make a basic html file and place it in the root of where you want to see. 尝试制作一个基本的html文件,并将其放置在您要查看的根目录中。 Connect through the proxy and see if you get the page you made. 通过代理连接,查看是否获得创建的页面。 This will tell you if there is any problems with the proxy or the service. 这将告诉您代理或服务是否存在任何问题。
  • Sometimes names can be confusing, since you are using the same server as host and as proxy, try and give your service a name, and the use the hosts.txt file to handle the naming. 有时,名称可能会造成混淆,因为您使用的是与主机和代理相同的服务器,请尝试为服务命名,然后使用hosts.txt文件处理命名。 Eg. 例如。

     # Socket io custom dns 127.0.0.1 socket.io.service 

    and call that through your nginix or virtualhost. 并通过您的nginix或virtualhost进行调用。

      proxy_pass http://socket.io.service:8081/; 

尝试侦听端口8081而不是仅侦听80。

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

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