Nginx vs Apache: In-Depth Comparison for Everyone

Anna Hmara

Senior Technical Writer

Most developers know that Apache and Nginx are most popular web servers that process 50% of files in the internet. Though, even most experienced programmer may find it difficult to choose a right web server for a project. That is why we decided to bring light to this topic and talk about the difference between Nginx and Apache.

We will give you a full overview of both web servers, and tell you everything you need to know about Nginx vs Apache dynamic, static, operating system support, flexibility, security and documentation. In the addition we will share with you a secret how to use Nginx and Apache together. Are you ready to explore?Let’s move on!

Nginx vs Apache. Scalability problems

Web server architecture research started because of large number of concurrent request and C10k problem. It means when your 10000 clients request your website at the same time, your website may face scalability problem.

That is why we decided to devote this article Nginx and Apache, and the difference between these two web servers.

Nginx: Brief Overview

It is interesting to know that  Nginx was created in 2004 by Russian developer Igor Sysoev, but only 5 years later the company with the same name started to provide support for their clients.

Generally speaking, Nginx is a web server that was created as a solution to the C10K problem. So, Nginx made it possible to handle 10, 000 simultaneous clients connections on the same server. It is an open-source web server and  has many common features with Apache. Though, Nginx prefers to deal with high traffic and focus on scalability and performance.

Unlike Apache, where the page load speed can decrease with number of visitors, Nginx can provide predictable and consistent performance. This is because Nginx is based on event-driven architecture, which means it does not  need to call a new process for each request.

Furthermore, Nginx can deal with four times more requests per second. That is why Nginx is so  popular among developers who has a large project or running a website with high number of visitors.

Apache: Brief Overview

Apache is free open-source cross-platform web server software, as well as Nginx. Though, unlike Nginx, Apache was developed and maintained not by one person, but it was created by open community of developers.

It became popular in 1996, when Apache played important role in the initial growth of the World Wide Web and overtook NCSA HTTPd as a dominant HTTP  server.

The main advantage of Apache is its convenience and popularity. According to the statistics in 2009, Apache was the first web server software to serve about 100 million websites.

In July 2016, Apache supported 46 % of all active website, which means it served a half of the whole bunch of websites in the world.

It is interesting to know that according to the FAQ in the Apache project website, the name  Apache was given out of respect to the Native American tribe Apache. This tribe had superior skills in warfare and strategy. Though, in  official interview one of the creators of Apache, Brian Behlendorf, stated that name Apache came out of the blue. Brian put this name on a page and when the project was started, he asked people if it was a good name for a company. People said it was a nice pun and this  is how Apache became a name of a most successful web server company.

Nginx vs Apache: Pros and Cons

First, if you want to make the right choice between Nginx and Apache, let’s talk about differences in Nginx and Apache web servers.

Apache

 

There are three main Apache Multi-Processing-Modules we would like to mention in this article. These are:

  • worker model

Main feature of this model is a single control process. On the one hand it is great model as it creates child processes. Then these child processes allows to develop listener thread and many other threads as well. The main task of listener thread is to listen for connection, and only then it passes it for processing thread. However, there is one disadvantage of this processing module. It can cause process bottleneck due to single control.

  • process model

Another Apache MPM model is process or,like developers like to call it,  “pre-fork” model.This method is worse than worker MPM model, because it can’t scale as good with concurrent connections as worker model. Another disadvantage of this model is that it takes a lot of RAM and sometimes reject connections when the load is high. That's why process MPM model is better to use for small projects.

  • event model

And the last but not least Apache MPM model is event model. This model has a lot of common features with worker model. Though,there is only one listener thread created by event model. Its task is to listen for connection and pass it to a working thread.

Main advantage of this model is quality, because it is easier to  manage long-running connections on a single thread. If you use the latest version of Apache, event model contains default setting and is considered a stable model.

Nginx

Unlike Apache, Nginx does not have many MPM models as it uses non-blocking connection handling algorithms. Furthermore, Nginx can handle multiple connections at the same time thanks to worker processes. The secret of Nginx speed is simple,it is fast looping mechanism that process events without a break. Worker use this mechanism to process requests. Another Nginx’s worker advantage is that it can work by itself, but when the event is triggered, it requires a connection.

In addition it would be nice to mention that Nginx connections can exist with other connections at the same time thanks to event loop. Moreover, events are processed asynchronously and that is why they does not cause any blocks.

So, answering the question which is better Nginx or Apache, we can say that Nginx is faster than Apache. It can scale projects even when the resources are limited.Every new request should not be processed by server and this is good for computer’s memory and CPU. It is possible, because Nginx is single-threaded.

Performance

Now, let’s talk about Nginx and Apache performance as this is a key factor in a web server decision making process. So, we used an HTTP server benchmarking tool Apache Bench in order to test Nginx and Apache.

In addition it would be nice to mention that about 25,000 requests took place during the test and different number of requests run at the same time to estimate web servers performance.

Memory

Why is measuring memory so important? The answer is simple. You need a web server that would save your memory, because raising memory is quite expensive, especially on VPS.

As you can see from our graph, Apache has a big difference with Nginx. It is difficult to believe that Nginx memory usage is so static and does not change even with 3000 concurrent connections, while Apache does not show a satisfied result.

To explain this we need to look at how Apache scales new requests, and will see that to process new request Apache calls new processes and this require more memory.

Requests per second

It is very important to know how many requests per second can a web server process, because it will influence a website speed when the traffic will be large.

On this graph you can see that Nginx is a strong leader in the battle of requests per second. Apache in this case can’t handle a large amount of requests per second.

Besides Nginx numbers are reduced with large number of concurrent requests, but still it can handle more requests than Apache.

Dynamic

According  to the Conetix research, if we compare dynamic content serving by Apache and Nginx, we can see that Nginx with PHP can handle the same concurrency as Apache with MPM.

Sometimes developers think that with Apache they will have problems with the page load, but it is easy to fix with a proper Apache module setup. Moreover, you can add more hardware, add a Varnish or Memcached caching layer, do load balancing and switch to faster PHP runtime. This will help you to speed up dynamic pages of your website.

Speed Comparison

According to recent study of Speedemy, Nginx consumed 4 % less memory and was two times faster, running with about 512 concurrent connections. What’s more, according to ESchrade research, Nginx was 2, 5 times faster than Apache running about 1, 000 concurrent connections.

Operating System Support

It is important to mention that Apache runs on Linux, Minix, BSD and other Unix-like systems. Moreover, Apache has full support for Windows. On the other hand, Nginx has not very strong support system for Windows, as it runs mostly on modern Unix-like operating systems.

That is why developers that prefer to work with Windows, like Apache more than Nginx.

Flexibility and Security

Most experienced developers know that they need to write modules in order to get customizations to the web server. Unlike  Nginx, Apache has dynamic module loading and all is modules support this.However, in 2016 Nginx was improved and got support for dynamic module loading, but unfortunately most modules do not yet support dynamic loading.

We hope in the future Nginx will find a solution to this problem,but now as you can see Apache has benefits on this point.

Sometimes developers say that both web servers, Apache and Nginx, can be configured easily to be insecure. That is why it is important to understand how to configure each properly and know what each option you are setting does.

At the same time mod security also happens to work with both and have an excellent security track record for their C-based code base. Though, Nginx code base is smaller and this is a big advantage of Nginx in security battle.

Documentation

You can find a lot of information about Nginx on Beginner’s guide. Furthermore, you will learn starting, stopping and reloading Nginx configuration and much more useful things for beginners.

On the other hand, Apache has excellent documentation too. You can find the latest version of Apache and download it at Apache HTTP server project.

Support

Like Nginx, Apache has a strong community that is done through Stack Overflow, mailing list and IRC. Though, Nginx has its forum where developers and ask and answer different questions.

What’s more, Nginx creators have recently released a commercial product called Nginx Plus.

Nginx Plus offers support for a set of extra features such as media streaming, monitoring and load balancing.

Nginx and Apache for Most Popular CMS

  • WordPress

Chris Lea, successful designer, politician and political activist in Canada said that “Apache has a million option while you need only six, Nginx does those six things, and it does five of them 50 times faster than Apache”.

Talking about Wordpress, like many other websites, it has been recently moved to Nginx and now every second many requests are served by this web server. According to the statistics, about 10000 websites in the internet are served by Nginx,  and this number is growing with each day!

Though, if you think that moving from Apache to Nginx will be a magic solution, you are not right. First you need to find out the cause of the bottleneck and only then move to another web server.

So, before changing web server check your MySQL, WordPress Plugins, Apache and other parts of your website code.

Here is a list of WordPress requirements:

  • PHP

  • MySQL 5.6

  • Mod_rewrite ( only for Apache)

It would be nice to mention that both web servers support php fpm( fork process manager for PHP). That’s why response time can be very short and all the processes are initialized very quickly.

  • Joomla

If you are thinking about starting your online business based on Joomla, we suggest you to choose Nginx web server. Why Nginx? The answer is simple! First, Nginx was created to save server resources. Besides, it is fast and has all necessary features for Joomla.

On the other hand, some third party Joomla modules and components need .htaccess rules when you use Nginx web server, while Apache does not require these rules. Though, if you want to move your Joomla based project into Nginx .htaccess rules can be converted into nginx.conf, and this is not very difficult to do.

In addition would be great to add that Nginx is #1 web server for Joomla as for fast and secure performance, while Apache is easier to use for developers.

  • Magento

Today, developers still prefer to use Apache when they have Magento based projects.This is because Apache has configuration and application control. Apache is not difficult to configure and customize thanks to htaccess support, and there are many online tools that can help you to handle this issue.

Moreover, Apache is compatible with app-specific modules.

On the other hand, Nginx does not have htaccess support and can be used only with PHP-FPM. So, a technical issue can be caused even by a redirection. Moreover, Nginx is  good for static content, while Apache handles PHP requests very well.

In addition it would be nice to add that to run Magento you require a web server that can handle PHP requests and deliver static for your website. That’s why many developers use Apache and Nginx together with Cloudwaves FPC.

...for Programming Languages

  • ... for Python

Before you started to develop your project on Python it is so right to make a choice whether you use Nginx or Apache web server. Sometimes it is difficult to choose when you have already worked with Apache and at the same time heard a lot of good things about Nginx. In this case you need to consider all the pluses and minuses or both web servers.

Some developers think Nginx is not suitable for Python based websites, but this is not true. Nginx as well as Apache are good for Python based projects. Moreover, Nginx is faster and lighter than Apache, and it does not require building from source like Apache.

On the other hand, Apache is easier to work with and has more features. So, you need to consider your application needs and make a decision.

  • ... for PHP

According to a recent research Nginx works better with PHP FPM. This is a software package that manages Fast CGI processes for PHP. So, abbreviation FPM means FastCGI Process Manager.  

Before PHP FPM developers used PHP as Apache module, but today most developers would agree that it is better to move to PHP FPM with mod event.

Moreover, some developers prefer to use PHP FPM together with Nginx, because the last processes static data, while PHP FPM works on script.

On the other hand, some developers use Nginx together with Apache and mode_php module. Though, it works much slower than PHP FPM + Nginx.

  • ... for Java

If you are still thinking about a perfect web server software for your Java based project, Nginx will help you to solve this problem. Nginx has possibility to handle many attacks, that’s why Java developers give it preference over Apache.

Moreover, you can use Nginx as a proxy. You just need to add some lines of code like:

In addition it would be nice to mention, that when proxy is simple, nginx can’t show its capabilities. That’s why it is essential to divide static to instruct Nginx and static from Java into different directories. In this case you will be able to move Java files to the Java server without any difficulties.

Using Nginx and Apache Together

Now, when you found out the advantages and disadvantages of both web servers, it is time to share with you a secret. This secret is quite simple. There is no need to choose between Nginx and Apache, because you can use them together and enjoy the benefits of both web servers.

First, you need to install Nginx on your computer, so it will be the front end of the website. After that you can configure the virtual host to serve on the front end.

Sometimes you can run Nginx and Apache on port 80. A good example in this case is a server running WHM/ Cpanel.This server usually  does not support Nginx,and as the result do not mess up with Apache configuration. Though, in order to make it possible you need to bind Apache to one IP address and Nginx to another one.

When you make Nginx a reverse proxy, this allows this web server to manage all requests from clients. Moreover, Nginx lets you manage many connections at the same time and thanks to high processing speed it can do it really quickly.

Another advantage  of using Nginx and Apache  together is that Nginx work with static content and serves files right to a client in no time, while dynamic content will be sent to Apache, and after processing the data all the files will be returned to Nginx. Then Nginx send the files back to the client.

Today, many developers like using Nginx and Apache together, because Nginx sort all the information and manage every request. Though, when Nginx can’t handle some files, it pass it to Apache. In this way developers can save their precious time they spend for processing information through Apache web server as Nginx manages everything much faster.

Sum up

In conclusion, it would be nice to mention that everything changes, and Nginx and Apache are not exceptions. That is why we can say without no doubts that Apache as well as Nginx are strong competitors. While Apache is #1 web server for active sites, Nginx takes second place in this battle of titans.

When we compare Nginx with Apache for dynamic content, these two web servers are similar. Though, for static content Nginx is a champion as it is two times faster than Apache.

Furthermore,Apache is good at loading various dynamic modules, while Nginx has just added this feature.

If your company prefer shared hosting, Apache will be a perfect solution for you. Though, if you like dedicated hosting, VPS hosting or cluster container, you would rather use Nginx web server.

And you should remember about ability to use Apache and Nginx together, as this is a perfect combination of sorting machine and processing of client’s requests.

Above all, it seems pertinent to remember that before choosing web server, you need to pick up a solid Linux hosting provider. If you need more information about hosting providers you can follow this link and find out more information about the most popular hosting providers in 2022

All things considered, it seems reasonable to assume that Nginx web server fits high traffic website owners that need to serve a lot of media streams or static content. On the other hand they can use combination of Nginx and Apache. Besides, if you have a simple website, both web servers work for you.

If you find it difficult to choose between Nginx and Apache, and need professional help, we are always glad to help you.

We have a team of professional developers who can help with your project development from scratch or find some solutions for your existed project.

About KeyUA Our projects Our developers