Node.js 使用节点http代理的默认路由?
我想做一个简单的node.js反向代理,在同一端口80上承载多个node.js应用程序和我的apache服务器。所以我找到了这个例子 问题是,我想让app1.my-domain.com指向localhost:3001,app2.my-domain.com指向localhost:3002,所有其他的都转到端口3000,例如,我的apache服务器将在那里运行。我在文档中找不到关于如何拥有“默认”路由的任何内容 有什么想法吗Node.js 使用节点http代理的默认路由?,node.js,node-http-proxy,Node.js,Node Http Proxy,我想做一个简单的node.js反向代理,在同一端口80上承载多个node.js应用程序和我的apache服务器。所以我找到了这个例子 问题是,我想让app1.my-domain.com指向localhost:3001,app2.my-domain.com指向localhost:3002,所有其他的都转到端口3000,例如,我的apache服务器将在那里运行。我在文档中找不到关于如何拥有“默认”路由的任何内容 有什么想法吗 编辑我想这样做,因为我的apache服务器处理了很多域/子域,我不想每次我
编辑我想这样做,因为我的apache服务器处理了很多域/子域,我不想每次我想添加一个新子域时都要修改这个路由表。这不是烘焙到节点http代理中的,但编码很简单:
var httpProxy = require('http-proxy'),
http = require('http'),
addresses;
// routing hash
addresses = {
'localhost:8000': {
host: 'localhost',
port: 8081
},
'local.dev:8000': {
host: 'localhost',
port: 8082
},
'default': {
host: 'xkcd.com',
port: 80
}
};
// create servers on localhost on ports specified by param
function createLocalServer(ports) {
ports.forEach(function(port) {
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('<h1>Hello from ' + port + '</h1');
}).listen(port);
});
console.log('Servers up on ports ' + ports.join(',') + '.');
}
createLocalServer([8081, 8082]);
console.log('======================================\nRouting table:\n---');
Object.keys(addresses).forEach(function(from) {
console.log(from + ' ==> ' + addresses[from].host + ':' + addresses[from].port);
});
httpProxy.createServer(function (req, res, proxy) {
var target;
// if the host is defined in the routing hash proxy to it
// else proxy to default host
target = (addresses[req.headers.host]) ? addresses[req.headers.host] : addresses.default;
proxy.proxyRequest(req, res, target);
}).listen(8000);
var-httpProxy=require('http-proxy'),
http=require('http'),
地址;
//路由散列
地址={
“本地主机:8000”:{
主机:“localhost”,
端口:8081
},
“local.dev:8000”:{
主机:“localhost”,
端口:8082
},
“默认值”:{
主持人:“xkcd.com”,
港口:80
}
};
//在由param指定的端口上的localhost上创建服务器
函数createLocalServer(端口){
forEach(函数(端口){
http.createServer(函数(req,res){
res.writeHead(200,{'Content-Type':'text/html'});
res.end('Hello from'+端口+''+地址[from].host+':'+地址[from].port];
});
httpProxy.createServer(函数(req、res、proxy){
var目标;
//如果主机是在路由哈希代理中定义的,则将其发送到
//默认主机的else代理
target=(addresses[req.headers.host])?addresses[req.headers.host]:addresses.default;
代理请求(请求、恢复、目标);
}).听(8000);
如果访问端口8000上的localhost,它将代理到localhost端口8081
如果您访问端口8000上的127.0.0.1(未在我们的路由散列中定义),它将转到默认的“位置”,即端口80上的xkcd.com。近一年来,我成功地使用了公认的答案来拥有默认主机,但现在有一种更简单的方法,节点http代理允许主机表中的RegEx
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,否则将不会处理正则表达式。谢谢。我会接受这个答案,因为它是最新的。看起来自从这个答案以来,该功能已经转移到一个单独的模块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');