APACHE vs NGINX - Who WINS in Performance?
Tags: apache, tutorial, devops, webdev
Published: 2022-11-30 20:14:20

## Quick Intro for Apache & Nginx

### Apache:

open-source cross-platform web server which is also known as “httpd” and Apache. It was developed by the Tim Berners Lee in 1995.

Mostly run on Linux, Apache powers around 46% of all the website around the world.

It is the key component of the LAMP (Linux, Apache, MySQL, PHP) stack.

### Nginx:

A web server that was released in 2004 by Igor Sysoev. Its not just a webserver! NGINX is more popular than Apache.

> 💡 **Nginx is often used as a reverse proxy, load balancer, mail proxy and for HTTP caching.**


## **Apache Vs NGINX**

The design architecture represents the most significant difference between APACHE and NGINX. APACHE uses a **process-based** approach, Whereas NGINX uses an event-driven architecture.

To Explain in an easy way what the difference between the above design architecture is to imagine for a moment that the web server is a busy coffee shop, and the customers in the shop are the requests that the web server must handle.

**Which coffee shop handles its customers more efficiently?**

### **The Process-Driven approach:**

**In the process-based coffee shop, each employee serves one customer**. The employee takes the order, pours the coffee, and collects the payment.

If there are three customers in the shop, there must be three employees to serve those customers. This is a manageable scenario in a shop that serves few customers or has a simple menu such as coffee and donuts.

But what happens when there are ten customers in the shop? And each of them wants a mocha latte with no whip and a heated croissant? The first three customers to place their orders probably get served pretty quickly, but the other seven? They will have a long wait. Many customers won't have that kind of patience and will go elsewhere for their coffee.

In order to solve the efficiency problem in the process-based coffee shop, you need more employees. And in order to fit all of those extra employees behind the counter, you’ll need to expand your shop. You may need to move to a bigger location.

So process-based Apache web server in order to deal with a large number of requests, you need more processes, and that means you’ll need to buy more hardware.

If processes are not powerful enough Apache can eat your server for breakfast.

## **The Event-Driven approach:**

**In the event-driven coffee shop, each employee serves multiple customers**. One employee takes the orders and the payment and delegates the order-filling tasks to other employees.

The employee at the register gets to all ten customers in a timely manner, because all it needs to be doing is to take the order, relay instructions to the baristas on the backend, and take the payment. So in meanwhile the backend baristas are making coffees, and lattes, and warming up croissants as needed.

This is an ideal solution that scales well, as one employee can do the work of many.

In the same way, Nginx Web Server saves you money. With fewer processes doing the work of many, your server can handle a large number of requests quickly. And without the need for more hardware.

So for static content like images, CSS, and HTML, the advantage of an event-driven model is clear, Instead of consuming the overhead required for a new process for each request, it needs one process to handle the requests as they come in. 

This is faster and consumes significantly less CPU and memory than Apache.

## **Performance**

### **Apache**

- **Serves static content using the file-based method**

Apache handles static content using its conventional file-based method.

### **NGINX**

- **At serving static content, Nginx is the king it uses an event-driven approach**

---

## **OS Support**

### **Apache**

- **Supports all Unix-like systems including Linux and BSD.**
- **It fully supports MS-Windows**

### **NGINX**

- **Supports almost all Unix-like OS**
- **Supports Windows partially.**

---

## **Centralized Configuration**

### **Apache**

the architecture allows non-privileged users to control certain aspects of their website without granting them permission to edit the main config.

### **NGINX**

It doesn’t provide additional configuration. 

**However, this is to increase NGINX performance by not allowing directory configuration It can serve requests faster than Apache.**

---

## **Request Interpretation**

### **Apache**

- **Passes File system location**

Provides the ability to interpret req. As a physical resource on the file system location that may need more abstract evaluation.

### **NGINX**

- **Passes URI to interpret requests**

Due to the architecture being a reverse proxy server Nginx use URI as a request, so passing requests as URI instead of file system locations allow Nginx to function easily in both web and proxy server.

---

## **Security**

both of the web servers have great security for their C-based code base.

**However, Nginx has Better security because its code base is significantly smaller than the apache code base.**

---

## **Use Nginx and Apache -Together**

It is possible to use each server’s strengths as it can use Nginx in front of Apache as a server proxy. 

For static connections, Nginx will serve the files quickly to the clients. For dynamic content, like PHP files, Nginx reverse proxy server will proxy the request to Apache then process the results and return rendered page requested.