简体   繁体   中英

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

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. 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.

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.

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.

This isn't baked into node-http-proxy, but it's simple to code:

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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