--> -->
Sun

Apache vs. Nginx. Choosing the best server for your business website

Hostens Data Center
June 12, 2018
Author: Hamster, www.hostens.com

Nginx and Apache are two of the most popular web servers. Both of them take approximately 70% of the market. While both web servers have similarities, at the same time they work rather differently.
In this article, we’re going to highlight some of the pros and cons to help you decide which one to choose for your server.

Benefits of Nginx

Nginx is often prefered by server administrators because it does not require a lot of resources and responds very well under heavier load. It is the preferred option of those who have a static content, which Nginx serves quickly. This web server is designed to pass dynamic requests off to other software that is better suited for those purposes.

Nginx spawns worker processes, each of which can handle thousands of connections. For this reason, it is great for handling a lot of simultaneous connections on the same server and, being event-driven, consumes a little less memory than Apache. Memory and CPU usage tend to stay relatively consistent even with a heavy load.

One of the downsides when using Nginx is that “.htaccess” is not supported. This means that rewriting rules and other customization need to be done in the main Nginx configuration file, which is not an option for non-privileged users.

Benefits of Apache

Apache is great for running small sites and suits different types of operating systems. Apache handles the connections in a variety of “multi-processing modules”, or MPMs. MPMs architecture allows flexibility in choosing and handling different dynamic connections.

MPMs handle clients’ requests with three types of requests that handle the algorithm:

  • mpm_preforke
  • mpm_worker
  • mpm_event

Every one of these requests is required for a different server and loading.

On the other hand, Apache is not great for many concurrent connections as it requires a lot of RAM. The web server can stop working when the load is high, because it is process-based.

Main differences of Apache and Nginx

Apache and Nginx share many qualities, but they cannot be considered interchangeable. One of the main differences between these two web servers is how they handle static and dynamic content.

Apache vs. Nginx  static and dynamic content

Apache handles static content using its conventional file-based methods. Dynamic content is processed without external components using dynamically loadable modules. Nginx, on the other hand, cannot handle dynamic content itself. It has to pass the dynamic content to an external processor for execution and wait for the rendered content to be sent back.

Apache allows additional configuration on a per-directory basis by interpreting directives in a “.htaccess” file. It is possible to allow non-privileged users to control certain aspects of their website without granting them the permissions to edit the main configuration file.

Nginx htaccess file

Nginx does not provide solution for per-directory configuration, because it does not check the files system until it is ready to serve the request. This explains why it does not have the “.htaccess” file. However, this comes as an advantage in increased performance and serves requests faster.

Apache module system allows you to load or unload modules dynamically. Nginx modules must be selected and compiled into the core software as they are not dynamically loadable. This has changed after 2016 and some of the dynamic modules are now supported.

How it differs?

NginxApache
Processing static content++
Processing dynamic content+
Effective at high load+
Requires little memory to run+
Good at customizing user privileges per directory+
Event driven+
Process driven+

An example

The easiest way to understand how Apache or Nginx works is to look at a shopping example.

When a customer comes into an Apache “store”, there is only a number of available shopping assistants. The customer has to go to the shopping assistant and tell him what he needs to buy. Then the shopping assistant goes around the shop and finds the needed products and hands them to the customer. Only after finishing with the first customer, the shopping assistant can proceed to another customer.

It is a great system for a small number of customers, however, when there are a lot of customers coming in and there are just a few shopping assistants available, the shop can become crowded and a huge line may form.

This example explains why Apache cannot handle a lot of simultaneous connections at the same time and requires more resources to run smoothly.

What makes Nginx faster?

A Nginx “store” works in a different way. The customer comes in and checks the store himself. Only after finding the required item, the customer finds the shopping assistant and pays for the product. In this case, there can be a lot of customers checking the store at the same time and no line will form, as a result, as the whole shopping process will be more effective and faster.

This is the reason why Nginx is much faster and requires fewer resources. The issue here is only if a “customer” requires something that is not available at the store, but is instead at the back of the storage (dynamic). The shopping assistant cannot help this kind of customer.

Conclusions

Above it was discussed how the Nginx and Apache work as web servers and what the main differences between them are.

If you are looking for a web server for a more static website or have a lot of connections, then choose the Nginx. If you have a lot of dynamic content or have many users with different privileges, Apache is the way to go.

There is also a possibility to use Nginx and Apache together by placing Nginx in front of Apache as a reverse proxy. This will allow Nginx to handle requests from all clients and will have the advantage of the fast processing speed and ability to handle a large amount of requests at the same time.

This advantage will be used for the static content. While for dynamic content, for example, PHP files, Nginx will proxy these requests to Apache, which will process the results, and Nginx will send the content back to the client.

Related articles

Have any questions? Do not hesitate to contact us!