Nginx vs Apache – Which is the Best Web Server?

Nginx vs Apache – which is the best web server?

NGINX vs Apache – which server is superior? There was a period, somewhere between the end of the last century and the tail end of the noughties 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 it’s 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”). It’s 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.

With IIS approaching in the rear-view mirror like this, we thought it made sense to ask the question of Apache versus NGINX – which comes out on top? 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 (which is 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.

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 at 46 percent of all sales, but that’s a figure that’s falling by about 4 percent each year; and by the same reckoning, NGINX is in second place with 24 percent of the market, with 3% year-on-year growth. So, now the ENGINX versus Apache comparison seems even more relevant.

NGINX Outline

NGINX came about because of a grueling test, where a server has to reach 10,000 client connections all at the same time. NGINX uses a non-synchronized, event-driven architecture to cope with this prodigious load. 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, in that it does not 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.

NGINX 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, 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).

Apache Outline

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.

60 official modules and various unofficial modules can be added 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 is 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, I’ll summarize at the end.

Performance

Let’s compare them in both static and dynamic content serving roles.

Static Content

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.

Dynamic Content

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.

Security

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.
Vulnerability reporting is available for Apache 2.2 and 2.4. NGINX also keeps a list of recent security advisories. Apache offers configuration tips for handling DDoS attacks, and also 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.

Flexibility

The web server can be customized by writing modules. Apache has offered dynamic module loading for ages, so all Apache modules support this.
With NGINX it’s a different story though. 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.

Apache Modules:

  • 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.

NGINX modules:

  • 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.

Apache .htaccess

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.

Documentation

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.

Apache vs NGINX – conclusion

The current iterations of our pair of contenders compete well with each other in the majority of situations. NGINX is better with static content, but offers little extra performance for dynamic content. 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 that NGINX only added recently. 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 (or use a mix of Apache and NGINX). Either web server will do a perfectly acceptable job in most other website use situations. Now, 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, and as luck would have it, we’ve compiled a list of the best ones for you.

About

Elvis Plesky
Our fun and curious team mascot's always plugged into the latest trends. He's here to share his knowledge and help you solve your tech problems.

Leave a Comment

Start typing and press Enter to search