Written by Clifford
What is the Nginx web and proxy server and how does it compare to Apache?
Should you use one of these servers or both?
Here we explore some answers to these questions.

The Apache web servers have been in use since 1995.
Apache powers more websites than any other product; Microsoft IIS comes in second.
Another example is using mod_php to execute php scripts without having to use cgi.
It also creates new threads that must compete with others for access to memory and CPU.
Apache will also refuse new connections when traffic reaches the limit of processes configured by the administrator.
The product is open source and free, but Nginx offers support if you buy its Nginx Plus version.
Its clearly the fastest.
Nginx is indeed event-based.
They call their architecture event-driven and asynchronous.
Apache relies on processes and threads.
So, whats the difference?
How Apache works and why it has limitations
Apache creates processes and threads to handle additional connections.
The administrator can configure the server to control the maximum number of allowable processes.
This configuration varies depending on the available memory on the machine.
Too many processes exhaust memory and can cause the machine to swap memory to disk, severely degrading performance.
Plus, when the limit of processes is reached, Apache refuses additional connections.
Apache can be configured to run in pre-forked or worker multi-process mode (MPM).
Either way it creates new processes as additional users connect.
So one worker mode process handles more than one connection and one pre-fork mode process handles one connection only.
Moreover, worker mode is not thread safe.
So, when choosing modules and configuration you have to confront the thread-versus-process optimization problem and constraint issues.
If a thread is deadlocked, it does not know how to restart, thus remaining stuck.
Nginx
Nginx works differently than Apache, mainly with regard to how it handles threads.
(One rule of thumb is to have one worker process for each CPU.)
Each of these processes is single-threaded.
Each worker can handle thousands of concurrent connections.
It does this asynchronously with one thread, rather than using multi-threaded programming.
Nginx is composed of modules that are included at compile time.
That means the user downloads the source code and selects which modules to compile.
There are modules for connection to back end software servers, load balancing, proxy server, and others.
There is no module for PHP, as Nginx can compile PHP code itself.
The worker processes load the main ht_core Ngix process to for HTTP requests.
Nginx is said to be event-driven, asynchronous, and non-blocking.
Event means a user connection.
Asynchronous means that it handles user interaction for more than one user connection at a time.
Nginx compared with Apache 2.4 MPM
Apache 2.4 includes the MPM event module.
The goal is to reduce memory requirements as load increases.
MPM dedicates a thread to handle sockets that are both in listening and keep-alive state.
Nginx does not set up more processes per user connection.
Using Nginx and Apache both
Apache is known for its power and Nginx for speed.
Nginx includes advanced load balancing and caching abilities.
Apache servers can, of course, be deployed in great numbers.
A load balancer is needed so that exploit this.
Apache has a load balancer module too, plus there are hardware based load balancers.
Another configuration is to deploy Nginx with the separate php-fpm app.
When is Apache preferred over Nginx?
Apache comes with built in support for PHP, Python, Perl, and other languages.
For example, the mod_python and mod_php Apache modules process PHP and Perl code inside the Apache process.
The same is true for mod_rails and mod_rack, which give Apache the ability to run Ruby on Rails.
These processes run faster inside the Apache process.
For PHP, it does not matter as Nginx supports PHP internally.
So you could choose the best solution for your needs.