If you are learning or using nodejs, most of you might encounter the problem of running nodejs with apache or nginx. There are several solutions for this issue, with several pros and cons for each solution. For this article I’ll only consider we have a web server with apache.

some of the suggested solutions are:

  1. use a reverse proxy with apache (with mod_proxy) to serve nodejs apps
  2. disable apache and run nodejs on port 80 😀
  3. run nodejs on port 80 with a reverse proxy configured on nodejs

If you go with the first method, you are really wasting the power of nodejs. but if you do a simple google search on this, lots of articles will suggest you or show you to go with this approach. I am not going to explain this in depth. Following extracted text will explain what is the downside of this approach very well. 🙂

You just bought a brand new ferrari and you want to take it for a spin on the highway. So you go there. but there is a big fat truck in front of you that only goes 50mph and there is no way to get passed it.

That is basically what you are doing if you put apache (truck) in front of nodejs (ferarri) this defeats one of the main purposes of using nodejs having a FAST evented machine. Which does not allocate threads for requests.

In the second approach, the only downside is you will lose your apache server. So if you run some CMS-es like wordpress / drupal you have to find other alternative options like our 3rd solution.

So the 3rd option, running a reverse proxy with nodejs. This idea is really crazy. 😀 Following are the steps to do this.

  1. first, we have to  make apache run on different port other than port 80.
  2. then configure nodejs to run on port 80.
  3. create the proxy script with redbird.
  4. configure PM2 to auto the start proxy server.

and we are done.

step 1: Make apache run on different port

First open ports.conf in /etc/apache2/ directory.

sudo nano /etc/apache2/ports.conf

Then change Listen 80 to Listen 8080
So the ports.conf will look like following.

Listen 8080

<IfModule ssl_module>
	Listen 8443

<IfModule mod_gnutls.c>
	Listen 8443

Next open 000-default.conf in /etc/apache2/sites-available directory.
change <VirtualHost *:80> to the previously given port as <VirtualHost *:8080> and restart the apache server to reflect the changes.

sudo service apache2 restart

step 2: Configure nodejs to run on port 80

To run nodejs on port 80, the script will need sudo permission. But there are several ways to do this without sudo permission, which is more safer. We can bind nodejs to listen on lower ports (1 – 1024) by following commands.

sudo apt-get install libcap2-bin
sudo setcap cap_net_bind_service=+ep /usr/local/bin/node

Once you do this, we can listen to all lower ports including port 80 via our nodejs app.

step 3: Create the proxy script

Now we have to write the reverse proxy script. For an example let’s say we have a wordpress blog running with apache on http://mydomain.me:8080/blog/ (in the first step, I made apache run on port 80) and a nodejs app running on http://mydomain.me:3000/ and we want to make both of these apps available on port 80.

To write the proxy script we will be using a npm module called redbird. We can begin writing the script as follows.


var proxy = require('redbird')({port: 80, xfwd: false);

proxy.register("mydomain.me/blog", "http://mydomain.me:8080/blog");
proxy.register("mydomain.me", "http://mydomain.me:3000");

Note that, this reverse proxy script is set to run on port 80. Now if you run this script with node proxy.js we’ll be able to access our blog via http://mydomain.me/blog/ and our node app on port 3000 via http://mydomian.me/. 🙂

step 4: Configure PM2 to auto start the proxy server

Next step is to making this proxy script auto-start when the system boots. There is a great tool which will do this job for us called PM2. Install and configure it if you haven’t already used it.


sudo npm install -g pm2
pm2 startup ubuntu

This last line will output a command for you to run on the terminal to make PM2 autostart on system boot. Run it and you are almost done.

Next step would be adding our proxy script to process list of PM2. Then PM2 will take care of our script for us.

pm2 start proxy.js

also via PM2 we can start / stop / restart the process like we use services in linux.



howdy fellas! 🙂 today i had to setup apache reverse proxy with my transmission web interface. in my university wifi all ports are blocked other than port 80 and in order to access my transmission server i had to either setup an apache reverse proxy or make transmission daemon run on port 80.

for this, first you have to enable apache mod_proxy and mod_proxy_http modules.

sudo a2enmod proxy
sudo a2enmod proxy_http

next you need to edit the apache configuration file. so first open it with nano.

cd /etc/apache2/sites-available
sudo nano 000-default.conf

your default configuration file name might not same as 000-default.conf. provide the appropriate name.

next you need to add following configuration block to the conf file. here our goal is to access transmission web interface via http://mydomain.me/transmission instead of http://mydomain.me:9091

ProxyRequests Off
<Proxy *>
        AddDefaultCharset off
        Order Allow,Deny
        Allow from all

ProxyPass /transmission http://mydomain.me:9091/transmission
ProxyPassReverse /transmission http://mydomain.me:9091/transmission

ProxyVia On

following isn’t the reason for HTTP 409 conflict error.
one thing to note is don’t add http://localhost:9091/transmission instead of http://mydomain.me:9091/transmission. in my configuration putting localhost instead of actual domain generated HTTP 409 conflict error.

save the file & exit. next restart the apache server and you are almost done.

sudo service apache2 restart

now you can access your transmission web interface via http://mydomain.me/transmission/web/. 🙂 so happy reverse proxying.

update *
note that you have to access transmission web interface via the exact url as above. if you loose or don’t add /web/ part you’ll receive HTTP 409 conflict error.