node.js 本身还是用于提供静态文件的 nginx 前端?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9967887/
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
node.js itself or nginx frontend for serving static files?
提问by artvolk
Is there any benchmark or comparison which is faster: place nginx in front of node and let it serve static files directly or use just node and serve static files using it?
有没有更快的基准或比较:将 nginx 放在 node 前面,让它直接提供静态文件,或者只使用 node 并使用它提供静态文件?
nginx solution seems to be more manageable for me, any thoughts?
nginx 解决方案对我来说似乎更易于管理,有什么想法吗?
回答by m33lky
I'll have to disagree with the answers here. While Node will do fine, nginx will most definitely be faster when configured correctly. nginx is implemented efficiently in C following a similar pattern (returning to a connection only when needed) with a tiny memory footprint. Moreover, it supports the sendfilesyscall to serve those files which is as fast as you can possibly get at serving files, since it's the OS kernel itself that's doing the job.
我将不得不不同意这里的答案。虽然 Node 会做得很好,但如果配置正确,nginx 肯定会更快。nginx 在 C 中按照类似的模式(仅在需要时返回连接)有效地实现,占用的内存很小。此外,它支持sendfile系统调用来为那些文件提供服务,这与提供文件服务的速度一样快,因为它是操作系统内核本身在做这项工作。
By now nginx has become the de facto standard as the frontend server. You can use it for its performance in serving static files, gzip, SSL, and even load-balancing later on.
到目前为止,nginx 已经成为事实上的前端服务器标准。您可以使用它在提供静态文件、gzip、SSL 甚至负载平衡方面的性能。
P.S.: This assumes that files are really "static" as in at rest on disk at the time of the request.
PS:这假设文件确实是“静态的”,就像在请求时磁盘上的静止文件一样。
回答by gremo
I did a quick ab -n 10000 -c 100for serving a static 1406 byte favicon.ico, comparing nginx, Express.js (static middleware) and clustered Express.js. Hope this helps:
我快速ab -n 10000 -c 100提供了一个静态 1406 字节favicon.ico,比较了 nginx、Express.js(静态中间件)和集群 Express.js。希望这可以帮助:


Unfortunately I can't test 1000 or even 10000 concurrent requests as nginx, on my machine, will start throwing errors.
不幸的是,我无法测试 1000 甚至 10000 个并发请求,因为 nginx 在我的机器上会开始抛出错误。
EDIT: as suggested by artvolk, here are the results of cluster + staticmiddleware (slower):
编辑:按照 artvolk 的建议,这里是集群 +static中间件的结果(较慢):


回答by ssotangkur
I have a different interpretation of @gremo's charts. It looks to me like both node and nginx scale at the same number of requests (between 9-10k). Sure the latency in the response for nginx is lower by a constant 20ms, but I don't think users will necessarily perceive that difference (if your app is built well). Given a fixed number of machines, it would take quite a significant amount of load before I would convert a node machine to nginx considering that node is where most of the load will occur in the first place. The one counterpoint to this is if you are already dedicating a machine to nginx for load balancing. If that is the case then you might as well have it serve your static content as well.
我对@gremo 的图表有不同的解释。在我看来,node 和 nginx 都以相同的请求数(在 9-10k 之间)扩展。确保 nginx 响应的延迟恒定降低 20 毫秒,但我认为用户不一定会察觉到这种差异(如果您的应用程序构建良好)。给定固定数量的机器,在我将节点机器转换为 nginx 之前,将需要相当多的负载,因为该节点是大部分负载首先发生的地方。与此相反的一个观点是,如果您已经将一台机器专用于 nginx 以进行负载平衡。如果是这种情况,那么您不妨让它也为您的静态内容提供服务。
回答by Will Stern
Either way, I'd setup Nginx to cache the static files...you'll see a HUGE difference there. Then, whether you serve them from node or not, you're basically getting the same performance and the same load-relief on your node app.
无论哪种方式,我都会设置 Nginx 来缓存静态文件......你会在那里看到巨大的差异。然后,无论您是否从 node 为它们提供服务,您的 node 应用程序基本上都获得了相同的性能和相同的负载减轻。
I personally don't like the idea of my Nginx frontend serving static assets in most cases, in that
我个人不喜欢在大多数情况下我的 Nginx 前端服务静态资产的想法,因为
1) The project has to now be on the same machine - or has to be split into assets (on nginx machine) & web app (on multiple machines for scaling)
1)项目现在必须在同一台机器上 - 或者必须分成资产(在 nginx 机器上)和 web 应用程序(在多台机器上进行扩展)
2) Nginx config now has to maintain path locations for static assets / reload when they change.
2) Nginx 配置现在必须在静态资产/重新加载更改时维护它们的路径位置。
回答by Brad Harris
That's a tricky question to answer. If you wrote a really lightweight node server to just serve static files, it would most likely perform better than nginx, but it's not that simple. (Here's a "benchmark"comparing a nodejs file server and lighttpd - which is similar in performance to ngingx when serving static files).
这是一个很难回答的问题。如果您编写了一个真正轻量级的节点服务器来仅提供静态文件,那么它的性能很可能会比 nginx 好,但并不是那么简单。(这是一个比较 nodejs 文件服务器和 lighttpd的“基准”——它在提供静态文件时的性能与 ngingx 相似)。
Performance in regard to serving static files often comes down to more than just the web-server doing the work. If you want the highest performance possible, you'll be using a CDN to serve your files to reduce latency for end-users, and benefit from edge-caching.
提供静态文件的性能通常不仅仅取决于网络服务器的工作。如果您希望获得尽可能高的性能,您将使用 CDN 来为您的文件提供服务,以减少最终用户的延迟,并从边缘缓存中受益。
If you're not worried about that, node can serve static files just fine in most situation. Node lends itself to asynchronous code, which it also relies on since it's single-threaded and any blocking i/o can block the whole process, and degrade your applications performance. More than likely you're writing your code in a non-blocking fashion, but if you are doing anything synchronously, you may cause blocking, which would degrade how fast other clients can get their static files served. The easy solution is to not write blocking code, but sometimes that's not a possibility, or you can't always enforce it.
如果你不担心这个,node 在大多数情况下可以很好地提供静态文件。Node 适合异步代码,它也依赖异步代码,因为它是单线程的,任何阻塞的 i/o 都可能阻塞整个进程,并降低应用程序的性能。您很有可能以非阻塞方式编写代码,但如果您正在同步执行任何操作,则可能会导致阻塞,这会降低其他客户端获取其静态文件的速度。简单的解决方案是不编写阻塞代码,但有时这是不可能的,或者你不能总是强制执行它。
回答by user2379441
I am certain that purely node.js can outperform nginx in a lot of aspect.
我确信纯 node.js 可以在很多方面胜过 nginx。
All said I have to stay NginX has an in-built cache, whereas node.js doesn't come with it factory installed (YOU HAVE TO BUILD YOUR OWN FILE CACHE). The custom file cache does outperform nginx and any other server in the market as it is super simple.
所有人都说我必须保留 NginX 有一个内置缓存,而 node.js 没有在工厂安装(你必须构建你自己的文件缓存)。自定义文件缓存确实优于 nginx 和市场上的任何其他服务器,因为它非常简单。
Also Nginx runs on multiple cores. To use the full potential of Node you have to cluster node servers. If you are interested to know how then please pm.
Nginx 也运行在多个内核上。要充分利用 Node 的潜力,您必须对节点服务器进行集群。如果您有兴趣知道如何请pm。
You need to deep digger to achieve performance nirvana with node, that is the only problem. Once done hell yeah... it beats Nginx.
您需要深入挖掘以实现节点的性能必杀技,这是唯一的问题。一旦完成地狱是的...它击败了 Nginx。

