2011-11-30 59 views
3

我需要爲3個域使用Node.JS。我該怎麼做?目前我有一個綁定端口80的應用程序,如何支持多個域? 我還使用了在3個進程中分派我的應用程序的集羣模塊。如何處理多個域名?

回答

2

可能是最好的方法,使用connect vhost,這是一個連接模塊。

或:你可以與全球的URL處理器重寫網址,然後寫基於改寫的網址您的約束:

app.get('*', function(req, res, next){ 
    if(req.headers.host === 'domain1.com') 
    req.url = '/domain1' + req.url; 
    else if(req.headers.host === 'domain2.com') 
    req.url = '/domain2' + req.url; 
    next(); 
}) 

.get('/domain1/index', function(){ 

}) 

.get('/domain2/index', function(){ 

}); 
2

使用https://github.com/nodejitsu/node-http-proxy

您需要在端口80上運行反向代理(假設您使用HTTP而非HTTPS),然後將請求路由到不同的服務(即節點服務器)。實際的節點服務器將使用非標準端口進行監聽。

e.g

Service A (for domain A) - 8001 
Service B (for domain B) - 8002 
Service C (for domain C) - 8003.