简体   繁体   English

Node.JS前面反向代理的优点

[英]Advantages of a reverse proxy in front of Node.JS

What are the advantages of having nginx or another web-server running as a reverse-proxy in front of the Node.JS?在 Node.JS 前面作为反向代理运行 nginx 或其他 Web 服务器有什么优势? What does it provide?它提供什么?

(This question is intended for matters concerning web-apps, not web-pages). (这个问题是针对有关网络应用程序的问题,而不是网页)。

Thank you.谢谢你。

I think the greatest benefit is that you're then able to use the same port (80) for multiple applications.我认为最大的好处是您可以将同一个端口 (80) 用于多个应用程序。 Otherwise, you'd need a new IP address for each nodejs application you have.否则,您需要为您拥有的每个 nodejs 应用程序创建一个新的 IP 地址。 Depending on how you set things up, you can also configure different folders and subdomains to different nodejs apps running on different ports.根据您的设置方式,您还可以为在不同端口上运行的不同 nodejs 应用程序配置不同的文件夹和子域。 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.想象一下,能够在一个节点应用程序上运行您的 API,在另一个应用程序上运行您的网站,在另一个应用程序中运行登录网站(会员区、仪表板等)。 Your load balancer can determine who needs to go where (example.com/api* -> api.js, example.com/dashboard* -> dashboard.js, example.com -> app.js).您的负载均衡器可以确定谁需要 go where (example.com/api* -> api.js, example.com/dashboard* -> dashboard.js, example.Z4D236D9A2D102C5FE0Z5 -> app.) 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. Nodejs +永远+ node-http-proxy = 太棒了。 Run 1 proxy server for all of your apps with a minimal config/complexity (lower chance of failure).为您的所有应用程序运行 1 个代理服务器,配置/复杂性最低(失败的可能性更低)。 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.然而,负载平衡并不是大多数人会从中受益的东西,因为单线程、非阻塞 nodejs 线程可以处理非常大的负载。 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.另请注意,如果您使用非节点代理解决方案(nginx、龙卷风等)执行 go,请确保不要使用会阻塞的代理解决方案。 Apache blocks. Apache 块。 Nginx doesn't. Nginx 没有。 You don't want to throw away one of the greatest benefits of using nodejs in the first place on a crummy server.您不想放弃在糟糕的服务器上首先使用 nodejs 的最大好处之一。

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. Nginx、Apache 等已针对大量案例进行了测试,并已在生产中使用多年。

You can also use features from these web server that otherwise you would have to implement yourself or use a node.js module.您还可以使用这些 web 服务器中的功能,否则您必须自己实现或使用 node.js 模块。 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.另一方面,您将失去 node.js 的一些功能、websockets 等实时功能(在端口 80 上,您仍然可以使用其他端口)、页面缓冲以及根据使用的反向代理、对缓存和标头的控制。

Edit:编辑:

Reverse proxy really helps to improve the performance when you especially dealing with the SSL and gzip compression.当您特别处理 SSL 和 gzip 压缩时,反向代理确实有助于提高性能。 Also, there are many other advantages.此外,还有许多其他优点。 Thanks to Thomas Hunter II(Intrinsic).感谢 Thomas Hunter II(内在)。 Read whole blog here https://medium.com/intrinsic/why-should-i-use-a-reverse-proxy-if-node-js-is-production-ready-5a079408b2ca在此处阅读整个博客https://medium.com/intrinsic/why-should-i-use-a-reverse-proxy-if-node-js-is-production-ready-5a079408b2ca

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM