how is nginx different from apache
Hi all in this post we will be discussing two webserver packages, one is apache which already has shown its ability to do multiple things, in a single package with the help of modules, and millions of websites in the internet is powered by apache platform. The other is the relatively new webserver package called Nginx made by Russian programmer Igor Sysoev.
Many people in the industry might be aware of the thing called “speed” for which nginx is famous for. There are some other important difference between apache and nginx's working model, we will be discussing, that differences in detail.
Lets discuss two working models used by the apache web server. We will get to nginx later. Most people who are associated with apache might be knowing about these two models, through which apache servers its requests. These models are mentioned below.
1.Apache MPM Prefork
2.Apache MPM Worker.
Note: there are many different MPM modules available, for different platforms and functionalities but we will be discussing only the above two here.
Lets have a look at the main difference between MPM Prefork and MPM Worker. MPM stands for Multi Processing Module.
Most of the functionality in apache comes from modules, even this MPM Prefork comes as a module, and can be enabled or disabled. This prefork model of apache is non-threaded and is a good model, as it makes each and every connection isolated from each other.
So if one connection is is having some issues, the other one is not all effected. By default if no MPM module is specified then apache uses this MPM Prefork as its MPM module. But this model is very resource intensive.
Why is prefork model reource intensive?
Because in this model a single parent process sits and creates many child processes which wait for requests and serve as the requests arrive. Which means each and every request is served by a seperate process. In other words, we can say its “process per request”. And apache maintains, several number of idle process before the requests arrive. Due to these idle processes waiting for requests, they can serve fast when requests arrive.
If you are interested in learning and understanding more about processes in linux. Then go through the below post.
But each and every process will utilize system resources like RAM, and CPU. And equal amount of RAM is utilized for each and every process.
If you have got a lot number of requests at one time, then you will have lot number of child processes spawned by apache, and which will result in heavy resource utilization, as each process will utilize a certain amount of system memory and cpu resources.
This model of apache can serve a large number of requests with less system resources than the prefork model because here a limited number of process will serve, many number of requests.
This is multi threaded architecture of apache. This model uses thread rather than process to serve requests. Now what is thread??
In Operating System's thread is a small instance of a process which does some job and exits. Thread is sometimes called a process inside a process.
In this model also there is one single parent process, which spawns some child processes. But there is no “process per requests”, but instead “thread per requests”. So the child process will have a certain number of threads inside it. Each child process will have certain “server threads” and certain “idle threads”. Idle threads are waiting for new requests, so there is no time wasted in creating threads when the requests arrive.
There is a directive inside apache config file /etc/httpd/conf/httpd.conf called “StartServers” which says how many child process will be there when apache starts.
Child process handles requests with the help of a fixed number of threads inside them which is specified by the argument “ThreadsPerChild” in the config file.
Note: there are some php module issues reported while working with apache MPM worker model.
Now lets discuss nginx.
Nginx was made, to solve the c10k problem in apache.
C10k: its a name given to the issue of optimizing the web server software to handle large number of requsts at one time. In the range of 10000 requests at a time, hence the name
Nginx is known for its speed in serving static pages, much faster than apache and keeping the machine resources very low.
Fundamentally both apache and nginx differs a lot.
Apache works in a multi process/multi threaded architecture, While nginx is an event driven single threaded architecture.(i will come back to event driven later). The main difference this even driven architecture makes is that, a very small number of nginx worker process can serve a very very large number of requests.
Sometimes nginx is also deployed as a front end server, serving static content requests faster to the clients, and apache in behind.
Each worker process handles requests with the help of the event driven model. Nginx does this with the help of a special functionality in linux kernel called as epoll and select poll. Apache when even run by its threaded model utilizes considerably much more system resource than nginx.
Why does nginx run more efficiently than apache?
In apache when a request is being served, either a thread or a process is created which serves the request. Now if one requst needs some data from the database,and files from disk, etc the process waits for that.
So some processes in apache just sits and wait for certain task to complete(eating system resources).
Suppose a client with a slow internet connection connects to a web server running apache, the apache server retrieves the data from the disk, to serve the client. Now even after serving the client that process will wait until a confirmation is received from that cliet(which will waste that much process resource)
Nginx avoids the idea of child processes. All requests are handled by a single thread. And this single thread will handle everything, with the help of something called as event loop. So the thread pops up whenever a new connection, or some thing is required(not wasting resources.).
Step 1: Gets Request
Step 2: Request Triggers events inside the process
Step 3: Process manages all these events and returns the output(and simultaniously handles other events for other requests)
Nginx also supports major functionalists which apache supports like the following.
- Virtual Hosts
- Reverse Proxy
- Load Balence
- URL rewrite
are some of them...