I want to run a node process on my server.
Requests to http://mysite.me:80/ should go to http://localhost:8000
Requests to http://mysite.me:80/xxx should go to http://localhost:8001
I used to do something like:
httpProxy = require('http-proxy');
var options = {
    router: {
        'mysite.me': 'localhost:8000',
        'mysite.me/xxx': 'localhost:8001'
    }
};
httpProxy.createServer(options).listen(80)
But I understand 'router' is now deprecated.
I've been at this for hours!
Right, so I got it all working...
Modify apache2 so it only listens on loopback interface:
vim /etc/apache2/ports.conf
Change "Listen 80" to "Listen 127.0.0.1:80"
Have your socket.io server ("server.js") listening on port 8001:
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
server.listen(8001);
io.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  /*socket.on('my other event', function (data) {
    console.log(data);
  });*/
});
node server.js
Now you need a proxy ("proxy.js") listening on your public interface (x.x.x.x:80)
It splits traffic between apache2 and socket.io as appropriate
var http      = require('http');
var httpProxy = require('http-proxy');
var routes = [
  'http://127.0.0.1:80',
  'http://127.0.0.1:8001'
];
var proxy = new httpProxy.createProxyServer({});
var proxyServer = http.createServer(function (req, res) {
    var route=0;
    var split1=req.url.split('/');
    if (split1.length>0) {
        if(split1[1]=="socket.io"){   //requests to "socket.io" get tapped off to nodejs
            route=1;
        }
    }
    proxy.web(req, res, {target: routes[route]});
});
proxyServer.on('upgrade', function (req, socket, head) {
    proxy.ws(req, socket, head, {target: routes[1]});
});
proxyServer.listen(80,"x.x.x.x");
//proxyServer.listen(80,"ip:v6:address");   //optional...
sudo node proxy.js
And here's the HTML/jQuery for the web page:
<script src="./lib/socket.io/socket.io-1.1.0.js"></script>
<script>
$(document).ready(function(){
    var socket = io.connect('http://www.mysite.me:80');   //where mysite.me resolves to x.x.x.x
    socket.on('news', function (data) {
        console.log(data);
        socket.emit('my other event', { my: 'data' });
    });
});
</script>
Now I need a way to figure out how to make sure proxy.js doesn't go down and is hammer proof!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With