nginx vs. Apache HTTP Server

Get help choosing one of these Get news updates about these tools



Nginx or Apache - Help me decide


Nginx & Apache are the two most used web servers on the internet. Together, they own over 60% of the total market share.

But which one should you use? That's exactly the purpose of this article.

Performance

The first thing you should think about when choosing a critical piece of software like a web server is its performance characteristics.

Not only how many requests they can serve per second, but also how they behave under heavy load & what does the resource usage (RAM, CPU) look like.

This our testing setup:

  • Ubuntu 18.04
  • Apache 2.4.29 (mpm_event)
  • Nginx 1.14.0
  • Default settings
  • 1GB ram
  • 1 CPU

As a benchmarking tool we're going to use wrk with the following settings:

  • -d 60 (duration of the test)
  • -c 40 (concurrency)
  • --latency (latency distribution)

Our target URL returns a small HTML file with no server language involved.

Running this test we get the following results (requests/second):

Apache Nginx
670.53 660.15

It seems like Nginx & Apache are about the same speed!

But what about resource usage?

While running this test, Apache averaged a CPU usage of 20% & 18MB RAM:

Nginx CPU usage averaged 12% & only 8MB RAM:

While this benchmark might not be representative of all real-world use cases & you should consider running your own benchmarks for your particular setup, it can give you a general idea of how these servers perform.

In conclusion, if your biggest concern is performance & efficient use of your resources you should consider using Nginx.

Extensibility

Both servers come with a good set of core features which should be enough for most people...

...but sometimes you need that little extra.

That's why you can extend both servers using modules.

Modules can be compiled into the main server binary, or they can be added as dynamic modules that can be installed separately from the binary.

Dynamic modules are more flexible because they can be updated on their own, and you can add new modules without having to recompile your server.

Most Apache modules are dynamic, but Nginx recently (version 1.9.11, released in 2016) started supporting this feature.

Now:

Let's take a look at some useful modules for both servers.

  • modsecurity: Available for Apache. This module adds a Web Application Firewall (WAF) in front of your application. There is a Nginx version, but it seems not maintained, you can use Naxsi instead.
  • page_speed: Available for Apache & Nginx. This module can optimize images on the fly & add other optimizations to improve page loading times.
  • ngx_mruby / mod_ruby: Available for Apache & Nginx. This module allows you to use the Ruby programming language to process requests & make decisions to redirect to another page, return some file contents, etc. The nginx version is well-maintained & faster.

Many popular modules are available for both servers, so module availability may not be a factor when deciding what server to use.

For a complete list of available modules you can go here:

Installing a new module:

Adding a new module to Apache is easier than adding new modules to Nginx.

You can install Apache modules from your package repository, then use the a2enmod command to enable it & restart your server.

That's it.

Nginx may require you to compile from source to install some modules, since dynamic modules must be built against the same version of Nginx that you're running.

However, you can do this on a non-production server, then copy the dynamic module (.so file) into production.

If you think you'll need to be changing modules frequently this is something to consider, but that's not often the case.

Popularity

The popularity of a piece of open-source software matters because the most popular usually get the most attention. This can translate into better documentation, the ability to find solutions to specific problems & how well maintained is the software itself.

So exactly how popular are Apache & Nginx?

According to the 2018 August Web Server Survey conducted by netcraft.com, these are the stats for active sites:

  • Apache 38.68% (-0.62 from previous month)
  • nginx 22.67% (+0.11 from previous month)

The rate of change is pretty small, but that's to be expected from an established technology.

Looking at the big picture it looks like Apache has been losing a lot of ground over the last 7 years. In 2011, Apache owned 60% of the active sites market share, while Nginx (released in October 2004) only had 10% by that same year.

If the trend continues, Nginx is going to overtake Apache as the "king of web servers" in a few years.

Maybe earlier than we expect.

Something to keep in mind when making your decision.

Most Common Uses

Let's have a look at the most common uses for Apache & Nginx, this will help you decide if your use case matches with what the server does best naturally.

Apache strengths:

  • Runs PHP applications (like Wordpress) without external software, just install mod_php if it isn't already part of the default install for your distribution.
  • Works great in a shared environment (like a hosting provider) because it supports directory-based configuration with .htaccess files.

Nginx strengths:

  • Serves static assets very efficiently thanks to its event-driven approach to handling requests.
  • Is a great proxy & cache layer for the same reason.
  • You can easily implement custom logic with modules like ngx_lua & ngx_mruby. Cloudflare makes great use of this for their custom WAF (Web Application Firewall).

Final Considerations

A few more things to consider before making your final decision:

  • Nginx offers an enterprise-grade solution in the form of Nginx PLUS. This adds professional support & a few extra capabilities (like monitoring) which may be important to you if you are running a big operation.
  • Apache & Nginx can be used together, with Nginx proxying non-static asset request to Apache. This can add significant complexity to your setup, but it's something to consider if you want to use features from both.

We hope you found this comparison useful!

nginx vs Apache HTTP Server - Comparison Table

Favorites

592

Favorites

135

Hacker News, Reddit, Stack Overflow Stats

  • 2.31K
  • 751
  • 33.7K
  • -
  • 294
  • 0

GitHub Stats

Description

What is nginx?

nginx [engine x] is an HTTP and reverse proxy server, as well as a mail proxy server, written by Igor Sysoev. According to Netcraft nginx served or proxied 30.46% of the top million busiest sites in Jan 2018.

What is Apache HTTP Server?

The Apache HTTP Server is a powerful and flexible HTTP/1.1 compliant web server. Originally designed as a replacement for the NCSA HTTP Server, it has grown to be the most popular web server on the Internet.

Pros

Why do developers choose nginx?
  • Why do you like nginx?

    Why do developers choose Apache HTTP Server?
  • Why do you like Apache HTTP Server?

    Cons

    What are the cons of using nginx?
    No Cons submitted yet for nginx
    Downsides of nginx?

    What are the cons of using Apache HTTP Server?
    No Cons submitted yet for Apache HTTP Server
    Downsides of Apache HTTP Server?

    Companies

    What companies use nginx?
    8064 companies on StackShare use nginx
    What companies use Apache HTTP Server?
    6036 companies on StackShare use Apache HTTP Server

    Integrations

    What tools integrate with nginx?
    15 tools on StackShare integrate with nginx
    What tools integrate with Apache HTTP Server?
    8 tools on StackShare integrate with Apache HTTP Server

    What are some alternatives to nginx and Apache HTTP Server?

    • Microsoft IIS - Internet Information Services is a web server for Microsoft Windows
    • Apache Tomcat - An open source software implementation of the Java Servlet and JavaServer Pages technologies
    • Passenger - A fast and robust web server and application server for Ruby, Python and Node.js
    • Unicorn - Rack HTTP server for fast clients and Unix

    See all alternatives to nginx

    Latest News

    Unit 0.6 Beta Release Available Now
    Dynamic A/B Testing with NGINX Plus
    Using ModSecurity to Virtual Patch Apache Struts CVE...


    Interest Over Time


    Get help choosing one of these