RSS Feed
^__^

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 :)

7 Responses to “Hosting multiple Express (node.js) apps on port 80”

  1. Ivo Bathke says:

    I took the node-http-proxy approach for my socket app and it works without any problems since then.
    thanx

  2. 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:

    # /sbin/iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 9000
    # /sbin/iptables -A FORWARD -p tcp --dport 9000 -j ACCEPT
    

    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.

  3. Max Girkens says:

    would that approach still enable you to use websockets?
    That was my main motivation for not using apache but node in front…

  4. Alex says:

    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?

    • Max Girkens says:

      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?

    • Pablo says:

      Haven’t implemented yet, but I can see 3rd solution is a working proof-of-concept. You should be able to implement dynamic routing and config persistence on top of it, so you can modify route tables without restarting the server.

  5. [...] via Hosting multiple Express (node.js) apps on port 80 | Nerdpress.org. [...]

Leave a Reply