Node.js 能否完全替代 Apache 或 NGINX 等解决方案?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12050704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:12:04  来源:igfitidea点击:

Can Node.js fully replace solutions like Apache or NGINX?

node.js

提问by sebpiq

About deploying a Node.js application, I have seen a lot of tutorials showing it deployed side-by-side with Nginx, with more or less pretty tricks to allow the 2 to work together (and annoying stuff, like Nginx not supporting WebSockets). This seems a bit complicated to me ...

关于部署 Node.js 应用程序,我看过很多教程,显示它与 Nginx 并排部署,或多或少有一些漂亮的技巧来允许两者一起工作(以及烦人的东西,比如 Nginx 不支持 WebSockets) . 这对我来说似乎有点复杂......

Why does everybody do this kind of setup ? Does deploying Nginx when you have Node.js provide any advantage ? Can't you serve static files with Node.js ?

为什么每个人都做这种设置?当你有 Node.js 时部署 Nginx 有什么好处吗?你不能用 Node.js 提供静态文件吗?

I've written a lot of apps in Django, and the doc says that you shouldn't use Django to serve static files cause it's not optimized for this and so on ... so I was thinking maybe this is the reason.

我用 Django 编写了很多应用程序,文档说你不应该使用 Django 来提供静态文件,因为它没有为此优化等等......所以我想也许这就是原因。

采纳答案by 3on

Well some people don't mind using Node instead of nginx. Certain clouds like dotCloudor Nodejistuuses gateways entirely written in Node.js. Mostly to be able to handle websockets. But also because Node.js is damn fast.

好吧,有些人不介意使用 Node 而不是 nginx。某些云(如dotCloudNodejistu)使用完全用 Node.js 编写的网关。主要是为了能够处理 websockets。但也因为 Node.js 太快了。

Here is the gateway of dotCloud which has been open sourced recently https://github.com/dotcloud/hipache

下面是最近开源的dotCloud的网关https://github.com/dotcloud/hipache

回答by Boris Ivanov

Both Apache and NGINX are fully developed web servers offering lots of modules and services out of the box. They're considered robust and has proven stability for several years now.

Apache 和 NGINX 都是完全开发的 Web 服务器,提供许多开箱即用的模块和服务。它们被认为是稳健的,并且已经证明了几年的稳定性。

Having that sort of available solutions, there's no need to re-invent the wheel. It can be more beneficial to implement the load balancer and routing with NGINX and not expose NodeJS to the outside and just run it on localhost.

有了这种可用的解决方案,就没有必要重新发明轮子了。使用 NGINX 实现负载均衡器和路由,而不是将 NodeJS 暴露给外部,而只是在本地主机上运行它会更有益。

NodeJS can't be considered a server software but merely a JavaScript engine plus libraries/modules. The fact that it's heavily used for server scripting does not make it web server.

NodeJS 不能被视为服务器软件,而只是一个 JavaScript 引擎加上库/模块。它被大量用于服务器脚本的事实并不能使它成为 Web 服务器。

If you decide to overlook the above and switch to NodeJS completely, i offer you think about maintaining such a solution. Logging, startup/shutdown scripts and monitoring can make the task more complicated than it seems.

如果您决定忽略上述内容并完全切换到 NodeJS,我建议您考虑维护这样的解决方案。日志记录、启动/关闭脚本和监控可能会使任务比看起来更复杂。

Further more, numerous libraries written for NodeJS tend to break with new versions delivered, as breaking changes are introduced by NodeJS. Consider that as the price for the lack of maturity. If you're up to the risk and not afraid of problems, go for NodeJS.

此外,许多为 NodeJS 编写的库往往会随着新版本的发布而中断,因为 NodeJS 引入了重大更改。将其视为缺乏成熟度的代价。如果您愿意承担风险并且不害怕出现问题,请选择 NodeJS。

Final note: static files can be served with NodeJS. Your scripts may read it and push it out.

最后一点:静态文件可以通过 NodeJS 提供。您的脚本可能会阅读它并将其推出。

Update: If you decided to go for Node.js consider to use Express.js framework.

更新:如果您决定使用 Node.js,请考虑使用 Express.js 框架。

回答by Van Coding

I prefer using only node.js for the whole thing. The reason for that is, that many node applications have built-in file servers or depend on one that serves their files. So, every application can use the fileserver that fits best and can customize it to its needs.

我更喜欢只使用 node.js 来完成整个事情。原因是,许多节点应用程序都有内置的文件服务器或依赖于为其文件提供服务的服务器。因此,每个应用程序都可以使用最适合的文件服务器,并且可以根据需要对其进行自定义。

Additionally, you lose a bit of performance when you have to proxy every single request from Apache/NGINX to node.js. It's much simpler to let node handle the requests by itself.

此外,当您必须将来自 Apache/NGINX 的每个请求代理到 node.js 时,您会损失一些性能。让节点自己处理请求要简单得多。