261

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

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

6 Answers 6

237

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

  • 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 serving 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.

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.

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.

15
  • 12
    express.static will handle ETags and cache-control headers just fine.
    – robertklep
    Commented May 27, 2013 at 10:13
  • 22
    Node isn't that good huh? centminmod.com/siegebenchmarks/2013/020313/index.html, zgadzaj.com/…
    – el vis
    Commented May 27, 2013 at 10:48
  • 5
    pauljz, do you have benchmarks to back up slower? the articles @pawlakpp pointed out seem to say Node.js is much faster under load. Commented May 27, 2013 at 15:48
  • 5
    There's some related discussion here: stackoverflow.com/questions/9967887/… with some additional perspectives. The benchmarks there (since you asked for additional benchmarks) show node.js/express, even clustered, underperforming noticeably. My feeling is it's best to keep static file serving and request handling out of the node event loop entirely, save those cycles for the work that needs to happen in Node. But honestly, if you serve static stuff out of Node, you'll be fine too. It's not a big deal.
    – pauljz
    Commented May 28, 2013 at 11:20
  • 4
    Benchmarks are useful and provide meaningful information, however the difference is not large enough in most applications to warrant concern over these numbers. Having Nginx/Apache in front of Node might be better, but in most situations it is simply overkill and adds unnecessary complexity. It all depends on your needs. My opinion is to make it work using NodeJS only and if the throughput is not to your liking, then consider adding a webserver in front.
    – Nepoxx
    Commented Sep 26, 2014 at 13:56
87

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.

0
36

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).

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.

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.

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

4
  • 2
    Good perspective on technical comparisons in general: "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." Too many comparisons these days are biased and based upon the baggage of experience and comfort-level on inferior but time-tested technologies.
    – Sunny
    Commented Jul 9, 2015 at 4:04
  • Yes but they're really /subjective/ reasons. A great example of an objective reason would be a benchmark - most of which I found indicate nginx > nodejs (though I really should do my own ....)
    – Nick
    Commented Nov 20, 2015 at 1:22
  • @Nick You're absolutely right. And there are a few out there though I'm not an expert in scientific bench-marking so I'll let people search the web for that. What I will say though is I think there is a benefit to the simplicity of using one server instead of two. There's just less potential for something to go wrong. On the other hand, Nginx usually has a package on every Unix-like system with good configuration whereas with Node you need to figure out integration with systemd, pm2, etc. So there are pluses and minuses and the user should pick their poison, so to speak.
    – user636044
    Commented Nov 20, 2015 at 2:38
  • I thought it was the opposite -- node would do better under load (perhaps not in pure unloaded speed) because it doesn't have to handoff a process serving up a file per request, but rather can push data when either the local disk or the remote client are ready on the same thread that all the other thousands of clients are on. This of course breaks down when you have multi processors.. Unless node knows how to use them now. Or webservers may use cooperative multitasking to server up static pages now.. Commented Oct 24, 2017 at 21:49
16

Using Node.js only

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.

Using Node.js with another webserver

Each web server has its own advantages. For example, Apache allows additional configuration per-directory via the .htaccess file. Nginx is known for its performance when it comes to serving static files or acting as a reverse proxy. Node.js provides a huge benefit when dealing with I/O heavy systems... Sometimes, we need to combine the forces of different web servers to satisfy the system's requirements.

Example: For an enterprise-level application that might scale up in the future, set up Nginx as a reverse proxy before Node.js application has some advantages :

  • Nginx can act as a load balancer to dispatch traffic to your NodeJS instances if you have more than 1.
  • Nginx can handle HTTPS, caching, and compression for you. Encryption and compression are heavily computed operations that NodeJS is not good at. So using Nginx will give you better performance.
  • Nginx will serve static content, which reduces the load of Node.js.
  • Separation of concerns: Nginx takes care of all the "configuration" part, and Node.js focus on the application logic.
8

Placing NGINX in front of Node helps better handle high connection volumes. NGINX offers (to name a few) caching, load balancing, rate limiting (using the leaky bucket algorithm) and can help mitigate attacks if paired with a banning service like Fail2ban.

As for production applications, you can run your application server behind NGINX as reverse proxy, coupled with a caching server like Redis- all of which can be situated behind a content delivery network as another line of defense from exposing your ipv4/ipv6.

5

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)

Not the answer you're looking for? Browse other questions tagged or ask your own question.