Ale*_*lin 3 node.js node-http-proxy
我想做一个简单的node.js反向代理,在同一个端口80上托管多个Node.JS应用程序以及我的apache服务器.所以我在这里找到了这个例子
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);
Run Code Online (Sandbox Code Playgroud)
问题是我希望例如app1.my-domain.com指向localhost:3001,app2.my-domain.com指向localhost:3002,而所有其他指向port 3000例如,我的apache服务器将要运行.我在文档中找不到有关如何使用"默认"路由的任何内容.
有任何想法吗?
编辑我想这样做,因为我有很多域/子域由我的apache服务器处理,我不希望每次我想添加一个新的子域时都要修改这个路由表.
Ala*_*vey 10
近一年以来,我已经成功地使用接受的答案有一个默认的主机,但现在有一个更简单的方式,节点-HTTP代理允许正则表达式中的主机表.
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');
Run Code Online (Sandbox Code Playgroud)
要求您没有hostnameOnly设置为true,否则将不会处理RegEx.