node.js - "Cannot GET" on reverse proxy from Nginx to socket.io on express.js -


i've followed this tutorial node.js working through nginx on 2 ubuntu 14.04 servers via private networking (node.js on myappserver - accessible via private ip myprivatewebserver , publicly via mypublicappserver - , nginx on mywebserver). works fine til point - can access node.js application on myprivateappserver:3000 proxied via nginx going http://mywebserver/node.

however, when try , run chat application in socket.io on express.js, works when access directly (http://mypublicappserver:3000) but, when try , access via nginx proxy (http://mywebserver/node), "cannot /node" in browser , in firebug console " "networkerror: 404 not found - http://mywebserver/node".

if curl http://myprivateappserver:3000 mywebserver, index.html socket.io application fine.

my /etc/nginx/sites-available/default contains:

location /node{ proxy_pass http://myprivateappserver:3000; proxy_http_version 1.1; proxy_set_header upgrade $http_upgrade; proxy_set_header connection 'upgrade'; proxy_set_header host $host; proxy_cache_bypass $http_upgrade; } 

my index.js is:

var app = require('express')(); var http = require('http').server(app); var io = require('socket.io')(http);  app.get('/', function(req, res){   res.sendfile(__dirname + '/index.html'); });  io.on('connection', function(socket){   console.log('a user connected');   socket.on('disconnect', function(){     console.log('user disconnected');   });   socket.on('chat message', function(msg){     io.emit('chat message', msg);   }); });  http.listen(3000, function(){   console.log('listening on *:3000'); }); 

and index.html is:

<!doctype html> <html>   <head>     <title>socket.io chat</title>     <style>       * { margin: 0; padding: 0; box-sizing: border-box; }       body { font: 13px helvetica, arial; }       form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }       form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }       form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }       #messages { list-style-type: none; margin: 0; padding: 0; }       #messages li { padding: 5px 10px; }       #messages li:nth-child(odd) { background: #eee; }     </style>   </head>   <body>     <ul id="messages"></ul>     <form action="">       <input id="m" autocomplete="off" /><button>send</button>     </form>     <script src="/socket.io/socket.io.js"></script>     <script src="http://code.jquery.com/jquery-1.11.1.js"></script>     <script>       var socket = io();       $('form').submit(function(){         socket.emit('chat message', $('#m').val());         $('#m').val('');         return false;       });       socket.on('chat message', function(msg){         $('#messages').append($('<li>').text(msg));       });     </script>   </body> </html> 

this first foray nginx/node/express/socket (i'm apache/cakephp person) may missing simple grateful pointers on how debug what's going wrong

this because socketio uses /socket.io path default, need configure nginx proxy not /node request, /socket.io too:

location ~ ^/(node|socket\.io) {     #your proxy directives } 

(by way, looks have typos: wrote proxypass, correct form proxy_pass. same goes other proxy directives)

you need perform 1 more edit in nodejs server file. replace:

app.get('/', function(req, res){ 

with:

app.get('/node', function(req, res){ 

now should work.

this not solution. change socketio path changes in nginx config, solution described above seems simlest me. luck!


Comments