[mini tutorial] "A quick and dirty way to redirect node-red to port 80"

3,927 views
Skip to first unread message

Luca PERENCIN

unread,
Mar 18, 2016, 9:02:38 PM3/18/16
to Node-RED

[repost from the slack channel, for people not there]


Testing the node-red, i start to think to use it to make some stand-alone boxes running the node-red-contrib-ui as front end, maybe with a small LCD screen or so (a-là timesquair.io, but with a screen)

So i tweak a bit the config to get the node-red working on localhost:80

Without running it on root mode.

first step:

few changes in the settings.js (or in a custom settings, to be launched with a -s parameter )

just uncomment the "httpAdminRoot" to force the node-red to move admin interface to localhost:1880/admin (or the path you like)

remember: with node-red-ui enabled, node-red serve UI interface to localhost:1880/ui

second step:

at this point i've managed to redirect ports from 1880 to 80 and /ui to /

for doing so, i've used haproxy (haproxy.org) a tiny, powerful reverse proxy and load balancer. just install it with

sudo apt-get install haproxy

haproxy work with a config file located in /etc/haproxy/haproxy.cfg.

This is the config i used:


global
	daemon
	maxconn 256

defaults
	mode http
	timeout connect 5000ms
	timeout client 50000ms
	timeout server 50000ms

frontend http-in
	bind *:80
	default_backend servers

backend servers
	reqrep ^([^\ :]*)\ /(.*)     \1\ /ui/\2
	server server1 127.0.0.1:1880 maxconn 32

The server just wait for connection un port 80 and bind it to localhost:1880/ui (you can easily manage to move/use another address )

after setting up the haproxy, you can simple start it as a service

sudo service haproxy start

done!

All request to localhost will be now rediected to /ui, causing the UI interface to appear by default.

of course admin page will be still available at localhost:1880/admin


If you want to keep the 1880 port and give a default page to /ui, you can uncomment the httpStatic to set a local path ('/home/pi' in my case) where place a static html page to serve for localhost:1880

i've done a small redirect html to move the path to /ui automatically


<html>
	<head>
		<title>Redirect</title>
		<meta http-equiv="refresh" content="0;URL=http:/ui">
	</head>
<body>
</body>
</html>

The only "problem" found is the management of error pages, but i've already opened an issue on github

have fun!

Dave C-J

unread,
Mar 19, 2016, 8:07:02 AM3/19/16
to node...@googlegroups.com
Luca,

node-red-contrib-ui does allow setting the path if you like...
add a section to your settings.js like

    ui: {
        path: "foo"
    },

to move it to /foo and so on...
Should be no need for haproxy unless there are other benefits you want.

Luca PERENCIN

unread,
Mar 19, 2016, 9:07:06 AM3/19/16
to Node-RED
Hi Dave, thanks
I miss this parameter, but, anyway, the port is still 1880 or a non standard port, due the security limitations for node js and port 80 (and running node on root is not exactly a safe way to run it )
On node environment, I'd like to test pm2 (https://github.com/Unitech/pm2 ) to see if it work in the same way. As described in subject, my tutorial is just "quick and dirty" to workaround a «problem»

Dave C-J

unread,
Mar 19, 2016, 1:56:42 PM3/19/16
to node...@googlegroups.com
Luca,

not a problem - also fairly easy to do a 

    sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 1880

to redirect the low numbered port 80 to 1880 - so you don't need to run as root. Even quicker and less dirty ;-)

Julian Knight

unread,
Mar 20, 2016, 8:58:56 PM3/20/16
to Node-RED
No you really shouldn't run anything other than a hardened server on any port below 1024 since that generally requires root privileges.

Of course, you don't need a proxy to do this if you already run a web server since it is easy enough to forward requests. All mainstream webservers can do this, at least with the help of a plugin. My personal preference is NGINX as a web server with Phusion Passenger to do the heavy lifting. Passenger also works with Apache and can run other services such as Ruby and Python too. It gives a great boost to performance & security and keeps the node.js service running, restarting it on crashes.

I've not tried it with Node-Red as such but it should work.
Reply all
Reply to author
Forward
0 new messages