1

I'm setting up a node.js server but I would also like to have Apache running on there at the same time. Node is going to be the main website, and there will be subdomains that point to Apache.

The only way I can think of how to do this is have the different applications listen to different ports and then have a proxy application that listens to port 80 and then "redirects" the port according to the subdomain used. I'm not sure if this is the right way to do it, or how to do it if it is.

Research has shown me that it could be possible to use Apache as this proxy, though I would prefer it if I didn't have to. If I could somehow use node.js to do it, that would be fantastic (my preferred solution). If that is impractical/impossible, then of course I am open to other ideas.

I would really appreciate some guidance as to how to do this.

Pinpickle
  • 984
  • 2
  • 9
  • 18

2 Answers2

1

Have a look through this thread

While it discusses some issues using http-proxy with WebSockets on <= 0.8.x, if you aren't doing that, you should be fine.

You can create a very basic proxy listener like so:

var http = require('http'),
    httpProxy = require('http-proxy');

httpProxy.createServer(8888, 'localhost').listen(80);

And create a back-end server like so:

var http = require('http').listen(8888);

But of course, more complex implementations can be accomplished by reading the http-proxy documentation.

Community
  • 1
  • 1
brandonscript
  • 68,675
  • 32
  • 163
  • 220
  • I'm on node 0.10.13 and will probably want to use web sockets of some description in the future so for full compatibility, I'll have to go with their caronte tree. Is that recommended? – Pinpickle Dec 14 '13 at 06:34
  • Uhh... not until we get it working! So far we haven't. If you play around with it and get it working, feel free to write up a solution on the other thread! – brandonscript Dec 14 '13 at 06:36
1

You wanted a solution that can serve both Node.js and Apache at the same time, and you wanted to have Node.js to do the reverse proxy. However, it is best to use a program that is designed for reverse proxy (Nginx, HAProxy) for that job. Using Node.js as a reverse proxy server will be inefficient.

Nginx is something I recommend. It is simple and highly efficient. You can have the Nginx server at the very front, taking in all the requests.

Here is how you setup Nginx to reverse proxy to Node

http://www.nginxtips.com/how-to-setup-nginx-as-proxy-for-nodejs/

And here is how to setup Nginx to reverse proxy to Apache

http://www.howtoforge.com/how-to-set-up-nginx-as-a-reverse-proxy-for-apache2-on-ubuntu-12.04

Simply combine the setting files of the two will enable you to serve apache and node at the same time.

leorex
  • 2,058
  • 1
  • 14
  • 15
  • This seems to be working quite well. I've noticed that Nginx adds some latency (~50ms) to my responses on the Node server. Is there any way to lower this or is it something I will just have to accept? Using Node as the reverse proxy will probably eliminate this latency (but will add even more to the Apache side if my understanding is correct). – Pinpickle Dec 18 '13 at 07:20
  • Nginx will give u an overhead, but this overhead is largely constant. Unless you have a strict real time performance requirement to meet, I would highly recommend you to maintain the current setup. Reverse proxy with node will not add more load to the Apache side, but your node now has to reverse proxy and serve at the same time on a single thread. You can do some Apache Benchmark testing to verify the setup, but I say stick with Nginx if you can. – leorex Dec 19 '13 at 20:20