NGINX配置与Socket.IO工作 [英] NGINX configuration to work with Socket.IO

查看:1666
本文介绍了NGINX配置与Socket.IO工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我一直在努力得到这个工作2天,我坚持。这是我第一次配置钢轨使用+的NodeJS IO插槽的服务器。我与NGINX和独角兽一个小白。基本上我的应用程序的+的NodeJS部分SocketIO将推消息谁是连接到我的应用程序的用户。这是我的nginx.conf

So I have been trying to get this to work 2 days and I am stuck. This is my first time configuring a server for rails that uses NodeJS+Socket IO. I am a noob with NGINX and Unicorn. Basically the NodeJS+SocketIO part of my app will push messages to users who are connected to my app. This is my nginx.conf

server{
    listen 80 default;
    root /home/deployer/saigon/public;
    try_files $uri/index.html $uri @unicorn;

    location /sockets {
        proxy_pass http://localhost:3210;
    }

    location @unicorn {
       proxy_pass http://localhost:3000;
    }
}

在我production.rb,我已经配置,用户将不得不将消息发送到/从

And in my production.rb, I have configured the url which the user will have to send message to/receive message from

SOCKET_IO_URL ='http://localhost:8080/sockets

8080,为什么?我用流浪来转发8080 - > 80

Why 8080? I use Vagrant to forward 8080 -> 80

我试过访问的http://本地主机:8080 /插座,我能得到插座欢迎信息。我看了看我的NodeJS服务器日志,它是正常的接收消息。然而,当涉及到广播.....它只是没有做到这一点。有没有人得到这样的应用程序与设定了我想要做的工作?我应该只是与Apache +独角兽走?

I tried accessing http://localhost:8080/sockets and I was able to get the socket welcome message. I looked at my NodeJS server log and it was receiving messages alright. However, when it comes to broadcasting..... it just does not do it. Has anyone ever gotten this kind of app to work with the set up I am trying to do? Should I just go with Apache + Unicorn ?

推荐答案

更新答案

我终于想出了一个办法得到这个工作。这都不明显,但在子文件夹托管socket.io时,您不使用子文件夹中的连接语句。这是我在做之前和客户端从未收到响应。

I have finally figured out a way to get this working. It's not at all obvious, but when hosting socket.io on a subfolder, you do NOT use the subfolder in the connect statement. This is what I was doing before and the client was never receiving a response.

无法正常工作

<script src="/test/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost:8080/test/', {resource:'test/socket.io'});
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });
</script>

的http://本地主机:8080 /测试/ 这是被扔东西掉一部分。这创造了一个本地套接字的服务器端不尊重命名空间。因此,客户端发送的命名空间中的信息/测试/,但服务器的响应将要空namepace'',使客户永远不会的消息。解决方法是简单地删除了/测试/,并确保您正在使用的客户端和服务器上的资源变量。

http://localhost:8080/test/ This is the part which is throwing things off. That creates a namespace for the local socket which the server side does not respect. So the client sends the message on the namespace '/test/' but the server responses are going to an empty namepace '' so the client never gets the messages. The workaround it to simply remove the '/test/' and make sure you are using the resource variable on the client and the server.

工作!

<script src="/test/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost:8080', {resource:'test/socket.io'});
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });
</script>

我希望这可以帮助你得到的东西对你合作的。

I hope this helps you get things working on your end.

原来的答案

这不是你的设置有问题,那是不想socket.io在子文件夹中运行的一个问题。我敢打赌愿意是,如果你放弃了/插座的例子就是做工精细。我使用http节点代理试图主办的子文件夹socket.io连接时,碰到了相同的问题。有创造了前段时间的错误,但它被关闭了,从来没有解决。

It is not a problem with your setup, it is a problem of socket.io not wanting to work on sub folder. I would bet willing to be that if you dropped the /sockets your example would work fine. I ran into the exact same problem when using http-node-proxy trying to host socket.io connections on subfolders. There was a bug created a while ago, but it was closed out and never resolved.

<一个href=\"https://github.com/LearnBoost/socket.io-client/issues/185\">https://github.com/LearnBoost/socket.io-client/issues/185

<一个href=\"https://github.com/LearnBoost/socket.io/issues/320\">https://github.com/LearnBoost/socket.io/issues/320

我还在寻找一个解决方案为好,但我有一种感觉,我将不得不挽起了袖子,深入到code喽。

I am still looking for a solution as well, but I have a feeling I'm going to have to roll up my sleeves and dig into the code myself.

这篇关于NGINX配置与Socket.IO工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆