에몽이

What is the difference between an Apache/nginx server and Node.js one? 본문

Backend/web server

What is the difference between an Apache/nginx server and Node.js one?

ian_hodge 2017. 4. 14. 14:50
출처:https://www.quora.com/What-is-the-difference-between-an-Apache-nginx-server-and-Node-js-one

Apache and Nginx are both HTTP servers. They can serve static files like (.jpg and .html files) or dynamic pages (like a Wordpress blog or forum written in a language like PHP or Python). Apache/nginx need to be configured to recognize the URLs that users will be requesting and route them to the right place.

So, for example, with a typical PHP site (like a Wordpress blog) you tell Apache that any file that ends with .php should be interpreted as PHP code, so when the user visits "http://myblog.com/tag.php?q=mytag", for example, Apache will launch the PHP interpreter to read the file and process it into an HTML page. As part of this process, PHP may talk to a MySQL database and use that to generate the page. Lastly, PHP gives the final HTML code to Apache to send to the user's browser.

So, far so good?

Now, Node is a bit different. It's a programming environment like PHP that lets you talk to database, make dynamic pages, etc. However, it differs in that it includes an HTTP server. That means that it can actually act completely on its own without nginx or Apache. You can just run Node and it will be the HTTP server and also the "app server" (which actually creates your dynamic pages and talks to the DB).

It's a two for one deal.

Last thing to know is that when people actually deploy Node on the internet, they sometimes put another HTTP server like nginx in front of Node. That means that when a user loads a page, it first hits Nginx which can make a decision about where to route the request. This is handy if you have a big site and need to run many Node instances to handle all your traffic. This way, the single nginx server can split the work to be done among many Node.js "app servers".

I necessarily simplified things here and there, but this is the general idea. Hopefully, this all made sense and I answered your question in there somewhere.
Sumit Gupta

First of all let's discuss Apache vs Nginx. The Apache HTTP server and NGINX are the two most popular open source web servers but they are different in there architecture.

Apache

Apache's architecture simplicity : 
Apache debuted in 1995 and in those days the web pages were very simple and the traffic was very low.

Apache's architecture was pretty simple for each new request a new process of correct type was spawned to handle the HTTP connection. The process would receive the request, process the request, make a response and return the response. The architecture was pretty simple and apache adopted it, there was very big downside to it we will discuss it in next paragraph. To make the process of spawning faster apache adopted a prefork model a number of processes were pre-spawned and they would process the HTTP connection as soon as the requests were made.

So each request was handled by a separate process but by 2005 the web traffic was increasing at a very high rate and the web pages were becoming more and more complex and to decrease the web page load time what was earlier a single request to the server was broken down into multiple requests and each request was made in parallel so that the resources could be downloaded in parallel and moreover browser would keep these connections open using keep-alive header. So apache's basic architecture of one process for one request soon started to consume the entire memory and crash.

Nginx :

NGINX was written specifically to address the performance limitations of Apache web servers. It was created in 2002 by Igor Sysoev, a system administrator for a popular Russian portal site (Rambler.ru), as a scaling solution to help the site manage greater and greater volumes of traffic. It was open sourced in October 2004.

The success of Nginx is attributed to its architecture. Nginx follows an asynchronous event driven model for handling requests. Each worker can handle number of requests.

Question is what is event based model and what is relation of event based model with a worker handling multiple number of request ?

So suppose hypothetically we have only one worker on the nginx server. So nginx server maintains a queue for all the requests. It picks up the request if no I/O is required for that request it does the processing and returns the request with a response.

If the request has an I/O the a process is spawned for it at this point asynchronous behaviour and event driven behaviour comes into picture. The web worker does not waits for I/O to complete it moves to next request. What happens to the I/O the I/O on completion fires an event of completion and its callback is placed in queue for execution.

So in this way one web worker is able to handle multiple number of requests and generally on Nginx server there are number of workers or processes.

Node.js :

What JVM is to java, node.js is to javascript. It allows us to use javascript as a language for scripting on servers.

Ryan Dahl creator of Node.js was impressed by Nginx and looked at javascript which was made with asynchronous and event based model. He used google V8 engine which is used in chrome to process Javascript and created Node.js.

Since it is my first detailed answer - Ignore my grammatical mistakes if any.

Jiaxin Shan
Let me add something.  Nginx is a kind of event-driven (asynchronous) architecture. NodeJS 's most impressed feature is event-based instead thread-based. They're like soul-mate which can easily get high concurrency and solve the C10K problem of old web Server like Apache.

We can use NodeJS as server, but we need to write this server. The problem is there will not some problems deloy some project  but big ones.  Like Server cluster, Optimization , we need high performace server like nginx.
Arpit Agrawal
I think the ground has pretty much been covered on the difference between the two. 

While Apache/nginx is a web server capable of serving http requests, it is more of static service engine, serving html, js and css files. It can act as a web proxy as well, which is a very common scenario of usage for nginx. For now we restrict the capabilities of nginx to this, assuming it cannot create dynamic pages, connect to a database etc.

Now Nodejs is the other part, which is a Javascript based programming environment. It has a http module where you can run a web server and serve web requests by programmatically processing them, connecting to databases and creating dynamic web pages. 

So, put together both of them, it's a good stack to create a dynamic web application.

Now, there can be two questions one can ask:

1. What makes the two so similar, so that they are being questioned for distinction.

2. Can both of them perform the task of each other.

The answer to 1 is it's the event driven architecture that makes the two similar. Both nginx and node js follow event based asynchronous architecture which does not block the request processing flows, which make them cater to far more load than traditional web server engines.

The answer to 2 is yes, but to a limited extent and with a certain level of complexity. 

For node js serving a static file can be as simple as reading the content programmatically and responding in the request handler.

For nginx to be able to do dynamic stuff, it would need some scripting/programming language to be tied up. There are scripts like Lua script which can be plugged in with nginx to perform dynamic processing, but again Lua is a complex and is not too evolved to perform tasks supposed to be done by modern web application servers in a very optimised manner.
William Hurley

So NodeJS ships with primitives to listen for HTTP connections. This is called, funnily enough, `http`. So with nothing but out-of-the-box NodeJS you can write a web server. So you could write an analogue to Apache or nginx that way. However you would have very little of the functionality that they provide. You can add additional functionality by either writing it yourself or using a framework on top of it -- something like Express. This has handlers for things like easily serving static files, etc. However the web server is still tightly coupled to your application, with maybe some exception for -- again -- serving static files. You could also implement FastCGI in order to call external services like PHP-FPM which would allow you to move the processing to other languages. Basically this is what nginx does in most cases. Of course to achieve feature parity with nginx would be challenging.

One important point is that Nodejs is async, so, is not "cool" you use a nodejs server and a mysql database(or anything sync) because you will have a non-blocking server with a blocking database transaction.
The main difference as I see it is that Apache is thread and process based - i.e each request is handled by a separate thread or process, which means while it is processing input/output, the entire thread is blocked. Node JS has asynchronous, event driven input/output. Every nodejs instance runs in a single thread and due to its asynchronous nature, it can handle far more number of concurrent requests as compared to Apache. Node can do far more than just http web applications though, which makes it useful in a number of unique situations (ex: nodebots - robots programmed in node).
 
While you can do a lot of things with node, it is not the best fit for things like static sites, a CMS, a blog, or ecommerce websites. These sites don't benefit a great deal from node's asynchronous nature and it can end up being overkill. One of the best use-cases for node is for a chat room, a multiplayer real-time game, or multiple users simultaneously editing the same document (a la google docs).
 
Apache generally uses PHP as a scripting language, which has been around forever, is stable, and has a lot of support. If you're going to use a CMS like Wordpress, Drupal, or Joomla or really anything PHP-related, you're much better off going with Apache. Nginx is similar to apache, but it is supposed to be even faster at serving static files and its gaining in popularity.
 
These three are not mutually exclusive, however. Many websites have Apache an/or Nginx installed alongside node and use different servers to best serve the type of content requested. In fact, It is rare to find a website that relies solely on node to serve content in the wild.
C. Aaron Cois
Nginx is a web server, Node.js is a software development framework. They are similar in that they both use an asynchronous I/O model to allow them to scale to large numbers of requests/client communications without a lot of memory overhead.
Christopher Hall

Nginx is a web server and Nodejs is more like an application server.  Though if nginx is built with its Lua module you can get asynchronous code execution and get good performance. Nodejs effectively has a single thread and maybe is not so suitable to handle large numbers of connections so you would likely use nginx as the front end to multiple Nodejs back ends to achieve both redundancy and high performance.

I would put Apache in the middle ground as both a web server and an application server because of its many modules to run various languages.  Al though it can also be put behind nginx as it seems a waste to use an Apache process to serve a static file when it could be more productive running a module.

Nginx is an asynchronous web server, etc., while Node.js is a runtime environment with asynchronous IO libraries one of which is the HTTP Server.
Joseph Misiti
Nginx is a webserver/reverse-proxy/load balancer, Node.js is an asynchronous web framework. I believe both have comet functionality.


Comments