Help needed NGINX reverse proxy to NODE.JS

Ben ben+nginx at
Tue Aug 27 14:57:03 UTC 2013


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 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 
(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);

Simple NGINX conf :

         upstream node_admin {
                 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 :

<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 
      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 =;
         alert("Message is received...");
         console.log('received: %s', received_msg);
      ws.onclose = function()
         // websocket is closed.
         alert("Connection is closed...");
      // The browser doesn't support WebSocket
      alert("WebSocket NOT supported by your Browser!");
<div id="sse">
    <a href="javascript:WebSocketTest()">Run WebSocket</a>

More information about the nginx mailing list