Online Support

Apache web server vs nginx

Apache web server vs nginx

Apache web server is one of the oldest around. One reason might be most people learn to use it when they first start to learn about web servers. It powers 48% of the world’s web servers, Microsoft has 27%, and nginx 14%.

Apache and nginx are both easy to install and work with. With either one you just copy html files into the default document directory then you can view them on the web. So you can install them, copy HTML documents to the document root, and be up and running without having to understand anything beyond that.

Even though we just said they are “easy” to work with, getting the syntax just right and making things work can be frustrating for people who do not have the patience to dig into the rather lengthy documentation.

Big hosting companies Apache and nginx hold multiple customers. Both web servers support these. These are called virtual domains. Multiple ones of those can be configured inside each web server.

Performance and Multithreaded Processing

Nginx, when it first came out 15 years ago, had a superior method for handling large volumes of traffic. That goal was one reason the developers wrote nginx saying, “… time is showing that the architectural decisions made when the code [Apache] was first laid down are becoming limiting factors in its suitability for modern web demands..”

To illustrate one example, when a browser makes a connection to an Apache web server it opens a TCP socket. There is a fixed number of those, set by the administrator and limited by the memory on the machine, type of traffic, etc. Apache spawns a new http process for each connection. That can cause the machine to run out of memory. When there is no more memory it pages that to disk. That is called “thrashing,” which does not sound good. What this means is that web requests can be stuck in TIME_WAIT status when the server gets too busy.

Nginx instead spawns a fixed number of processes. Each of these are multi-threaded. That means when one browser requests one item it goes and does that, then it works on another request while that browser is sitting idle, then it comes back to the other again when the user clicks on the web page, all in the same process

Apache has added that feature to their web server with a multithreaded (worker) and pre-forked (multithreaded) modules. The ability to expand both open source servers with open source modules is one reason they are so popular and well-suited.

Nginx Config

On Linux, the default location of the nginx config file is /etc/nginx/nginx.conf. You can think of that as the main config file. Anything below that is specific to domains below the main one or processes running on other ports, like another kind of server. It also loads configurations for other sites (virtual hosts) using the wildcard character * to include, for example, whatever sites it finds in /etc/nginx/conf.d/*.conf and /etc/nginx/sites-enabled/*.

In the config file you tell nginx where the document root is located and what port to listen on using these directives:

root /var/www/html;

listen 80 default_server;
listen [::]:80 default_server;

Apache Config

On Ubuntu, you install Apache like this:

sudo apt install apache2

People generally call it Apache2, since it was a major rewrite of the version that came before it.

It’s main configuration file is located here /etc/apache2/apache2.conf. And the default document root /var/www. Virtual hosts, reverse proxy servers, and other sites and configurations are loaded from /etc/apache2/conf-enabled and /etc/apache2/sites-enabled.

Both are relatively easy to configure with lots of examples on the internet.

Apache and Nginx Modules

With Apache, you copy modules to the folder mods-enabled and they are ready-to-used. With nginx, you have to recompile the web server from source code and include that module.

Modules provide a link to other functions and resource. For example there are modules to connect to LDAP, connect to a database, provide additional logging info to help with cybersecurity, give advanced authentication, and there are different modules to attach PHP, Python, and other code to the web page. Of course, code is what makes the web page interactive and do something useful.

Nginx and Apache Together

If you are of the mindset that Nginx is better at handling heavy volume sites, then you can deploy Nginx as a proxy in front of a farm of Apache web server. That configuration is also called a reverse proxy. It means Nginx handles the initial connection from the browser. Then it hands the connection off to whatever server is best suited to handle that. So one server might handle static unencrypted pages, another run WordPress, and another be running server-side JavaScript.

Whether or not Nginx is really faster is a controversial point. And it is a moot point is many situations. How fast your system runs depends on a lot more factors than just the web server.

But one analyst puts it this way “Apache is like Microsoft Word. It has a million options but you only need six. NGINX does those six things, and it does five of them 50 times faster than Apache.”

Maybe so. Maybe not. One thing you can say about Apache is it has a historical bias that inflates its market share. This is because Apache is usually where programmers start when they first learn Java programming. And Java programming is one of the first languages people usually learn. And it is also popular to deploy the so-called LAMP stack: Linux, Apache, MySQL, and PHP/Python/Perl. That too is old fashioned as PHP and Perl are far behind Python and other languages in ease of use and functionality. No one should be using PHP or Perl anymore as there are far better alternatives, yet many do.

0800 817 4727