Performance Comparison : Apache and Nginx

May 03, 2017

Apache and Nginx

Apache and Nginx are the two most popular Web Servers powering the Internet. The most popular in between them is Apache. While 49% of the total web still uses Apache for powering their websites, and Nginx shares 33.7% traffic.

But on researching more about them I found that Nginx surpasses Apache in many factors. Internet is using Nginx more on the top sites. Stats says that Nginx shares 51.6% traffic on top 100,000 sites while Apache shares 30.7%.

And the Top 1,000 sites of the world or you can say Busiest sites uses the nginx server (53.8%) where Apache lacks behind to only 19.2% of its share on traffic.

All the Stats are from w3techs.com.

Because of too many reasons I’ve moved my server from Apache to Nginx. One of the reason was the traffic hike during the Scintilla Selfie Contest. When because of some unexpected traffic my server crashed. And was down for around 1 hour.

Internet is full of all the info about the differences and features of these two web servers but I haven’t find a website that ensures about the performance  of the Nginx server. So I decided to do some load test on both of the Servers.

Setup :

System Specification –

  1. Operating System – Amazon Linux AMI
  2. RAM – 1GB
  3. PHP 5.6
  4. MySQL 5.5
  5. WordPress 4.7.4 with Comet Cache plugin
  6. GZip enabled in both servers

Apache Stack Specification –

  1. Httpd 2.4
  2. mod24_ssl
  3. supports http1.1

Nginx Stack Specification –

  1. Nginx 1.10
  2. supports http2.0

Tests Configuration :

For load tests I’ve used Loader.io. They provide 3 types of load tests for websites. For single domain the services are free, but if you want to test too many websites then they have some premium charges.

Tests Types:

  1. Maintain client load
  2. Clients per second
  3. Clients per test

Testing :

Test 1 – Maintain client load

Configuration :-

Clients Duration Error Threshold Timeout
0-250 1 min 100% 10 sec

Apache : Maintain client load

Apache : Maintain client load

Nginx : Maintain client load

Nginx : Maintain client load

In this test loader performs a increasing clients test from 0-250 clients over a span of 1 min. you can clearly see that in Apache Configuration the error rate is 23.8% where in the Nginx setup it is about 2.3 %. also the apache fails in between of the tests with timeouts. where the Nginx is started failing at 50th sec to 60th sec.

Point Table – Nginx : 1 :: Apache : 0

Test 2 – Clients per test

Configuration :-

Clients Duration Error Threshold Timeout
0-250 1 min 100% 10 sec

Apache : Clients per test

Apache : Clients per test

Nginx : Clients per test

Nginx : Clients per test

This test is for determining that how many clients the server can handle at a time. Apache fails after 10 sec for even for serving 3 clients per sec. where on an average we can say Nginx is handling 4 clients per sec. with a peek of 7 clients per second.

Point Table – Nginx : 2 :: Apache : 0

Test 3 – Clients per second

Configuration :-

Clients Duration Error Threshold Timeout
250/sec 1 min 100% 10 sec

Apache : Clients per second

Apache : Clients per second

Nginx : Clients per second

Nginx : Clients per second

This test is for determining that how many request per second a server can handle. You can see that on requesting 250 request in a second the server doesn’t respond for 10 sec. and then fails with 100% error threshold. Where Nginx work as a shock absorber in this test, and respond to some of the requests that it can handle. It is like making a queue for the requests and sending them to the current free worker process. And hold them till the working process free some memory.

The reason why Apache failed when getting too many requests is because Apache uses a prefork method for serving. Means it creates processes for each request. Because of getting too many requests at a single time and with a lack of memory(only 1GB RAM) it asked for too much memory and crashed. This is what happened on that day, because of occupying too much memory by Apache and MySQL I was not able to access my server via SSH at that time.

Whereas the Nginx server work as a Shock Absorber and create a queue for all the requests and serve them with their token number. Some of the requests from them got higher then 10sec of Timeout threshold so they are represented in the graph with orange color.

Apache error threshold 100% where Nginx 47.1%.

Point Table – Nginx : 3 :: Apache : 0

Results :

You can clearly see that Nginx gave better performance then the Apache Web Server. But still it can be optimized for more performance. By adding Cache tools because Nginx serves Static files much better than any other server.

We can also optimize the Nginx server performance :

  1. Pagespeed module for Nginx by Google
  2. Minifying CSS/JS
  3. Using a better Cache Plugin for generating static files because Nginx surpasses almost every server in serving static files.
Next Challenge :

For the next challenge I am thinking for using Nginx as a proxy server for Apache. Because I have read somewhere that The most powerful combination is that using Nginx in front of the Apache with cache enabled.

Also there is another combination of Varnish and Apache. Where Varnish is not a Web Server but it can be used as reverse proxy and caching static pages. It is used by lot of websites like GitHub.com etc.

Also because I’ve analysed that PHP-FPM is using too much memory. Where Apache is good option for using with PHP and Nginx is better at serving Static Files it will be a better option to use them together.

Links to all the test with full report :

Apache:

  1. Maintain client load
  2. Clients per test
  3. Clients per second

Nginx :

  1. Maintain Client Load
  2. Clients per test
  3. Clients per second

Other :

When I added SSL into my website last time, It was not as much secure as it should be. It was some of the Obsolete Ciphers that can make some vulnerabilities. And so I got a **“B” ** Certificate.

SSl Rating B for shubhampandey.in

SSl Rating B for shubhampandey.in

Now with a proper configuration and Enabling **“HTTP2” **in my site I have got new Rating for website and that’s an “A+”.

SSl Rating A+ for shubhampandey.in with HSTS enabled

SSl Rating A+ for shubhampandey.in with HSTS enabled

You can also generate a good configuration from the Mozilla’s SSL Configuration generator website where they provide many options for creating a configuration that can get you an A+ rating 🙂.

Thanks for reading if you have any comments or feedback you can reach out to me on twitter.

Up Next