node.js itself or nginx frontend for serving static files?
Written by: J Dawg
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 solution seems to be more manageable for me, any thoughts?
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 sendfile syscall 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.
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.
P.S.: This assumes that files are really “static” as in at rest on disk at the time of the request.
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).
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.
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.
I did a quick
ab -n 10000 -c 100 for serving a static 1406 byte
favicon.ico, comparing nginx, Express.js (static middleware) and clustered Express.js. Hope this helps:
Unfortunately I can’t test 1000 or even 10000 concurrent requests as nginx, on my machine, will start throwing errors.
EDIT: as suggested by artvolk, here are the results of cluster +
static middleware (slower):
Node.js can stand alone. You don’t need to use nginx with Node.js any more (see this entry for details). Therefore there is no point in using nginx only for this simple task. As for performance I don’t really know, but I don’t think that there will be any noticeable difference.
By the way: is there any noticeable difference (in performance) between any two environments in serving static files? Reading files is an OS job and sending them to client depends (mostly) on connection, doesn’t it?