WebSocket Apache Proxy

I recently installed a cool tool called Node-RED which is a neat visual coding tool for IoT-type devices. It uses node.js, and listens on port 1880.  However, I wanted Node-RED to function over port 80, to avoid having to type a port as part of the URL. I struggled getting the WebSocket connection to function correctly. I kept getting a HTTP response of 500.

 

I figured I might be able to save some other folks the trouble (and help me remember for next time). To get this to work on a system running Apache, you’re going to need to enable a few modules.  I’m running Apache 2.4 on Raspbian Jessie for context.

sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_wstunnel
sudo a2enmod proxy_html
sudo a2enmod proxy_fcgi

 

And configure your site listener to include the lines:

ProxyPreserveHost On

ProxyRequests Off


ProxyPass /nodered http://localhost:1880

ProxyPass /nodered/comms ws://localhost:1880/comms


ProxyPassReverse /nodered http://localhost:1880

ProxyPassReverse /nodered/comms ws://localhost:1880/comms

 

I learned where the WebSocket connection failed by watching the Javascript console in Chrome – it points you to the /comms directory, so you need to enable the WebSocket proxy there.

This way, you can simply navigate to your Node-RED installation on port 80 by simply using the URL http:///nodered/

Comments

2 responses to “WebSocket Apache Proxy”

  1. 부천출장마사지 Avatar

    I used to be able to find good advice from your articles.

  2. speakerdeck.com Avatar

    I don’t even know how I ended up here, but I thought this post was great.
    I don’t know who you are but certainly you’re going to a famous blogger if you are not already 😉 Cheers!