1

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

2 Answers 2

1

Apache's name virtual host looks at the Host field in the request header. They can be all running on the same IP and port.

NameVirtualHost *:9000

<VirtualHost *:9000>
  DocumentRoot /var/www/site1
  ServerName site1.com
</VirtualHost>

<VirtualHost *:9000>
  DocumentRoot /var/www/site2
  ServerName site2.com
</VirtualHost>

The ServerName directive tells the virtual servers apart

In your proxy code, make sure you populate the "host" field correctly.

Sign up to request clarification or add additional context in comments.

Comments

0

I run into a similar solution, leaving the node apps proxied behind apache, but running through apache 80 as standar vhosts and connecting the node.js app:

https://stackoverflow.com/a/30287224/3617531

Hope it could help anyone

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.