What is Nginx and Apache
Nginx (Pronounced Engine-x) and Apache, both are open source web servers.
The Apache web servers are the oldest ones around. They have been in use since 1995, whereas nginx came some where around 2002. Both the products are open-source and free but if you need support for nginx you will need to buy Nginx Plus version. Nginx was written primarily to address the C10K problem and some performance and scalability issues associated with Apache.
How They Work
Apache handles HTTP Requests sent to it. It 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, thus severely degrading performance. Plus, when the limit of processes is reached, Apache refuses additional connections. Apache is comprised of Two main building Blocks with the Latter being comprised of many other little building blocks. The Building Blocks are the Apache Core and then the Apache Modules that in a sense extend the Apache core.
Apache is like Microsoft Word, it has a million options but you only need six. Nginx does those six things, and it does five of them 50 times faster than Apache.
— Chris Lea on nginx and WordPress
Nginx works differently from Apache, specially how it handles threads. It uses Event-driven, asynchronous, non-blocking and single threaded architecture.
- Event Driven : The starting and completion of events are marked by notifications, therefore the resources can be used by the other process until a process start or completion notification is received.
- Asynchronous : Threads can be run concurrently, thus they do not block each other.
- Single Threaded : As the resources are not blocked therefore, multiple clients can be handled by a single worker process.
The following diagram represents the Nginx architecture :
Pros and Cons
- Large number of Modules: Apache been available since many years and has a larger community base, therefore has lots of Open-source modules written to expand it’s functionality.
- Open Source : Everything relating to Apache is free, be it there modules or there support.
- Large number of Users : Nearly 46% of all websites run on Apache.
- Less Secure than nginx.
- Requires a heavy server configuration to handle high traffic websites. Since it spawns new process threads for each request, Apache requires a high memory footprint.
- Fast : It does not create a new process for each new request, therefore it becomes very fast.
- Lightweight : As Nginx comes only with the core features required by the server, it makes it lightweight in comparison to Apache and hence consumes less memory as well.
- Easy Scalability: Nginx is highly scalable, and it’s performance is not dependent on hardware.
- Does not support Operating Systems like OpenVMS and IBMi.
- Limited support: The Nginx community is not as big as the Apache community and it is repetitively newer as compared to Apache, so the community is also smaller for now.
With the above advantages in mind, more and more people are moving towards Nginx. Even WordPress.com was shifted over to Nginx and they have thousands of servers, serving thousands of requests per second. Also Nginx has become the number 1 web server for the top 10,000 websites on the internet (Read more).