I have a root server with Apache installed to serve my websites. The last weeks I was playing around a bit with NodeJs. Now the time has come where I want to serve a NodeJs app from my root server. Apache has the possibility to proxy to other servers. But if I proxy through Apache I’d loose a lot of what makes NodeJs special. So I had to find another way.
Luckily I am not the first person to run into this problem.
First install the nodeJs http-proxy.
npm install http-proxy
The next step is to run Apache on another port. I chose 9000. So edit /etc/apache2/ports.conf with your desired port. It does not really matter which one, just use one that is not occupied. You will also need to updat the ports in your vhosts. Do not forget to reload the Apache configuration after you are done:
sudo service apache2 reload
The I choose, configured and ran the http proxy with the proxy table listening on Port 9001.
node examples/http/proxy-table.js
Now everything needs to be wired up with iptables, just forward traffic from port 80 to 9001 and the node proxy with the table will do the rest.
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 9001
You can list the iptables rules with
sudo iptables -t nat -L -n -v
and if you need to you may remove rules like so:
sudo iptables -t nat -D PREROUTING INDEX
Where INDEX is the index of the rule.