Proxying WebSockets on NodeJS running off Apache

Stack_Overflow

Disclaimer: I’m not a WebServer Admin/Pro and only tinker with stuff found around, so please excuse my ignorance or wrong jargon used here.

I had this terrible problem where a NodeJS app running on port 3000 on an Apache Server would refuse to proxy the incoming Web Sockets to the Node App. After almost two weeks of battling it out (including Server upgrades and losing out the entire app :), we finally managed to get it running). I’ll start off with my setup and then come over to the solution. If you’d like to directly jump to the solution, please scroll down.

Background:

I’m building an IoT gateway that allows for bi-directional communication between nodes/sensors (Arduino based) and decided to experiment with NodeJS to build the Backend (and Frontend) server. Since I’m moderately comfortable with Apache (and running WordPress etc on it), the first thing I did was install Apache on my Debian 7 instance which is running off on Linode. (The folks at Linode have a bunch of awesome tutorials that take you through the process of getting started with a Server, configuring (L)AMP stack and much more).

Finally, we got our NodeJS App running on Port 3000 (looks like that port is a default standard out there) and our Apache server live on port 80.

Now came the funny part: We needed a sub domain which would redirect to the NodeJS app running on Port 3000. We played around a while with Virtual Hosts in the Apache Site config file and all went well… Until I found out that Web Sockets weren’t being properly proxied back to port 3000 from sub.domain.com while domain.com:3000 would work just fine.

Solution

After banging my head around for many days (2-3 weeks in-fact), I found the following solution on StackOverflow: http://stackoverflow.com/questions/27526281/websockets-and-apache-proxy-how-to-configure-mod-proxy-wstunnel

It turned out, I was running Apache 2.2 that didn’t support mod_proxy_tunnel module and had to either build it off the Apache 2.4 sources and patch it or get the instance running on Debian 8 which supports Apache 2.4 out of the box. I thought building it from scratch on Jessie (Debian 8) would be a better solution instead of patching and messing around.

After a couple of hours, finally got those Web Sockets working.

Stack_Overflow

Pictured above is a screengrab of the conf file that we had to modify to get Web Sockets proxy working (after enabling (mod_proxy  and mod_proxy_wstunnel).

Since my NodeJS app was handling WebSockets at sub.domain.com/api/ws, I decided to only ProxyPass that particular path instead of the document root (as shown in stackoverflow answer above).

Now I have a nice sub.domain.com server listening/tunneling (I don’t even know much about Web Sockets yet to use the right word here) to Web Sockets of a NodeJS app on port 3000.

Advertisements

Author: Anwaarullah

Designer, Developer, Maker, Speaker, Educator and Dreamer. ♥ Tinkering with Embedded, Android, Open Soft/Hard/Firm ware, Robotics and 3D Printer. Talk to me at syed {Shift+2} anwaarullah dash com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s