How do I run multiple instances on the same server?

I would like to run two instances of the Sails.js API on the same server. I only got one domain name and would like something like:

How is this possible with Sails / Express / Node.js? Is there any module to solve this problem? Or do I need a proxy server like Apache or Nginx?

+3


source to share


1 answer


You can do node-http-proxy

You have server 1 on port 8080, for example, and sails port 2 to port 8081. And now with the http-proxy on port 80 you can redirect / 1.0 / to server 1 and v1.1 to server 2.

Here is an example http-proxy you can use (not tested):



var http = require('http'), httpProxy = require('http-proxy'), proxy = httpProxy.createProxyServer({}), url = require('url');

proxy.on("error", function(err){console.log(err);});//Prevent proxy crash by socket hang out error

var proxyServer = http.createServer(function (req, res)
{
    var hostname = req.headers.host.split(":")[0]; 

    switch (hostname+req.url)
    {
        case 'api.example.com/1.0':
            proxy.web(req, res, {target : 'http://localhost:8080'});
            break;
        case 'api.example.com/1.1':
            proxy.web(req, res, {target : 'http://localhost:8081'});
            break;
        default:
            proxy.web(req, res, {target : 'http://localhost:8080'});
    }
});

//
// Listen to the `upgrade` event and proxy the
// WebSocket requests as well.
//
proxyServer.on('upgrade', function (req, socket, head)
{
    var hostname = req.headers.host.split(":")[0]; 

    switch (hostname+req.url)
    {
        case 'api.example.com/1.0':
            proxy.ws(req, socket, {target : 'ws://localhost:8080'});
            break;
        case 'api.example.com/1.1':
            proxy.ws(req, socket, {target : 'ws://localhost:8081'});
            break;
        default:
            proxy.ws(req, socket, {target : 'ws://localhost:8080'});
    }
});
proxyServer.listen(80, function ()
{
    console.log('proxy listening on port 80');
});

      

You can do the same with Apache or Nginx, but you are asking for a complete node solution :)

+3


source







All Articles