意外的响应代码:502 nginx

时间:2013-01-18 17:44:39

标签: node.js websocket

我有以下设置:

作为代理运行的nginx服务器:

server {
    server_name  domain.tld;
    location / {
            # For Read Requests
            proxy_pass http://XXX.XXX.XXX.XXX;
            proxy_set_header        Host            $host;
            proxy_set_header        X-Real-IP       $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    location ~* "(/config.json|/nowjs/now.js|socket.io|/stat/1.gif)" {
            proxy_pass http://127.0.0.1:9080;
            proxy_set_header        Host            $host;
            proxy_set_header        X-Real-IP       $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

其中XXX.XXX.XXX.XXX是我的静态内容网页和资源的IP域。

我有一个简单的聊天应用程序在与端口9080上的nginx相同的服务器上运行

在我的index.html页面中,我有这段代码:

  <div id="chat">
    <div id="messages"></div>
    <input type="text" id="text-input" />
    <input type="button" value="Send" id="send-button" />
  </div>

  <script type="text/javascript" src="http://domain.tld/nowjs/now.js"></script>
  <script type="text/javascript">
    $(document).ready(function(){
        now.receiveMessage = function(name, message){
        $("#messages").append('<br />' + '<a href="${path}">' + name + '</a>' + ': ' + message);
        }
        $("#send-button").click(function(){
        now.distributeMessage($("#text-input").val());
        $("#text-input").val("");
        });
      now.name = "${name}";
    });
  </script>

这是app.js文件:

var server = require('http').createServer(function(req, res){
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Chat Server\n');
});
server.listen(9080);

var nowjs = require("now");
var everyone = nowjs.initialize(server);

everyone.now.distributeMessage = function(message){
  console.log('User '+this.now.name+' added message ' +message);
  everyone.now.receiveMessage(this.now.name, message);
};

一切似乎都运行正常,虽然我一直收到Unexpected响应代码:502页面加载时!

有没有办法为nginx启用web代理,因为使用Firefox Firebug进一步查看,我收到此错误:

Firefox can't establish a connection to the server at ws://domain.tld/socket.io/1/websocket/-WFvaQTvzASDBMHNpUGA.

任何建议都非常感激。

1 个答案:

答案 0 :(得分:0)

我改变了:

var everyone = nowjs.initialize(server);

var everyone = nowjs.initialize(server, {socketio: {transports: ["xhr-polling"]}});

似乎完成了这项工作。