Паруса за узлом http-proxy

Я пытаюсь разместить несколько серверов парусов на одном сервере, для этого я хочу поместить узел http-proxy, который перенаправляет каждый домен на нужный сервер следующим образом:

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

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

    switch(hostname) 
    { 
        case 'example.com': 
            proxy.web(req, res, { target: 'http://localhost:8080' }); 
            break; 
        case 'dev.example.com': 
            proxy.web(req, res, { target: 'http://localhost:8081' }); 
            break; 
        default: 
            proxy.web(req, res, { target: 'http://localhost:80' }); 
    } 
}).listen(80, function() { 
    console.log('proxy listening on port 80'); 
});

Но при этом после вызова новой страницы у меня возникает эта ошибка и сбой прокси:

/var/www/default/node_modules/http-proxy/lib/http-proxy/index.js:119
    throw err; 
          ^ 
Error: socket hang up 
    at createHangUpError (_http_client.js:215:15) 
    at Socket.socketCloseListener (_http_client.js:247:23) 
    at Socket.emit (events.js:129:20)
    at TCP.close (net.js:485:12)

Я полагаю, что пропустил какой-то код для пересылки сокетов, но как я могу это сделать? Достаточно ли силен этот тип прокси для производства?


person jaumard    schedule 03.08.2015    source источник


Ответы (2)


Вы обрабатываете прокси только для http запроса. Вы также должны обрабатывать socket запрос.

Вот небольшой трюк, вы можете добавить свою логику в свое приложение.

proxyServer.on('upgrade', function (req, socket, head) {

    console.log(req)
    proxy.ws(req, socket, head);
});
person Nishchit    schedule 14.03.2016

Наконец, я использую это: https://gist.github.com/jaumard/047f10b7c0563b4bd045

Он обрабатывает http и сокеты и проверяет, жив ли веб-сайт, если нет, отправляет страницу обслуживания.

/**
 * Node.js proxy to redirect domain to correct server
 * Also check is server is alive or send a maintenance page
 */
var http = require('http'),
    httpProxy = require('http-proxy'),
    proxy = httpProxy.createProxyServer({}),
    request = require('request'),
    domains = {
    "domain1.com" : "localhost:8080",
    "dev.domain1.com" : "localhost:8081",
    "domain2.com" : "localhost:8082",
    "default" : "localhost:8080"
};

var maintenance = "<!DOCTYPE html>"+ "<title>Site Maintenance</title>" + "<style>" + "  body { text-align: center; padding: 150px; }" + "  h1 { font-size: 50px; }" + "  body { font: 20px Helvetica, sans-serif; color: #333; }" + "  article { display: block; text-align: left; width: 650px; margin: 0 auto; }" + "  a { color: #1bbc9b; text-decoration: none; }" + "  a:hover { color: #109a7e; text-decoration: none; }" + "</style>" + "<article>" + "    <h1>We&rsquo;ll be back soon!</h1>" + "    <div>" + "        <p>Sorry for the inconvenience but we&rsquo;re performing some maintenance at the moment. If you need to you can always <a href=\"mailto:[email protected]\">contact us</a>, otherwise we&rsquo;ll be back online shortly!</p>" + "        <p>&mdash; My Team</p>" + "    </div>" + "</article>";

//Check is server is alive, if not respond a maintenance page
var checkServerAvailability = function (req, res, url)
{
    request(url + '/isAlive', function (error, response, body)
    {
        if (!error && response.statusCode == 200)
        {
            proxy.web(req, res, {target : url});
        }
        else
        {
            res.writeHeader(503, {"Content-Type" : "text/html"});
            res.write(maintenance);
            res.end();
        }
    });
};

var server = http.createServer(function (req, res)
{
    var hostname = "";
    //If server call with domaine and not call with IP directly
    if (req.headers.host)
    {
        hostname = req.headers.host.split(":")[0];
    }

        //If it's a know domain, redirect to correct server
    if (domains[hostname])
    {
        checkServerAvailability(req, res, 'http://' + domains[hostname]);
    }
    else
    {
        checkServerAvailability(req, res, 'http://' + domains["default"]);
    }
});

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

    //If it's a know domain, redirect to correct server
    if (domains[hostname])
    {
        proxy.web(req, socket, {target : 'ws://' + domains[hostname]});
    }
    else
    {
        proxy.web(req, socket, {target : 'ws://' + domains["default"]});
    }
});

//Catch error to prevent proxy crash
proxy.on('error', function (err)
{
    console.error(err);
});

server.listen(80, function ()
{
    console.info('proxy listening on port 80');
});
person jaumard    schedule 14.03.2016