仅使用 Node.js 与使用 Node.js 和 Apache/Nginx

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16770673/
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 14:37:06  来源:igfitidea点击:

Using Node.js only vs. using Node.js with Apache/Nginx

node.js

提问by 7cows

In what cases should one prefer to use Node.js only as a server in real deployment?

在什么情况下应该更喜欢将 Node.js 仅用作实际部署中的服务器?

When one does notwant to use Node.js only, what plays better with Node.js? Apache or Nginx?

当一个人希望只使用Node.js的,有什么用Node.js的发挥更好?Apache 还是 Nginx?

采纳答案by pauljz

There are several good reasons to stick another webserver in front of Node.js:

在 Node.js 前面添加另一个 Web 服务器有几个很好的理由:

  • Not having to worry about privileges/setuid for the Node.js process. Only root can bind to port 80 typically. If you let nginx/Apache worry about starting as root, binding to port 80, and then relinquishing its root privileges, it means your Node app doesn't have to worry about it.
  • Serving static files like images, css, js, and html. Node may be less efficient compared to using a proper static file web server (Node may also be faster in select scenarios, but this is unlikely to be the norm). On top of files serving more efficiently, you won't have to worry about handling eTags or cache control headers the way you would if you were servings things out of Node. Some frameworks may handle this for you, but you would want to be sure. Regardless, still probably slower.
  • As Matt Sergeant mentioned in his answer, you can more easily display meaningful error pages or fall back onto a static site if your node service crashes. Otherwise users may just get a timed out connection.
  • Running another web server in front of Node may help to mitigate security flaws and DoS attacks against Node. For a real-world example, CVE-2013-4450is prevented by running something like Nginx in front of Node.
  • 不必担心 Node.js 进程的权限/setuid。通常只有 root 可以绑定到端口 80。如果您让 nginx/Apache 担心以 root 身份启动,绑定到端口 80,然后放弃其 root 权限,则意味着您的 Node 应用程序不必担心它。
  • 提供静态文件,如图像、css、js 和 html。与使用适当的静态文件 Web 服务器相比,Node 的效率可能较低(Node 在某些情况下也可能更快,但这不太可能成为常态)。除了更有效地提供文件服务之外,您不必担心处理 eTags 或缓存控制标头的问题,如果您在 Node.js 之外提供服务,您会这样做。一些框架可能会为您处理这个问题,但您需要确定。无论如何,仍然可能更慢。
  • 正如 Matt Sergeant 在他的回答中提到的,如果您的节点服务崩溃,您可以更轻松地显示有意义的错误页面或退回到静态站点。否则,用户可能会遇到超时连接。
  • 在 Node 前面运行另一个 Web 服务器可能有助于减轻针对 Node.js 的安全漏洞和 DoS 攻击。举个真实的例子,CVE-2013-4450通过在 Node 前面运行类似 Nginx 的东西防止

I'll caveat the second bullet point by saying you should probably be serving your static files via a CDN, or from behind a caching server like Varnish. If you're doing this it doesn't really matter if the origin is Node or Nginx or Apache.

我会警告第二个要点,说您可能应该通过 CDN 或从像 Varnish 这样的缓存服务器后面提供静态文件。如果你这样做,源是 Node、Nginx 还是 Apache 并不重要。

Caveat with nginx specifically: if you're using websockets, make sure to use a recent version of nginx (>= 1.3.13), since it only just added support for upgrading a connection to use websockets.

特别注意 nginx:如果您使用 websockets,请确保使用最新版本的 nginx (>=1.3.13),因为它只是添加了对升级连接以使用 websockets 的支持。

回答by Matt Sergeant

Just to add one more reason to pauljz's answer, I use a front end server so that it can serve up 502 error pages when I'm restarting the backend server or it crashes for some reason. This allows your users to never get an error about unable to establish a connection.

为 pauljz 的回答再添加一个原因,我使用前端服务器,以便在我重新启动后端服务器或由于某种原因崩溃时它可以提供 502 个错误页面。这允许您的用户永远不会收到有关无法建立连接的错误。

回答by Matt Sergeant

It is my belief that using Node to serve static files is fine in all circumstances as long as you know what you're doing. It is certainly a new paradigm to use the application server to serve static files as so many (every?) competing technologies (PHP, Ruby, Python, etc) require a web server like HTTPD or Nginx in front of the application server(s).

我相信,只要您知道自己在做什么,就可以在任何情况下使用 Node 来提供静态文件。使用应用程序服务器来提供静态文件当然是一种新的范例,因为许多(每一种?)竞争技术(PHP、Ruby、Python 等)都需要在应用程序服务器前面使用 HTTPD 或 Nginx 等 Web 服务器.

Every objective reason I have ever read against serving static files with Node revolves around the idea of using what you know best or using what is perceived as better-tested / more stable. These are very valid reasons practically speaking, but have little purely technical relevance.

我读过的反对使用 Node 提供静态文件的每一个客观原因都围绕着使用你最了解的东西或使用被认为经过更好测试/更稳定的东西的想法。实际上,这些都是非常有效的理由,但几乎没有纯粹的技术相关性。

Unless you find a feature that is possible with a classic web server that is not possible with Node (and I doubt you will), choose what you know best or what you'd prefer to work with as either approach is fine.

除非您发现 Node 无法实现的经典 Web 服务器具有的功能(我怀疑您会这样做),否则请选择您最了解的或您更喜欢使用的,因为这两种方法都可以。

As for Nginx vs Apache -- they will "play" with Node the same. You should compare them without regard to Node.

至于 Nginx 与 Apache - 他们将与 Node 一起“玩”。您应该在不考虑 Node.js 的情况下比较它们。

回答by Enrique

An extra: It is important also if you need a Reverse Proxy, for example to execute a Websocket Server on the same port, or maybe mix some techonlogies (reply with NodeJS some requests and with PHP some others or whatever)

额外的:如果你需要一个反向代理也很重要,例如在同一个端口上执行一个 Websocket 服务器,或者可能混合一些技术(用 NodeJS 回复一些请求,用 PHP 回复一些其他的或其他什么)