Hosting multiple Express (node.js) apps on port 80
In the last days, i was trying to find a solution hosting multiple Express apps on my vServer the same Server.
Starting with Apache and mod_proxy, i ended up with a plain node solution, which i really like.
Let’s take a quick look on some different approaches out there:
—1—
Using apache on port 80 as a proxy
ProxyPass /nodeurls/ http://localhost:9000/ ProxyPassReverse /nodeurls/ http://localhost:9000/
via stackoverflow
– no websockets
++ probably the easiest way to integrate with your running AMPP-stack
—2—
Using a node.js app on port 80 as a Wrapper for other node apps.
express.createServer()
.use(express.vhost('hostname1.com', require('/path/to/hostname1').app)
.use(express.vhost('hostname2.com', require('/path/to/hostname2').app)
.listen(80)
via stackoverflow
++ you can use websockets on port 80
– apps crash/restart/stop globally
–what about your apache or the like?
—3—
Using node.js with node-http-proxy on port 80
var http = require('http')
, httpProxy = require('http-proxy');
httpProxy.createServer({
hostnameOnly: true,
router: {
//web-development.cc
'www.my-domain.com': '127.0.0.1:3001',
'www.my-other-domain.de' : '127.0.0.1:3002'
}
}).listen(80);
++ proxy websockets to any port
– you might need to move your old web server to another port
The really cool thing about using node-http-proxy is its capability of proxying websockets.
So you can have your apps running independtly on different ports while serving everything to the user over port 80 and use stuff like socket.io.
Since i’m new to node.js and miles away from beeing a admin, any feedback is highly appreciated :)

I took the node-http-proxy approach for my socket app and it works without any problems since then.
thanx
Hmm, why would you want to listen to port 80? First, you shouldn’t start node as privileged user. Instead create user to run service and redirect all packets with iptables to your preferred port:
and then route using either httpProxy or “router” app.
Using apache in front makes sense if you have other stuff running on server, like Tomacat.
would that approach still enable you to use websockets?
That was my main motivation for not using apache but node in front…
Hi, Im still a dummy on Node.js! :-)
Using the 3rd approach, when I need to add a new site, will I need to restart the main server.js script? Or there’s a way to implement a new app/site without interrupt all the nodejs apps?
Thanks for your comment – the 3rd solution was the best i could come up with in terms of app-independence. Of course you’ll have to restart the proxy – and you have a point there as this affects all apps availability – but the other apps can still keep running and keep sessions etc.
Plus no other app is involved when one app crashes…
Would a quick restart of the server really affect users of running apps that much – i think sockets are capable of reconnecting in such case, aren’t they?