Help needed NGINX reverse proxy to NODE.JS

Ben ben+nginx at list-subs.com
Tue Aug 27 14:57:03 UTC 2013


Hi,

I've tried this with NGINX 1.4.1-1ppa1~precise and node v0.10.17 and 
just can't get it to work.

What I've tried / what happens :
(a) Yes, I have tested this direct between client code and node 
listening on 0.0.0.0 and it works as expected
(b) I have tried a multitude of alternative configs... adding a path 
addribute to my node server config and tweaking proxy_pass 
http://node_admin accordingly.  That doesn't work either.
(c) When running via NGINX, I only ever get two alerts, the "WebSocket 
is supported by your Browser!" string and the "Connection is closed..." 
string.    Nothing happens in between, and nothing on the node console 
either.
(d) Nothing in the NGINX logs either.

Help !  ;-)

Thanks !


Simple "hello world" style node script :

var WebSocketServer = require('ws').Server
   , wss = new WebSocketServer({host:'localhost',port: 8080});
wss.on('connection', function(ws) {
     ws.on('message', function(message) {
         console.log('received: %s', message);
     });
     ws.send('something');
});


Simple NGINX conf :

         upstream node_admin {
                 server 127.0.0.1:8080;
                 keepalive 64;
         }
         location /api {
                 proxy_redirect off;
                 proxy_http_version 1.1;
                 proxy_pass http://node_admin;
                 proxy_set_header Upgrade $http_upgrade;
                 proxy_set_header Connection "upgrade";
                 proxy_set_header Host $host;
         }


Simple client :

<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
function WebSocketTest()
{
   if ("WebSocket" in window)
   {
      alert("WebSocket is supported by your Browser!");
      // Let us open a web socket
      var ws = new 
WebSocket("ws://ukc-vm02-buildbox.ops.itsynergy.net.uk/api");
      ws.onopen = function()
      {
         // Web Socket is connected, send data using send()
         ws.send("Message to send");
         alert("Message is sent...");
      };
      ws.onmessage = function (evt)
      {
         var received_msg = evt.data;
         alert("Message is received...");
         console.log('received: %s', received_msg);
      };
      ws.onclose = function()
      {
         // websocket is closed.
         alert("Connection is closed...");
      };
   }
   else
   {
      // The browser doesn't support WebSocket
      alert("WebSocket NOT supported by your Browser!");
   }
}
</script>
</head>
<body>
<div id="sse">
    <a href="javascript:WebSocketTest()">Run WebSocket</a>
</div>
</body>
</html>




More information about the nginx mailing list