2011-04-19 61 views
4

我真的很喜歡的node.js,但it's真的複雜,當你想運行多個服務器的WebSocket,讓他們遍佈端口進行訪問80Node.js - 具有WebSocket代理和SSL支持的良好WebServer?

我目前正在運行的nginx的,但代理到不同傳入WebSocket連接websocket服務器取決於url是不可能的,因爲nginx不支持http 1.1。

我試圖實現一個具有我自己的功能的網絡服務器,但是它涉及到頭傳遞等時非常複雜。另一件事是SSL支持。支持它並不容易。

那麼,有沒有人知道一個很好的解決方案來做我提到的事情?

感謝您的幫助!

回答

8

我使用nodejitsu的node-http-proxy獲得了很好的結果。正如他們的自述中所述,他們似乎支持WebSockets。

舉例的WebSockets(從GitHub的自述拍攝):

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

// 
// Create an instance of node-http-proxy 
// 
var proxy = new httpProxy.HttpProxy(); 

var server = http.createServer(function (req, res) { 
    // 
    // Proxy normal HTTP requests 
    // 
    proxy.proxyRequest(req, res, { 
    host: 'localhost', 
    port: 8000 
    }) 
}); 

server.on('upgrade', function(req, socket, head) { 
    // 
    // Proxy websocket requests too 
    // 
    proxy.proxyWebSocketRequest(req, socket, head, { 
    host: 'localhost', 
    port: 8000 
    }); 
}); 

它的生產使用應該沒有問題,因爲它是用於nodejitsu.com。要將代理應用程序作爲守護程序運行,請考慮使用forever

+0

感謝這一個看起來不錯! – 2011-04-19 13:49:41

+0

好吧,它工作正常!非常感謝你! – 2011-04-19 14:27:38

1

較新版本的nginx實際上會支持http/1.1的反向代理。你可能想要1.1.7或更高版本。

嘗試這樣的事情在你的配置:

location/{ 
    chunked_transfer_encoding off; 
    proxy_http_version 1.1; 
    proxy_pass  http://localhost:9001; 
    proxy_buffering off; 
    proxy_set_header X-Real-IP $remote_addr; 
    proxy_set_header Host $host:9001; #probaby need to change this 
    proxy_set_header Connection "Upgrade"; 
    proxy_set_header Upgrade websocket; 
} 

這個好處是,你可以在nginx的終止SSL。