我想做一个简单的 Node.js 反向代理来托管多个 Node.JS 应用程序以及同一端口 80 上的 apache 服务器。所以我找到了这个示例here http://nerdpress.org/2012/04/20/hosting-multiple-express-node-js-apps-on-port-80/
var http = require('http')
, httpProxy = require('http-proxy');
httpProxy.createServer({
hostnameOnly: true,
router: {
'www.my-domain.com': '127.0.0.1:3001',
'www.my-other-domain.de' : '127.0.0.1:3002'
}
}).listen(80);
问题是我想让 app1.my-domain.com 指向 localhost:3001,app2.my-domain.com 指向 localhost:3002,而所有其他端口都指向端口 3000,例如我的 apache 服务器将会运行。我在文档中找不到有关如何拥有“默认”路线的任何内容。
有任何想法吗?
EDIT我想这样做是因为我的 apache 服务器处理了很多域/子域,并且我不想每次想要添加新子域时都必须修改此路由表。
近一年来,我已经成功地使用了接受的答案来拥有默认主机,但是现在有一种更简单的方法,node-http-proxy 允许在主机表中使用正则表达式。
var httpProxy = require('http-proxy');
var options = {
// this list is processed from top to bottom, so '.*' will go to
// '127.0.0.1:3000' if the Host header hasn't previously matched
router : {
'example.com': '127.0.0.1:3001',
'sample.com': '127.0.0.1:3002',
'^.*\.sample\.com': '127.0.0.1:3002',
'.*': '127.0.0.1:3000'
}
};
// bind to port 80 on the specified IP address
httpProxy.createServer(options).listen(80, '12.23.34.45');
您没有的要求hostnameOnly
设置为 true,否则将不会处理正则表达式。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)