{"id":902,"date":"2014-12-24T07:33:25","date_gmt":"2014-12-24T07:33:25","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=902"},"modified":"2015-08-16T20:04:10","modified_gmt":"2015-08-16T20:04:10","slug":"should-i-use-node-js-only-or-behind-apache-or-nginx","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/stack-overflow\/should-i-use-node-js-only-or-behind-apache-or-nginx\/","title":{"rendered":"Should I use Node.js only or behind Apache or Nginx"},"content":{"rendered":"<p><a href=\"http:\/\/stackoverflow.com\/users\/534755\/nikola\"><img loading=\"lazy\" decoding=\"async\" title=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" src=\"http:\/\/stackoverflow.com\/users\/flair\/534755.png\" rel=\"lightbox[902]\" alt=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" width=\"208\" height=\"58\" \/><\/a><br \/>\nI&#8217;m a big fan of <a href=\"http:\/\/stackoverflow.com\/\">Stack Overflow<\/a> and I tend to contribute regularly (am currently in the <a href=\"http:\/\/stackexchange.com\/leagues\/1\/alltime\/stackoverflow\/2008-07-31\/534755?sort=reputationchange#534755\">top 0.X%<\/a>).\u00a0In this category (<a href=\"http:\/\/www.nikola-breznjak.com\/blog\/category\/stack-overflow\/\">stackoverflow<\/a>)\u00a0of posts I will will be posting my top rated questions and answers. This, btw, is allowed as explained in the meta thread <a href=\"http:\/\/meta.stackoverflow.com\/questions\/266971\/can-i-post-so-questions-and-answers-in-a-personal-blog\/266973\">here<\/a>.<\/p>\n<p>I wondered about basically the same\u00a0<a href=\"http:\/\/stackoverflow.com\/questions\/16770673\/using-node-js-only-vs-using-node-js-with-apache-nginx\">quesiton<\/a>\u00a0as user <a style=\"color: #4a6b82;\" href=\"http:\/\/stackoverflow.com\/users\/2308693\/7cows\">7cows<\/a>\u00a0had:\u00a0In what cases should one prefer to use Node.js only as a server in real deployment?\u00a0When one does\u00a0<strong>not<\/strong>\u00a0want to use Node.js only, what plays better with Node.js, Apache or Nginx?<\/p>\n<p>The answer, by <a href=\"http:\/\/stackoverflow.com\/users\/1590616\/pauljz\">pauljz<\/a>,\u00a0was:<\/p>\n<blockquote><p>There are several good reasons to stick another webserver in front of Node.js:<\/p>\n<ul>\n<li>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&#8217;t have to worry about it.<\/li>\n<li>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&#8217;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.<\/li>\n<li>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.<\/li>\n<li>Running another web server in front of Node may help to mitigate security flaws and DoS attacks against Node. For a real-world example,\u00a0<a style=\"color: #4a6b82;\" href=\"http:\/\/web.nvd.nist.gov\/view\/vuln\/detail?vulnId=CVE-2013-4450\">CVE-2013-4450<\/a>\u00a0is\u00a0<a style=\"color: #4a6b82;\" href=\"http:\/\/blog.nodejs.org\/2013\/10\/22\/cve-2013-4450-http-server-pipeline-flood-dos\/\">prevented by running something like Nginx in front of Node<\/a>.<\/li>\n<\/ul>\n<p>I&#8217;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&#8217;re doing this it doesn&#8217;t really matter if the origin is Node or Nginx or Apache.<\/p>\n<p>Caveat with nginx specifically: if you&#8217;re using websockets, make sure to use a recent version of nginx (&gt;= 1.3.13), since it only just added support for upgrading a connection to use websockets.<\/p><\/blockquote>\n<p>Also, user <a style=\"color: #4a6b82;\" href=\"http:\/\/stackoverflow.com\/users\/819962\/matt-sergeant\">Matt Sergeant<\/a>\u00a0added:<\/p>\n<blockquote><p><span style=\"color: #000000;\">Just to add one more reason to pauljz&#8217;s answer, I use a front end server so that it can serve up 502 error pages when I&#8217;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.<\/span><\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m a big fan of Stack Overflow and I tend to contribute regularly (am currently in the top 0.X%).\u00a0In this category (stackoverflow)\u00a0of posts I will will be posting&hellip;<\/p>\n","protected":false},"author":1,"featured_media":609,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-902","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-stack-overflow"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/902","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=902"}],"version-history":[{"count":4,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/902\/revisions"}],"predecessor-version":[{"id":1695,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/902\/revisions\/1695"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/609"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}