node.js proxy to multiple services

61 views
Skip to first unread message

siliconplains

unread,
Oct 30, 2015, 11:54:35 AM10/30/15
to nodejs
I need to setup a node.js service that runs on port 443 https, that proxies out to multiple other node.js services that are hosted against differnet ports but all run https.

Does anyone have any examples of how to setup a node proxy server to accmomplish this?  I have exhausted examples online.  I have looked all over the place for an example of how to do this and I find no examples of how to configure this.

Thanks in advance,
Allan

Jimb Esser

unread,
Oct 30, 2015, 4:17:42 PM10/30/15
to nodejs
Only your front end service, listening on 443, needs to be https, as it will handle decrypting of the client's request.  When you proxy to other services, if they're internal/on your own network, there's no reason to use https on the back end (just adds a ton of performance overhead), however if they're on a public network or for some reason you want additional internal security, you can use https, however it will be a https handshake between your front end and the back end, *not* between the user's browser and the back end (this matters if you need to get at the user's https client certificates or something, you'd have to do that on your front end and forward appropriate information along).

As for how to do it, it's basically the same whether it's http or https.  The simplest is probably going to be to use the http-proxy module, the examples there should work whether you're using "http.createServer" or "https.createServer".

I wrote a blog post a while back detailing my solution (which does more than just simple proxying, uses express for ease of managing routes, does some URL re-writing to just proxy sub-directories transparently, and static file serving on the front end).  It's available here: https://jimbesser.wordpress.com/2014/10/20/its-node-js-all-the-way-down/

Note that if performance is of the utmost importance, you'll want to use a dedicated piece of software designed for this, such as NGINX, last I saw it was still significantly faster than a Node app for the specific task of https handshake + serve static files + proxy to other services.  But, Node's still pretty darn fast, and it's likely other performance bottlenecks will far outweigh this difference, and it's nice to have everything in the same framework.

Hope this helps!
  Jimb
Reply all
Reply to author
Forward
0 new messages