Home
Blog
Apache and Nginx web servers
09 March 2023
5 minutes
Leo Matyshkin
software developer
Apache and Nginx web servers

Apache and Nginx web servers

Apache and Nginx web servers

No website can operate without a web server. The web server is a software program running on a physical server (remote computer). It accepts client requests and responds with an HTML page, image, file, or some kind of media flow: music, video or stream. The client can be a web browser or any software that tries to "talk" to our web server.

Client-server interaction

So the client sends a request, the web server processes it and, acting according to its settings, delegates it to one of its internal applications. The application takes the required action and comes up with a result, which the server promptly returns to client.

Simplified flow chart of a browser-web server connection processing interaction, leading to the receipt of an HTML page. The web server initiates a PHP process for dynamic page generation, which involves grabbing data from the database.
Simplified flow chart of a browser-web server connection processing interaction, leading to the receipt of an HTML page. The web server initiates a PHP process for dynamic page generation, which involves grabbing data from the database

Generally speaking, a web server is your classic go-between, dealing with clients in a standardized manner and so that they never need to know anything about the inner works of a website. When a client request cannot be fulfilled, the web server will have to let the client know so that the client may figure out where the problem is and trouble-shoot it if feasible. For example, the client may need to log on to the website in order to confirm their access rights or correct antypo to make sure the server never returns error 404 again.

Web servers

Clients can be all different, but the role of a go-between according to our definition above will usually be performed by one of these two web servers: Apache or Nginx. Both programs have an open source code, are accompanied by elaborate documentation and supported by influential communities, but they came about at different times, and, accordingly, were designed to meet different web server requirements. Let us explain.

Apache

Apache came on the scene in the 1990s as a web content delivery solution for a fast-growing internet. Apache architecture consists of a core and some modules linked with it: the core takes requests, and the modules perform the requested functions. Thanks to those modules, the web server can do a lot more than simply processing HTTP connections, performing functions such as:

  • Load balancing, or sharing the traffic between local network servers in order to apportion responsibilities and make the system more fail-proof. Some examples of such modules in Apache are: mod_status and mod_proxy_balancer;
  • Data caching: responses are stored for a preset length of time so that when a repeat request arrives backend requests may not be so numerous. PageSpeed, one such module in Apache, is used to compress and cache data.
  • Support of different protocols and request types: not only HTTP, but also FTP (mod_ftp module), email protocols and others;
  • Support of different operating systems and programming languages.

Apache is a dependable web server that is relatively easy to configure for small applications. However, as requests multiply, Apache's centralized architecture will cause the web server to slow down.

Nginx

Nginx, a Russian developed solution, came about in the mid-2000s. It was purported to address Apache's performance limitations by realizing a different approach to web server architecture.

By default, Apache creates a single program, sequence for a single incoming connection. It will not proceed to the next connection before it finalizes the service of the prior one. Nginx launches multiple parallel processes, each capable of handling thousands of HTTP connections.

Because the Nginx architecture is asynchronous, the traffic load is easier to predict in terms of resource allocation and latency. As a result, the server can be easily scale-adjusted even when running on most basic hardware. All performance tests have shown Nginx to either match or surpass Apache in speed while taking up less memory. Nginx is typically the preferred choice for websites supporting the more recent web technology: HTTP/2 and IPv6.

Apache vs Nginx

The key difference between the two web servers lies in the way they service multiple connections. Apache deploys several solutions with the aid of three Multi-Processing Modules (MPM). It's up to the developer which module to engage for the task at hand:

  • mpm_prefork launches one process per request, with a single processing flow in there, so the process can only handle one connection at a time;
  • mpm_worker launches several data processing flows within each process. It takes less program resources to switch between flows than it does between processes. The freed up flow takes on a new connection immediately;
  • mpm_event does a job identical to that of mpm_worker, except that it's optimized to handle keep-alive connections. This type of connection sees multiple request-response pairs sent out without any new connections opening. The module will allocate separate flows to different connection types, preventing the server getting bogged down in keep-alive processing.

If you are not familiar with the concepts of process and flow, you may find the description of Apache's MPM modules difficult to grasp. Incidentally, this is one of the reasons why beginner developers go for Nginx. Built on a foundation of asynchronous algos to begin with, Nginx copes excellently with a huge number of connections even without the use of any modules, and the developer does not have to follow up with any additional steps.

It takes a single configuration file, nginx.conf, to configure Nginx. To do the same for Apache, configuration files have to be edited on several levels of the operating system, including the special .htaccess files that govern rights at catalog level.

It may seem that Apache is inferior to Nginx across the board. This may be so but at least Apache does a pretty good job serving the purpose it was designed for, namely web content delivery, and it supports numerous programming languages to boot. No wonder the two web server systems are often used in tandem.

Apache + Nginx

One important reason why Nginx market share has steadily increased is that it can operate as a reverse proxy, transmitting client requests from the external network to the local network servers, including Apache web servers. This is a popular system administration matrix wherein Nginx takes care of all the routine work in caching statistic files and distributing requests, while Apache or another web server launches the processes to return the dynamically generated web page content.

Specimen standard server architecture with a web app: client interacts with proxy server (Nginx) and the latter can return static files (e.g. images or pdf files); for dynamic content, however, the Apache-based web servers have to be queried, as per configuration
Specimen standard server architecture with a web app: client interacts with proxy server (Nginx) and the latter can return static files (e.g. images or pdf files); for dynamic content, however, the Apache-based web servers have to be queried, as per configuration

A few alternatives

Although Apache and Nginx serve at least fifty percent of all websites between them, alternatives do exist. IIS and LiteSpeed are usually rated, respectively, as the third and fourth runners-up among the more popular web servers.

  • IIS is Microsoft's web server developed for its own operating systems. Disabled by default in Windows, it can be enabled any time. IIS is used in Windows Server projects and web applications involving the ASP.NET technology.
  • LiteSpeed, a modern alternative to Apache, is compatible with its configuration files. It has better productivity, engages fewer resources, and is good at caching data. LiteSpeed is a proprietary server, but it has an open-source version called OpenLiteSpeed.

How to try a web server in action

The easiest way to try your hand at running a web server is to use one with an ispmanager panel. The panel will accept the installation of any popular open-source web server: Apache, Nginx or OpenLiteSpeed Once the server is in place, you can run an application on it to test it in real time.

Try ispmanager

Subscribe to news
Subscribe to the ispmanager newsletter and get the best content every week