Nginx vs Apache

By Bangar CH

November 19, 2016

Apache, Nginx

Introduction

In this article we are going to discuss important features of Apache and Nginx. Also, I am going to explain advantages/disadvantages of Nginx vs Apache.​

Apache​

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.

Apache process

Also, when it reaches the maximum limit of processes, Apache refuses to handle additional connections.

It can be configured to run in pre-forked or worker multi-process mode (MPM). Either way, it creates new processes as additional users connect.

The difference between the two is that pre-forke mode creates one thread per process, each of which handles one user request.

Worker mode creates new processes too, but each has more than one thread, each of which handles one request per user.

So one worker mode process handles more than one connection and one pre-fork mode process handles one connection only.

Worker mode uses less memory than forked-mode because processes consume more memory than threads, which are nothing more than code running inside a process.

Moreover, worker mode is less secure. That means if you use non thread-safe modules like mod_php, to serve up PHP pages, you need to use pre-forked mode, thus consuming more memory.

So, when choosing modules and configuration you have to confront the thread-versus-process optimization problem and constraint issues.

The limiting factor in tuning Apache is the memory and the potential to dead-lock threads that are contending for the same CPU and memory.

If a thread stops, the user waits for the web page to appear, until the process makes it free, so it can send back the page. If a thread dead-locks, it does not know how to restart, thus remaining stuck.

Nginx

Nginx works differently than Apache, mainly with regard to how it handle threads.

This does not create new processes for each web request, instead, the administrator configures how many worker processes to create for the main Nginx process. (The thumb rule is to have one worker process for each CPU.)

Each process is a single-thread process. Each worker can handle thousands of concurrent connections. It does this asynchronously with one thread, rather than using multi-thread programming.

Nhinx Thread

The Nginx also spins off cache loader and cache manager processes to read data from disk and load it into the cache and expire it from the cache when directed.

It is build with modules that includes 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 application servers, load balancing, proxy server, and others. There is no module for PHP, as Nginx can compile PHP code itself.

Here is a diagram of the Nginx architecture taken from Andrew Alexeev´s deep analysis of Nginx and how it works.

Nginx Architecture


Using both Nginx and Apache

When comparing Nginx vs Apache, Apache is known for its power and Nginx for speed. This means Nginx can serve up static content quicker, but Apache includes the modules which require to work with back-end application servers and run scripting languages.

nginx and apache

Both Apache and Nginx can serve as proxy servers, but when we  compare Nginx vs Apache, using Nginx as a proxy server and Apache as the back end is a common approach to take.

It includes advance load balancing and caching abilities. Apache servers can be deployed in great numbers. It require a load balancer in order to exploit this.

Apache has a load balancer module too, plus there are load balancers which integrates hardware.

Another configuration is to deploy Nginx with the separate PHP-fpm application. We say that PHP-fpm is an application because it is not a .dll or .so loaded at execution time, as is the case with Apache modules.

Php-fpm (the FastCGI Process Manager) includes with Nginx to deliver PHP scripting ability to Nginx to render non-static content.

Performance & Speed

Both servers are capable of serving a huge number of requests per second, but Apache’s performance starts decreasing as you add more concurrent connections. Here Nginx performance doesn’t feel any pain at all.

Comparison

Nginx performance and speed, in general terms, overcomes Apache by 2x to 3x times for any simple PHP-base website.

Another design difference between both is that Nginx doesn’t support .htaccess files for the simple fact that .htaccess files are a resource hog.

If you have AllowOverride at all in your Apache configuration (default setting is "enable" ), you are telling your Apache server to search in all the directories of your document root to see if there are .htaccess files and then execute the instructions inside.

Due to this reason, Nginx rewrite rules are built into the core of the Nginx configuration, giving you an exact place to look into and of course faster application performance.

Developers who were lazy, Apache is fine with its .htaccess structure. However, for developers who really value web performance & security, Nginx is a perfect choice.

Community & Commercial Support

The Apache community has been around since the late 90’s. It has very huge community around the globe.

webserver support

Nginx was born in 2002, but being a young warrior doesn’t mean it lacks support or documentation. In fact, in the past years, the Nginx community has been growing a lot.

More and more blogs, user communities, and companies are evangelizing Nginx (NginxTips.com for example) and acknowledging how it’s changing the world of web performance.

If you find yourself as a newbie in the Nginx world, you can get commercial support from Nginx.com, the original developers of the product.

Apache hasn’t offered any commercial support since its beginning.

Apache vs Nginx

Here, I will compare how both servers distinguish in their own way.

1. Configuring Apache is easier than configuring Nginx.
2. When compare to Nginx, Apache has excellent documentation.
3. If we compare Nginx vs Apache, Nginx uses Event-driven Architecture (EDA) whereas Apache is based on process-driven architecture.
4. Apache has blocking architecture while Nginx has non-blocking nature.
5. When it comes to Nginx, it uses Single-thread that means that it doesn’t create a new process for a new request. But in Apache, a new process creates for a new request.

Below are few more comparisions


6. Once if you compare Nginx vs Apache, Nginx has a very low memory consumption for the static page but, because of the requirement of creating a new process for each request, Apache has a very high memory consumption.
7. Static pages server extremely faster with Nginx when compare to Apache.
8. Nginx lacks the support of operating systems such as OpenVMS and IBMi, but Apache supports the complete range of operating systems.
9. As Nginx only comes with required core features, it is much lighter than Apache.
10. The performance and scalability of Apache are heavily dependent on memory and CPU, whereas Nginx is not completely dependent on such hardware resources.

Conclusion

When you are looking for Nginx vs Apache we normally recommend to check your requirements and when dealing with many users you can surely go with Nginx.​

Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Create a website in 3 simple steps

Choose a website template, add features, then customise! - Free Online Website builder.