Nginx vs Apache – which is the best web server?
NGINX vs Apache – which server is superior? There was a period when Apache held the lion’s share of all business for web servers, running on more than half the world’s server boxes. But that advantage has now been eroded to nearly a third, and is still falling.
Concurrently, Microsoft’s IIS web server has consistently achieved similar popularity. Sitting behind them both, holding on to 16% of all websites is NGINX (sounds like “engine-x”). Slowly creeping up behind its two rivals at a speed of roughly 1% of total business per year. So the other two may be looking over their shoulder at the upstart fairly soon.
Why compare NGINX and Apache?
With IIS approaching in the rear-view mirror like this, we thought it made sense to ask the question of which comes out on top: NGINX vs Apache? Because they’re both equally close to the pinnacle of the popularity chart as web servers. We want to see how they measure up in 6 crucial areas – performance for static versus dynamic content, OS support, security, flexibility, documentation, and support.
Apache’s .htaccess file gives it an edge for those in need of shared hosting, while NGINX has the upper hand with dynamic content and more elaborate features. Hence why users of VPS and dedicated hosting sing its praises. We’ll consider them feature-by-feature and suggest which one might be better for handling whichever web app project you’ll be attacking next.
Current numbers for NGINX and Apache
It’s worth pointing out that some web analysts think the question of Apache versus NGINX is important because they actually have more business than suggested by the figures shown above. And they actually consider IIS to be much less of a competitor.
Market share can be calculated in different ways, so these figures are a rough guide, but the overall trends are clear. If we only consider the sites with the highest traffic, Apache weighs in with a frequently dropping 46% of all sales while NGINX is in second place with 24% of the market and 3% year-on-year growth. So, now the NGINX versus Apache comparison seems even more relevant.
NGINX came about because of a grueling test, where a server has to reach 10,000 client connections all at the same time. It uses a non-synchronized, event-driven architecture to cope with this prodigious load. And its design means that it can take high loads and loads that vary wildly all in its stride, leveraging predictions for RAM usage, CPU usage, and latency to achieve greater efficiently.
NGINX approaches event models a little differently from Apache because it doesn’t set up extra worker processes for each connection. Usually, NGINX is best configured to run one worker process for each CPU so that it can maximise the efficiency of hardware.
It also offers numerous features which make it well-suited to varied roles. Such as a reverse proxy server for the HTTP, HTTPS, SMTP, POP3, and IMAP protocols, a load balancer and an HTTP cache. Plus, a frontend proxy for Apache and other web servers, marrying the flexibility of Apache with the excellent static content handling abilities of NGINX.
NGINX offers support for FastCGI and SCGI handlers to serve dynamic content scripts like PHP and Python. It uses the LEMP stack: a variation of LAMP using the phonetic spelling of NGINX (Linux, “En-juhn-ex,” MySQL, PHP).
LAMP (Linux, Apache, MySQL, PHP) is a popular web stack these days, and Apache is the web server component of it. Although there are various other web stack components out their (e.g., NodeJS, rich clients JS frameworks, various cloud services, etc.), LAMP remains a favorite for many.
You can add 60 official modules and various unofficial modules in to give Apache web server a wide variety of features. Apache has come up with a few different approaches to churning through web requests. It’s had to. In recent years, ballooning page sizes and a potential avalanche of concurrent web have been enough to swamp the resources of unready servers.
All this extra work has inspired a rethink about how best to cope. The Apache request processing methodology can be set up in three different ways. Here’s an explanation of the three main Multi-Processing Modules (MPMs):
- Processes: this is the original “pre-fork” method; it doesn’t scale well with large numbers of concurrent connections, because it uses high amounts of RAM and may even refuse connections when loads are high. Smaller domains won’t experience this, but larger ones probably will.
- Events: This resembles the worker model, but it creates one listener thread which listens for connections and passes them to a worker thread for processing. This MPM takes care of long-running connections a lot more efficiently on a single thread (KeepAlive handling). Since Apache 2.4, the event model has been declared stable and is now also the default setting if the operating system can support it. You can also try compile-time and run-time options to improve Apache performance.
- Worker model: one control process is established, and this then creates additional sub-processes. Each one of these then spawns a set number of threads, along with a listener thread. The listener thread listens for connections and passes them to a thread for processing when they arrive. This model does scale more effectively than the pre-fork method, but it can still encounter scaling issues with high-traffic sites, as the single control process creates a bottleneck.
Apache vs NGINX – detailed comparison
Overall, a comparison of how both web servers fare across several areas of work will show that NGINX versus Apache is a close battle. But depending on your needs, one may do a better job than the other. Don’t worry if that sounds a little uncertain, we’ll summarize at the end.
Let’s compare them in both static and dynamic content serving roles.
NGINX performs 2.5 times faster than Apache according to a benchmark test running up to 1,000 simultaneous connections. Another benchmark running with 512 simultaneous connections, showed that NGINX is about twice as fast and consumed slightly less memory (4%). Without doubt, NGINX has the advantage over Apache with static content. So if you need to serve lots of concurrent static content, NGINX wins in this segment of the NGINX versus Apache contest.
A 2015 benchmark which compared dynamic content serving by Apache and NGINX revealed that the Apache event MPM, when linked with the PHP-FPM module, can manage about the same concurrency as can NGINX with PHP. Another web server performance comparison showed similar results. This result is down to almost all of the request processing time being spent in the PHP runtime environment instead of the core part of the web server. The PHP runtime environment is fairly similar between the two web servers.
Speedemy’s web server benchmarking found a similar situation when using both Apache and NGINX to serve dynamic content. In terms of PHP (and probably a bunch of other languages as well), the dynamic page server performance is virtually the same with a proper Apache module setup (PHP-FPM + FastCGI). If you need to get more speed from dynamic pages, you have a few options: add a Varnish or Memcached caching layer, switch to a faster PHP runtime (e.g., HHVM), do load balancing, or invest in extra hardware.
Unfortunately, doing a better job of serving static pages doesn’t magically translate into NGINX being just as efficient with dynamic pages too. In fact, both web servers offer more or less similar performance in this area.
Operating System Support
Apache works with all kinds of Unix-like systems (such as Linux or BSD) and fully supports Microsoft Windows. NGINX runs on a few of them too, and has some support for Windows, although performance is not as strong here. Apache has the edge with this one.
Both candidates have an excellent record of achievement of security for their C-based code base, but the NGINX code base is admittedly not as big. Which is a considerable bonus from a security point of view. Plus, vulnerability reporting is available for Apache 2.2 and 2.4.
NGINX also keeps a list of recent security advisories. And Apache offers configuration tips for handling DDoS attacks, including the mod evasive module for dealing with HTTP DoS, DDoS, or brute force attacks. In addition it offers resources for coping with DDoS threats on the NGINX blog.
The web server can be customized by writing modules. Apache has offered dynamic module loading for ages, so all Apache modules support this. However, with NGINX it’s a different story.
NGINX only got support for dynamic module loading at the start of 2016. Before that, the admin had to compile the modules into the NGINX binary. Most modules don’t support dynamic loading yet, but this will properly change in time. Apache is obviously ahead here.
Apache and NGINX – modules
Both Apache and NGINX offer a lot of modules which offer specific features.
- Official Modules (find them in the Modules section of the Apache documentation)
- Wikipedia module list
At the moment, there doesn’t seem to be an up-to-date list of all third-party modules. NGINX versus Apache is a close call because both offer well-featured and ever-expanding feature sets, but each web server excels at things that the other can’t match.
- Official Modules (find them the Modules Reference section of the NGINX documentation)
- Third-Party Modules
It’s difficult to conclude whether Apache or NGINX excels here, but the majority of the necessary core module functionality (e.g., proxy-ing, caching, load-balancing, etc.) is present with both web servers. NGINX appears better at acting as a reverse proxy for TCP and email (SMTP, IMAP, POP3) connections. As for media streaming modules, the commercial NGINX Plus version looks to be ahead.
NGINX doesn’t support an equivalent of the Apache’s .htaccess file. .htaccess files allow users to bypass system settings for any given directory; but for optimum operation, .htaccess directives should be included in the main configuration file(s) if possible. This will not be possible with shared hosting situations, but for shared hosting users it offers greater flexibility.
The Apache vs NGINX question is a tie with documentation offerings, as both are excellent, including the NGINX wiki. NGINX provides on-location and online training sessions that cover many NGINX topics — and they even offer exam certifications.
Support of Apache vs NGINX
Apache offers user support via mailing lists, IRC, and Stack Overflow. Paid Apache support comes via third-party companies like OpenLogic, but there isn’t an official list of these to select from. NGINX community support mirrors Apache’s, and there is also a forum. The company behind NGINX sells a commercial product called NGINX Plus, which supports a set of additional features that cover load-balancing, media streaming, and monitoring.
So who wins: Apache vs NGINX?
The current iterations of our pair of contenders compete well with each other in most situations. NGINX is better with static content, but offers little extra performance for dynamic content. Meanwhile, NGINX stands out because of some of its more advanced features (media streaming, reverse proxying for non-HTTP protocols), along with its commercial support and training.
Shared hosting users might find Apache’s .htaccess file more convenient, and Apache handles lots of dynamic modules. Something which NGINX only added recently. And NGINX is popular for VPS hosting, dedicated hosting, or cluster containers.
Owners of websites that attract a lot of visitors and which will be serving lots of static content and/or streaming media will veer towards NGINX. But either web server will do a perfectly acceptable job in most other website use situations. Regardless of which web server you choose, you’re going to need a reliable Linux hosting provider to provide a home for your shared or virtual servers.
How useful was this post?
Click on a heart to rate it!
Average rating 4.5 / 5. Vote count: 115
Oh no, sorry about that!
Let us know how we can do better below
Thanks for your feedback!