2011-11-30 60 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.