So this is my attempt, an I'm stuck. I have an apache2 web server with over 10 virtual hosts. All domains are bound to the servers ip, and apache routes via virtual hosts, so every domain gets it's right content. That works since years already. Now the challenge: I now have some webApps, written with node.js on this server. When I want to run an app I have to call some link like: mynodeapp.com:3000 depending on the port I run the app. I now want node.js to do all the routing, instead of apache. I changed apache's listen to port 9000 and want to run a proxy written in node, which handles all the requests on port 80. I thought this is possible with node-http-proxy. I thought it's more like forwarding, so I tried the following for the node Proxy app:
httpProxy = require('http-proxy');
httpProxy.createServer({
forward: {
port: 9000,
host: 'localhost'
}
}).listen(80);
But this seems like forwarding everything independent of the req.host. Has anyone an Idea how to do that? I need to forward (or proxy) the incoming urls (my.domain.com) get them and forward them as a whole to port 9000 (where apache2 runs) so vhosts in apache can handle the rest.... any idea? Best Martin
UPDATE: Ok, so at the end everything works fine. It was in fact an apache problem (wrong folder destination.... how silly.) Below I post my currently pretty fine working solution it's pretty that simple:
var httpProxy = require('http-proxy');
var http = require('http');
var util = require('util');
var url = require('url');
var options = {
target: 'some-domain.com:9000',
target: 'another-domain.com:9000'
}
var proxy = httpProxy.createProxyServer(options);
http.createServer(function(req, res, err) {
if(err) console.log(err);
var host = util.inspect(req.headers.host);
console.log(req.connection.remoteAddress + ' asks for: ' + host + req.url);
proxy.web(req, res, function(err) {
if(err) console.log(err);
});
}).listen(80);
console.log('Awesome proxy is listening on port 80...');