2016-07-29 17 views
1

私は私のニーズに任意のドキュメント/答えを見つけることができませんでした。HTTPプロキシ・ルールとWebSocketを

私は漬物のようなものでね。私は新しいサービス(websocketサーバー)を作成してモジュールを拡張できるwebsocketアプリケーションを開発しています。もちろんこれは接続するポートが増えることを意味します。問題は、私たちの企業ポリシーは唯一のポートしか開いていないので、私の要求をプロキシする必要があるということです。

私はnginxのを使用するように言って、多くの回答を読んだが、私は単純にすることはできません。最初は私は窓を走らせています、私たちの会社は使えるものと使用できないものが非常に厳格です。私は任意のノードモジュールをインストールすることができます。私はhttp-proxy-rulesと一緒にhttp-proxyモジュールを使用しようとしました。

の問題は、私はすべてのWebSocket要求に404年代を取得しています、です。私はデフォルトのプロキシ(ソケットではない通常のWebサービス用)が100%うまく動作していることに注意します。

var servPath = (cliSettings["AppPaths"]["Admin"] == null) ? 'http://' + window.location.hostname + ':5050' : cliSettings["AppPaths"]["Admin"], 
    AdminIO = new io(servPath, { 
     extraHeaders: { 
      Service: "Admin" 
     }, 
     path: '/ws/admin'}) 

...

とのWebSocketサーバはこのように呼ばれている:

var http = require('http'), 
     httpProxy = require('http-proxy'), 
     HttpProxyRules = require('http-proxy-rules'); 

    // Set up proxy rules instance 
    var proxyRules = new HttpProxyRules({ 
    rules: { 
     '.*/ws/admin': 'http://localhost:26266', // Rule for websocket service (admin module) 
     '.*/ws/quickquery': 'http://localhost:26265' // Rule for websocket service (quickquery module) 
    }, 
    default: 'http://Surface.levisinger.com:8080' // default target 
    }); 

    // Create reverse proxy instance 
    var proxy = httpProxy.createProxy(); 

    // Create http server that leverages reverse proxy instance 
    // and proxy rules to proxy requests to different targets 
    http.createServer(function(req, res) { 

    // a match method is exposed on the proxy rules instance 
    // to test a request to see if it matches against one of the specified rules 
    var target = proxyRules.match(req); 
    if (target) {  
     //console.log(req); 
     console.log("Returning " + target + " for " + req.headers.host); 
     return proxy.web(req, res, { 
     target: target, 
     ws: true 
     });  
    } 

    res.writeHead(500, { 'Content-Type': 'text/plain' }); 
    res.end('The request url and path did not match any of the listed rules!'); 
    }).listen(5050); 

のWebSocketに接続している私のクライアントコードは次のようになります。

は、ここに私の現在のコードです:

io = require('socket.io').listen(26266,{ path: '/ws/admin'}) // can use up to 26484 

私は本当にここに誰かがアイデアを持っていることを願っています。ありがとう!

答えて

0

これを行う方法を理解しました。これにはいくつかのことがあります... 1.プロキシしたい場合は、カスタムwebsocketパスを使用する必要があります。 2. Websocketをプロキシするときには、パス全体を指定する必要があります。 3. websocket(ws)トラフィックを処理するイベントを指定する必要があります。

ここではすべての例を示します。

+++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++

プロキシサーバー:

var ini = require('node-ini'), 
     conf = ini.parseSync('../settings/config.ini'), 
     http = require('http'), 
     httpProxy = require('http-proxy'), 
     HttpProxyRules = require('http-proxy-rules'); 

    // Set up proxy rules instance 
    var proxyRules = new HttpProxyRules({ 
    rules: { 
     '.*/ws/remote': 'http://' + conf["Server"]["binding"] + ':26267/ws/remote', 
     '.*/ws/admin': 'http://' + conf["Server"]["binding"] + ':26266/ws/admin', // Rule for websocket service (admin module) 
     '.*/ws/quickquery': 'http://' + conf["Server"]["binding"] + ':26265/ws/quickquery' // Rule for websocket service (quickquery module)  
    }, 
    default: 'http://' + conf["Server"]["binding"] + ':8080' // default target 
    }); 

    // Create reverse proxy instance 
    var proxy = httpProxy.createProxy(); 

    // Create http server that leverages reverse proxy instance 
    // and proxy rules to proxy requests to different targets 
    var proxyServer = http.createServer(function(req, res) { 

    // a match method is exposed on the proxy rules instance 
    // to test a request to see if it matches against one of the specified rules 
    var target = proxyRules.match(req); 
    if (target) {  
     //console.log(req.url); 
     //console.log("Returning " + target + " for " + req.url); 
     return proxy.web(req, res, { 
     target: target, 
     ws: true 
     });  
    } 

    res.writeHead(500, { 'Content-Type': 'text/plain' }); 
    res.end('The request url and path did not match any of the listed rules!'); 
    }).listen(conf["Server"]["port"]); 

// 
// Listen to the `upgrade` event and proxy the 
// WebSocket requests as well. 
// 
proxyServer.on('upgrade', function (req, socket, head) { 
    var target = proxyRules.match(req); 
    if (target) { 
     return proxy.ws(req, socket, head, { target: target });  
    } 
}); 
process.on('SIGINT', function() { 
    db.stop(function(err) { 
    process.exit(err ? 1 : 0); 
    }); 
}); 

のWebSocketサーバーソケットリスナー:

io = require('socket.io').listen(26266,{ path: '/ws/admin'}); 

クライアントページからのWebSocketに接続します。

AdminIO = new io({path: '/ws/admin'}); 

++++++++++++++++++++++++++++++++++ +++++++++++ ++++++++++++++++++++++++++++++++++++++ + + + + + + +

上記の例は、ポート26266でポート26266で実行されている私の "admin"接続をプロキシします。(もちろん、あらゆる状況で443/SSLを使用することをおすすめします。少し複雑です)。

これが誰かを助けることを願っています!

関連する問題