44

What are the advantages of having nginx or another web-server running as a reverse-proxy in front of the Node.JS? What does it provide?

(This question is intended for matters concerning web-apps, not web-pages).

Thank you.

JJJ
  • 32,902
  • 20
  • 89
  • 102
Phil
  • 13,875
  • 21
  • 81
  • 126

3 Answers3

38

I think the greatest benefit is that you're then able to use the same port (80) for multiple applications. Otherwise, you'd need a new IP address for each nodejs application you have. Depending on how you set things up, you can also configure different folders and subdomains to different nodejs apps running on different ports. If you're building something big or complex, this is pretty great. Imagine being able to run your APIs on one node application, your website from another, and the logged-in website (member's area, dashboard, etc.) in another app. Your load balancer can determine who needs to go where (example.com/api* -> api.js, example.com/dashboard* -> dashboard.js, example.com -> app.js). This is not only useful for scaling, but also when things break, not everything breaks at once.

To the maturity thing, meh. Nodejs + forever + node-http-proxy = Amazing. Run 1 proxy server for all of your apps with a minimal config/complexity (lower chance of failure). Then have fun with everything else. Don't forget to firewall off your internal ports, though ;).

Some people make note of load balancing, which true, is a benefit. However, load balancing isn't something that most people will benefit from, since a single threaded, non-blocking nodejs thread can handle quite impressively large loads. I truly wouldn't even consider this as a difference if I were you. Load balancing is easy enough to implement when you need it, but otherwise utterly useless until you do.

Also note, if you do go with a non-node proxy solution (nginx, tornado, etc.), just be sure NOT to use one that blocks. Apache blocks. Nginx doesn't. You don't want to throw away one of the greatest benefits of using nodejs in the first place on a crummy server.

  • 1
    Hello, thanks for the response and explanations, it's very inspiring and contains great ideas. Can you please tell what would be the use case of "node-http-proxy" if nodee is just running on one machine? What would it do then? – Phil Jul 20 '11 at 20:28
  • 1
    If it's just running on 1 machine, then it's simply acting as a local proxy (just as Apache or Nginx would do with Node), except that it's built on node too! `:)` This means you keep complete control over your node-only stack, your applications require no modifications (they just work), and you can run many, many, many applications on a single IP address/port(80). For more complicated setups, you can build your own load balancer (how cool!?) and distribute the work the way that fits your application. –  Jul 22 '11 at 09:26
  • 1
    Right on! That sounds truly awesome. Cheers! – Phil Jul 22 '11 at 13:32
  • 1
    `since a single threaded, non-blocking nodejs thread can handle quite impressively large loads` -- I'm trying to find some real-world numbers backing this up but am coming up short. I've seen crazy-good and crazy-bad stats. Can you point to/provide any? TIA – Cory Mawhorter Nov 27 '12 at 03:46
  • Thanks for the elaboration Michael. I think in production scenarios many are using PM2 over Forever. @Cory Mawhorter I do not have a number as such. But you can try the robustness of your server with [wrk](https://github.com/wg/wrk) – Winster Dec 14 '15 at 06:29
  • Security and Flexibility in that order. I wouldn't trust node on the Internet without something fronting it that can do filtering, firewalling, proper logging, SSLing and whatnot. Apache sounds like the ticket. – David Tonhofer Mar 02 '18 at 14:33
10

Having a more mature software as proxy is better for security and reliability. Nginx, Apache and others have been tested against a multitude of cases and used in production for years.

You can also use features from these web server that otherwise you would have to implement yourself or use a node.js module. Like caching, statistics, balancing, etc.

On the other side you would lose some features from node.js, realtime features like websockets (on port 80, you can still use other ports), page buffering and depending on the reverse proxy used, control over your caching and headers.

Edit:

Diogo Gomes
  • 2,385
  • 1
  • 19
  • 29
  • Hello, it makes absolute sense. Thank you for sharing this great explanation. Can you please explain though, what kind of security and reliability advantages are we talking about in this case? – Phil Jul 20 '11 at 20:29
  • The security and reliability advantages come from the fact that those programs are older and have been used in production for more time than node.js, by general rule this means that more use cases have been tried and possible bugs have been fixed. It's a general rule though, there are exceptions. IMHO you can use node.js without a reverse proxy, just be careful with your code and always keep with the latest version. – Diogo Gomes Jul 21 '11 at 10:04
  • 3
    And write solid code. More often than not, security problems aren't related to your web server, but your application. Thus, it is not a flaw with nodejs if you write insecure code, no more than it's Apache's fault when people write bad PHP. This is very important to remember, and is where most people go wrong. –  Jul 22 '11 at 09:29
1

Reverse proxy really helps to improve the performance when you especially dealing with the SSL and gzip compression. Also, there are many other advantages. Thanks to Thomas Hunter II(Intrinsic). Read whole blog here https://medium.com/intrinsic/why-should-i-use-a-reverse-proxy-if-node-js-is-production-ready-5a079408b2ca

Kiran Mali
  • 597
  • 4
  • 12