简体   繁体   English

使用node-http-proxy的默认路由?

[英]Default route using node-http-proxy?

I want to do a simple node.js reverse proxy to host multiple Node.JS applications along with my apache server on the same port 80. So I found this example here 我想做一个简单的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);

The problem is that I want to have for example app1.my-domain.com pointing to localhost:3001, app2.my-domain.com pointing to localhost:3002, and all other go to port 3000 for example, where my apache server will be running. 问题是我希望例如app1.my-domain.com指向localhost:3001,app2.my-domain.com指向localhost:3002,而所有其他指向port 3000例如,我的apache服务器将要运行。 I couldn't find anything in the documentation on how to have a "default" route. 我在文档中找不到有关如何使用“默认”路由的任何内容。

Any ideas? 有任何想法吗?

EDIT I want to do that because I have a lot of domains/subdomains handled by my apache server and I don't want to have to modify this routing table each time I have want to add a new subdomain. 编辑我想这样做,因为我有很多域/子域由我的apache服务器处理,我不希望每次我想添加一个新的子域时都要修改这个路由表。

For nearly a year, I had successfully used the accepted answer to have a default host, but there's a much simpler way now that node-http-proxy allows for RegEx in the host table. 近一年来,我成功地使用了已接受的答案来拥有一个默认主机,但是现在node-http-proxy允许在主机表中使用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');

The requires that you do NOT have hostnameOnly set to true, otherwise the RegEx would not be processed. 要求您没有将hostnameOnly设置为true,否则将不会处理RegEx。

This isn't baked into node-http-proxy, but it's simple to code: 这没有融入node-http-proxy,但代码很简单:

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);

If you visit localhost on port 8000 it will proxy to localhost port 8081. 如果您在端口8000上访问localhost,它将代理到localhost端口8081。

If you visit 127.0.0.1 on port 8000 (which is not defined in our routing hash) it will go to the default 'location', namely xkcd.com on port 80. 如果您在端口8000上访问127.0.0.1(未在我们的路由哈希中定义),它将转到默认的“位置”,即端口80上的xkcd.com。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM