I wrote a little while ago about how, for running PHP, Nginx was not faster than Apache. At first I figured that it would be and then it turned out not to be, though only by a bit.
But since Apache also has an event-based MPM I wanted to see if the opposite results were true; that if Apache were using its event MPM it would be about the same as Nginx. I had heard that Apache 2.2’s event MPM wasn’t great (it was experimental) but that 2.4 was better, possibly even faster, than Nginx.
So I had a few spare moments this Friday and figured I would try it out. I basically ran ab at concurrency levels of 1, 10, 25, 50, 100 and 1000. Like before the results surprised me.
The first run with Nginx was impressive. It peaked at 14,000 requests per second. Given my wimpy VM that I ran it on, those numbers are pretty good. What surprised me was that Apache was only half that. I will say for the record that I do not know how to tune the event MPM. But I don’t really have to tune Nginx to get 14k requests per second so I was expecting a little better from Apache. So I pulled out all of the LoadModule statements I could but still have a functional implementation of Apache. While the numbers were 25% better or so they were still well shy of what Nginx was capable of. Then I added the prefork MPM to provide a baseline. Again, I was surprised. The event MPM was faster than the prefork MPM for static content, but not by much.
So it seems that if you are serving static content Nginx is still your best bet. If you are serving static content from a CDN or have a load balancer in front of Apache which is running PHP then the prefork MPM is the way to go. While the event MPM will help with concurrency it will not help you speed up PHP and so is not really needed.
Comments
Gwyneth Llewelyn
I guess it’s time for you to run a benchmark with a “true” PHP application behind it 🙂
And let us know what version of nginx you’re using… I’m using mostly 1.4.3 and 1.4.7 (I have no idea if there are substantial performance issues between them, the changelogs show mainly bug fixes and security patches)
BrianLayman
Perhaps I am confused.. doesn’t this show that nginx is way faster than Apache 2.4 MPM?
kschroeder
Gwyneth Llewelyn A “true” PHP application would actually make the results a little more murky because the test would likely involve more logic to be executed. And, largely (though there might be some circumstances that might negate this), once you are in PHP, the web server doesn’t matter much. As such the true performance difference would be minimized. The actual performance difference between the two, once a true PHP application is executed, is negligible and the performance benefit from Nginx largely comes from dishing out static content.
kschroeder
BrianLayman Yep, it does show that Nginx is faster than Apache. I was hoping that Apache’s Event MPM would bring Apache close. In the end it doesn’t. The “faster” that I referred to was that Nginx has been reported to be faster than Apache and is, therefore, always a better choice. My quick test was the previous blog post that I referenced which showed that, when running PHP (that’s key), the Apache prefork MPM is faster. Just a smidge, but faster. That benefit goes away, however, once static content is included at which point Nginx is faster.
Gwyneth Llewelyn
kschroeder Good point… and the benchmark would actually only reveal performance data for that single application, and not others. So I guess you’re right, you did your benchmark well.
SM
Were these tests run with allowoverride off?
Kevin Schroeder
That is actually a very good question. I don’t recall.
Christoph Lehmann
I bet allowoverride were turned on…
Joshua Webb
I have for the past week or so been setting up an Load Balancing environment and have tried quite a few combo’s, I have tried Varnish with a Nginx and apache Backend. Haproxy with a Nginx and Apache backend. A Nginx with a Nginx backend and now my final setup and a Nginx Load balancer with apache2 running MPM event and php-fpm and its loads faster than any of the other combos. my question is going to be one the backend servers start getting load how they are going to hold up LOL
nginx: FastCGI Caching Basics | vancluevertech.com
[…] Some numbers put nginx at around twice the speed – or faster – of some Apache MPMs, even on current versions. Out of the box, I recently clocked the memory footprint of a nginx and PHP-FPM stack at roughly […]
meilleure configuration kimsufi
[…] simples dont les applications sont grandement composées (CSS, images, javascripts, html pur). Ce test montre d’ailleurs à quel point la différence est vrai. Le language de configuration y est […]
dE
So that’s an Apache loaded with unnecessary modules (default config), directives, scanning and loading changes to .htaccess files and loads of features which Nginx will never have for years to come vs Nginx.
I just benchmarked the 2 (nginx 1.6). The problem is Nginx is designed for benchmarks. If you tune Apache for benchmarks, it’ll outperform Nginx and it did in my case.
SV
how do I repeat your benchmark?
Gary Mort
Are you running Apache Event MPM + mod_php or Apache Event MPM + fastcgi?
Apache Event MPM + mod_php really does not buy much[I’m not really sure how it would buy anything actually]. With mod_php each Apache thread is going to execute PHP directly. So it makes almost zero use of libEvent, libUv or whatever flavor of the month.
Using event, the idea is that when it comes time to execute a cgi script, the processing is passed off to a separate server and the Apache worker thread goes to sleep until there is a response or a timeout. This means that the libEvent server can process many more requests as since it can run more workers.
This is how nginx works. Nginx was built to use libevent from the start which helps it to be so screamingly fast. So to test it, you have to use Apache Event MPM + fastcgi. In this case, the performance for service PHP will be based on how the web server handles it’s worker threads.
Moreover, not serving longer running larger content generating scripts is not really useful. Since the whole purpose of libevent is to allow more processes while waiting for results, server 100byte fast results means your not testing how the server handles having to suddenly handle 100 17k different responses from FPM. Here it will be wildly different since Nginx counts on persistent fpm connections by default and Apache counts on unique ones. So 100 17k results all coming over a single fastcgi connection will be processed differently then 100 17k results over 100 sockets.
Nginx also performs some special caching tricks on small responses. Even though you had still had to transfer the same 100bytes over and over to Nginx, once nginx receives it – based on the hash of the data it will send the data from it’s copy in cache – avoiding having to copy the response from one region in memory to another region in memory, run it through some filters, and then copy it again. It only does this for small responses.
Lastly, I really dislike the common method of benchmarking you ran ab 1,10,….
So basically what your really testing is how does Nginx scale as traffic increases from low to high. Nginx default configuration is to run many many more processes/threads then Apache does. So in Apache you go: 1 concurrent, 5 concurrent, 10 concurrent – opps Apache has to start up another 10 threads, 15 concurrent, 20 concurrent – opps time to start another 10 threads. That is why if you look at the raw data you see these really weird sudden drops in performance as concurrency crosses the new thread boundary.
Run it in the opposite direction and you will see an initial high performance for nginx[ramps up faster to 10,000 concurrent] but then as you go down the charts start to differ..nginx is more aggressive about cleanup/killing extra threads. Try varying back and forth, 10,000 to 5000, to 8000, to 500, to 3000 Of course this will not be a nice chart – but it will be real world.
Gwan has some interesting benchmarking tools and a different metric. I don’t agree with the author on some of his choices[he’s still using small files and sequential ramp up] but his tools do a better job of tracking memory.
NGINX vs. Apache (Pro/Con Review, Uses, & Hosting for Each) – 海浪家园
[…] NGINX is about 2.5 times faster than Apache based on the results of a benchmark test running up to 1,000 concurrent connections. Another benchmark running with 512 concurrent connections, showed that NGINX is about two times faster and consumed a bit less memory (4%). […]
Apache, Nginx – blog에 오신것을 환영합니다
[…] Performance of Apache 2.4 with the event MPM compared to Nginx […]
NGINX Vs Apache (Comparison of Both inc Pros & Cons)
[…] to Apache 2.4 with the event MPM, running 1,000 concurrent connections, results showed NGINX to run 2.5 times faster than Apache. In terms of static content, NGINX is the clear choice for speed and […]
NGINX vs Apache: What sets them apart – Indian Server Hosting Blog
[…] Content: According to one Benchmarking report, Nginx is about 2.5 times faster as compared to Apache. The results were concluded based on 1,000 […]
Nginx vs Apache: Web Server Showdown - WP FixAll
[…] mpm goes a bit further in terms of optimization, but some tests show that it can’t outrun Nginx. Especially when we talk about static files, where Nginx […]
Nginx vs Apache: Which Web Server Is the Best? (2019 Edition)
[…] mpm goes a bit further in terms of optimization, but some tests show that it can’t outrun Nginx. Especially when we talk about static files, where Nginx […]
Which Web Server Is the Best? (2019 Edition) – WPSensors
[…] mpm goes a bit further in terms of optimization, but some tests show that it can’t outrun Nginx. Especially when we talk about static files, where Nginx […]
Nginx vs Apache: Confronto Entre Servidores Web (Edição de 2019)
[…] mpm vão um pouco mais além em termos de otimização, mas alguns testes mostram que não conseguem superar o Nginx. Especialmente quando se fala de arquivos […]
Nginx vs Apache: Lucha entre Servidores Web – José Moreno
[…] mpm de evento pueden ir un poco más allá cuando se trata de la optimización, pero algunas pruebas muestran que no pueden rebasar a Nginx. Especialmente cuando hablamos de archivos […]
Nginx vs Apache: Which Web Server Is the Best? (2019 Edition)
[…] mpm geht in Sachen Optimierung etwas weiter, aber einige Tests zeigen, dass es Nginx nicht überlegen ist. Besonders wenn es um statische Dateien geht, wo Nginx […]
網站效能改善方案, 啟用 Apache Event MPM – 月半人的家
[…] Event MPM 後,個人是感覺有快一些些。參考網路上的比較,Event MPM 大概可以比 Prefork 快上 […]
Nginx vs Apache: Which Web Server Is the Best? (2019 Edition) - Top Of The World Services
[…] mpm goes a bit further in terms of optimization, but some tests show that it can’t outrun Nginx. Especially when we talk about static files, where Nginx […]
Nginx contre Apache : Comparaison des serveurs Web (Édition 2020)
[…] mpm va un peu plus loin en termes d’optimisation, mais certains tests montrent qu’il ne peut pas dépasser Nginx. Surtout quand on parle de fichiers statiques, […]
Web服务器王者之争:该选择Apache还是Nginx – 闪电博
[…] 事件mpm在优化方面走得更远,但是一些测试表明它不能超过Nginx。尤其是当我们谈论静态文件时,Nginx的服务量是Apache的两倍。 […]
Nginx vs Apache: Web Server Showdown - TECHEVANGELISTSEO | SEO Tips With YouTube Videos Infographic And How To
[…] mpm goes a bit further in terms of optimization, but some tests show that it can’t outrun Nginx. Especially when we talk about static files, where Nginx […]
NGINX vs Apache: What sets them apart – Indian Server Hosting
[…] Content:According to one Benchmarking report, Nginx is about 2.5 times faster as compared to Apache. The results were concluded based on 1,000 […]
Nginx vs Apache: Web Server Showdown – Tenten Content curator
[…] mpm goes a bit further in terms of optimization, but some tests show that it can’t outrun Nginx. Especially when we talk about static files, where Nginx […]