Running Node.js and Apache Together using Mod_Proxy

Running Node.js and Apache Together using Mod_Proxy

Lets say that you want to experiment with Node.js or you want to start to leverage its awesome features but you don't want to throw away all of your existing Apache compatible website. This tutorial will show you how to configure your existing Apache server to forward requests for a specific folder to Node.js. I'm going to set it up so Node.js handles the /node/ folder and Apache handles everything else.

Assumptions and My Setup

Apache server running on port 80 Node.js server already running on port 8080 Ubuntu Server 10.04.4 LTS Your site is example.com.

Configure the Proxy

Enable the mod_proxy and mod_proxy_html Apache modules. They should be available by default so just enable them with the a2enmod command. sudo a2enmod proxy sudo a2enmod proxy_http

Now edit your virtual host file (/etc/apache2/sites-available/example.com.conf) to have reverse proxy settings. Within the node, add the following: ProxyRequests Off Order deny,allow Allow from all

ProxyPass /node http://example.com:8080 ProxyPassReverse /node http://example.com:8080

Save that and restart apache with: sudo service apache2 restart

Success

You should be able to navigate to http://example.com/node and you will magically get your node server's responses.

Troubles?

If you got a “503 Service Temporarily Unavailable” error, that means your node server isn't serving on port 8080.

Other Options

Of course you could do things the other way around and run Apache through Node.js which probably has some minor performance benefits. There are a couple of existing packages that do this however I have yet to try them.

node-http-proxy : https://github.com/nodejitsu/node-http-proxy Bouncy : http://substack.net/posts/5bd18d