仅使用 Node.js 与使用 Node.js 和 Apache/Nginx [英] Using Node.js only vs. using Node.js with Apache/Nginx

查看:33
本文介绍了仅使用 Node.js 与使用 Node.js 和 Apache/Nginx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在什么情况下,在实际部署中应该只将 Node.js 用作服务器?

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

当人们不想只想使用 Node.js 时,Node.js 有什么更好的地方?Apache 还是 Nginx?

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

推荐答案

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

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

  • 不必担心 Node.js 进程的权限/setuid.通常只有 root 可以绑定到端口 80.如果您让 nginx/Apache 担心以 root 身份启动,绑定到端口 80,然后放弃其 root 权限,则意味着您的 Node 应用程序不必担心.
  • 提供静态文件,如图像、css、js 和 html.与使用适当的静态文件 Web 服务器相比,Node 的效率可能较低(Node 在某些情况下也可能更快,但这不太可能成为常态).除了更有效地提供文件服务之外,您不必担心处理 eTag 或缓存控制标头的问题,如果您从 Node.某些框架可能会为您处理此问题,但您需要确定.无论如何,仍然可能更慢.
  • 正如 Matt Sergeant 在他的回答中提到的,如果您的节点服务崩溃,您可以更轻松地显示有意义的错误页面或退回到静态站点.否则,用户可能会遇到超时连接.
  • 在 Node 前运行另一个 Web 服务器可能有助于缓解针对 Node.js 的安全漏洞和 DoS 攻击.对于实际示例,CVE-2013-4450阻止在 Node 前运行类似 Nginx 的东西.

我会警告第二个要点,说您可能应该通过 CDN 或从像 Varnish 这样的缓存服务器后面提供静态文件.如果您正在执行此操作,那么源是 Node、Nginx 还是 Apache 并不重要.

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.

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

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.

这篇关于仅使用 Node.js 与使用 Node.js 和 Apache/Nginx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆