Apache Vs Nginx Performance
If you’re ever looking to improve the performance of your PHP websites, the likelihood is that someone has recommended the move from Apache to Nginx web server. As part of my own website optimisation, I wanted to understand whether the performance improvements were genuine, or just marketing patter.
So, I launched two web servers, one a LEMP stack (Nginx) and one a LAMP stack (Apache). Both websites were configured with the same caching, compression etc.. as one another and both sites were identical. The first step was to run some simple load speed tests to understand load times of each – the tool I use provided a median of the tests run.
As you can see, the difference is a couple tenths of a second – really nothing to write home about. In fact, taking the median run, the Apache server actually had a better time to first byte & full load time. But, taking averages of data can sometimes be very misleading and in this case, it proved to be true.
I ran 25 speed tests on each of the sites and plotted the results below. I chose a 3.5 second load time as something I’d deem acceptable and have coloured the chart to show whether those times that the website loaded too slowly (highlighted in red).
At first glance, you may not think this looks too bad – they seem to perform fairly comparably. However, when you look at the number of times each web server breached my 3.5 second threshold, things start to become clearer. The Nginx server only breached the threshold 6 times compared with the twelve times that Apache breached the threshold. So, that is to say, in my test, Nginx was more consistent. Obviously 25 runs isn’t a huge number, but it’s enough to draw some initial conclusions.
I then expanded the test to a total of 40 full page loads. Now the picture becomes far clearer:
- Nginx did not exceed 4 seconds for any load, while Apache did several times
- Apache breached my 3.5 second threshold in 50% of all loads, Nginx only in 22%
- While both are fairly erratic, Nginx was more consistent
- Nginx averaged 3.31 seconds while Apache averaged 3.52 seconds load times
Overall, there isn’t a huge amount in it but if you’re chasing millisecond improvement to your load times, Nginx is the one to choose.