在实际部署中,哪些情况下应该只使用Node.js作为服务器?
当一个人不想只使用Node.js时,用Node.js做什么更好呢?Apache还是Nginx?
在实际部署中,哪些情况下应该只使用Node.js作为服务器?
当一个人不想只使用Node.js时,用Node.js做什么更好呢?Apache还是Nginx?
有几个很好的理由在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-4450 is prevented by running something like Nginx in front of Node.
我要提醒你的第二个要点是,你应该通过CDN或者像Varnish这样的缓存服务器来提供静态文件。如果你这么做了,不管它的源是Node还是Nginx还是Apache。
nginx需要特别注意的是:如果你正在使用websockets,请确保使用最新版本的nginx(>= 1.3.13),因为它刚刚添加了升级连接以使用websockets的支持。
为了给pauljz的答案再添加一个原因,我使用了一个前端服务器,这样当我重新启动后端服务器或它由于某种原因崩溃时,它可以提供502个错误页面。这允许您的用户永远不会得到关于无法建立连接的错误。
我相信,只要知道自己在做什么,在任何情况下都可以使用Node来提供静态文件。使用应用服务器来提供静态文件当然是一种新的范式,因为如此多的(每一种)竞争技术(PHP、Ruby、Python等)都需要一个像HTTPD或Nginx这样的web服务器放在应用服务器前面。
我所读到的反对使用Node提供静态文件的每个客观原因都围绕着使用您最了解的或使用被认为经过更好测试/更稳定的方法的想法。从实践上讲,这些都是非常合理的理由,但几乎没有纯粹的技术相关性。
除非你发现了一个经典的web服务器无法实现的功能(我怀疑你会),选择你最了解的或你更喜欢使用的方法,因为任何一种方法都可以。
至于Nginx vs Apache,他们也会和Node一样“玩”。您应该在不考虑Node的情况下比较它们。
另外:如果你需要反向代理,比如在同一个端口上执行Websocket服务器,或者混合一些技术(用NodeJS回复一些请求,用PHP回复一些请求或其他什么),这也很重要。
只使用Node.js
Node.js can do all the tasks of a web server: serve static files, respond to an API call, run server on HTTPS... There are also a lot of packages that provide extra functionalities like logging the request, compress the response, set cookies, prevent XSS attacks... Lack of functionalities isn't likely a reason for using another Webserver (Apache/Nginx/etc..) to complete Node.js. In other words, for a simple application that does not need to scale, you don't need to add an extra layer to Node.js, it just complicates the problem.
使用Node.js与另一个web服务器
每个web服务器都有自己的优势。例如,Apache允许通过.htaccess文件对每个目录进行额外配置。在提供静态文件或充当反向代理时,Nginx以其性能而闻名。Node.js在处理I/O繁重的系统时提供了巨大的好处…有时,我们需要结合不同web服务器的力量来满足系统的需求。
例如:对于未来可能会扩展的企业级应用程序,在Node.js应用程序之前将Nginx设置为反向代理有一些好处:
Nginx可以作为一个负载均衡器,如果你有多于1个的NodeJS实例,就可以把流量分派给它。 Nginx可以为你处理HTTPS,缓存和压缩。加密和压缩是需要大量计算的操作,而NodeJS并不擅长。所以使用Nginx会给你更好的性能。 Nginx将提供静态内容,这减少了Node.js的负载。 关注点分离:Nginx负责所有“配置”部分,Node.js专注于应用程序逻辑。
将NGINX放置在Node前面有助于更好地处理高连接量。NGINX提供(举几个例子)缓存,负载平衡,速率限制(使用泄漏桶算法),如果与像Fail2ban这样的禁止服务配对,可以帮助减轻攻击。
至于生产应用程序,你可以在NGINX后面运行你的应用服务器作为反向代理,再加上像Redis这样的缓存服务器——所有这些都可以位于内容交付网络后面,作为另一道防线,防止暴露你的ipv4/ipv6。