Introduction
Web servers have been a fundamental part of the internet ever since the beginnings of the World Wide Web. Web server software receives requests from remote clients and serves the clients with web pages stored on hardware, such as dedicated servers.
In this comparison article, you will read about the differences between the two most popular open-source web servers today – Apache and Nginx.
About Apache Server
The Apache HTTP Server (or Apache for short) is an open-source web server software developed by the Apache Software Foundation. It was initially designed as a process-based web server, but as of version 2.0 and the introduction of Multi-Processing Modules, it can be configured as both a process-based and threaded server. The modular architecture of the server also allows for features such as modular protocol handling.
Another essential feature of Apache’s architecture are filters, which allow modules to interact with the content created by other modules. This interaction includes encryption, virus scans, and compression of both static and dynamic content.
Note: The Apache HTTP Server works best on Linux. Learn how to install Apache on CentOS. For Ubuntu, read How to Install Apache Web Server on Ubuntu. For basic Apache web server management, learn how to start, stop, and restart Apache.
About Nginx Server
Nginx (pronounced engine-x) is an open-source web server developed by Nginx, part of F5, Inc.
As an HTTP server, Nginx utilizes a highly scalable event-driven (asynchronous) architecture allowing for high performance and a small memory footprint.
Nginx can also function as a reverse proxy, load balancer, mail proxy, and HTTP cache. In some use cases, Nginx also serves as a web accelerator or an SSL/TLS terminator.
Note: Like Apache, Nginx is also most compatible with Linux. To install Nginx on CentOS, refer to How to Install Nginx on CentOS. For installing it on Ubuntu, read How to Install Nginx on Ubuntu. For basic Nginx management instructions, learn how to start, stop and restart Nginx.
Apache vs Nginx: A Quick Overview
Below is a quick overview of the main points of comparison between Apache and Nginx.
Categories | Apache | Nginx |
---|---|---|
Cost | Available free of charge | The main product, OSS Nginx, is available free of charge. Nginx Plus is a paid option, featuring support and additional features. |
Licensing | Apache License 2.0 | 2-Clause BSD License |
Written in | C, XML | C |
Operating systems | Linux, Microsoft Windows, other Unix-like systems, OpenVMS | Linux, macOS, Microsoft Windows, other Unix-like systems, HP-UX, IBM AIX |
Architecture | Modular, process-based/threaded | Modular, Event-driven |
Configuration | Distributed | Centralized |
Interpretation | Primarily file-based | Primarily URI based |
Features | - Multi-processing modules - Per-directory configuration support - Reverse proxy with caching - Load balancing IPv6 compatible - Supports HTTP/2 - XML support FTP support (with a separate module) | - Serving static and index files - Open file descriptor cache - Accelerated reverse proxying with caching - Load balancing and fault tolerance - IPv6 supported, not enabled by default - Supports HTTP/2 - Mail proxying - HTTP caching |
Support | - Apache HTTP Server Documentation - Apache HTTP Server Users List - IRC - Stack Overflow - Commercial support by 3rd party providers | - Community operated mailing list - IRC - Stack Overflow - Paid official support for Nginx Plus |
Architecture
The Apache HTTP Server features a small server core and several modules. The modules are either statically compiled or loaded dynamically.
At boot time, Apache starts multiple server processes. The purpose of those processes is to share the workload. The main process is the parent process, while others are child processes. Each child process creates a pre-determined number of server threads that handle incoming requests.
To optimize the server for the operating system, Apache uses the Multi-Processing Module (MPM). The purpose of this module is to bind to network ports, accept requests, and manage request handling by assigning child processes to perform these tasks. Apache installs the MPM best suited for the environment depending on the detected OS and its capabilities.
The Apache Portable Runtime libraries (APR) are another important part of an Apache server. APRs provide a cross-platform OS layer. This layer serves as a universal API for developers, eliminating the need to code around platform-specific problems or features.
Nginx features an event-driven architecture that allows for easy scaling on modern hardware.
Like Apache, Nginx has a primary, master process. The purpose of this process is to control port binding and configuration reading. However, unlike Apache, the processes created by the master process are designed based on a predictable process model, whose main goal is to make the best use of hardware resources.
- The cache loader is a process that runs at start-up and loads the disk-based cache into memory.
- The cache manager makes sure the disk cache entries are within the defined size limits.
- The worker processes perform read and write operations and handle network connections.
Worker processes start by listening and waiting for events. Whenever a new incoming connection is requested, an event is initiated. Upon establishing the connection, Nginx creates a file descriptor that takes up only a small amount of worker process memory. This feature makes Nginx significantly more scalable than Apache, which uses the process-based approach, where each separate connection consumes a large amount of resources.
Modules
Both Apache and Nginx use module-based approaches. However, the implementation is different.
Apache features dynamically loaded modules that can be utilized whenever there is a need for them. The server supports many different modules, both official and 3rd party. This makes Apache a very customizable platform that users can tailor to suit their needs.
Nginx modules need to be integrated into the core and cannot be dynamically loaded. To include non-standard modules, users must compile their server from the source. While this lack of flexibility may seem limiting, it also means better security since allowing dynamic module integration poses security concerns.
Performance
By utilizing a variety of supported Multi-Processing Modules, Apache can run in three modes:
- Process-based mode.
- Hybrid process and thread mode.
- Hybrid-event mode.
The user can configure Apache in a way that suits their needs. Thus, server resources are used effectively for a particular use case.
Apache features a set of optimizations for increasing its throughput and scalability. Some of these optimizations are part of the default server configuration, and server administrators can configure the rest depending on their system’s specific needs. Performance tuning can solve many of Apache’s runtime and compile-time configuration issues.
Nginx is a web server created specifically to outperform Apache. It succeeds in doing so in several categories, such as connection time, the number of received requests per second, transfer rate, and time spent on processing a request.
Server administrators can further tune Nginx for performance by editing the server’s configuration to fit the system specification. Adjusting worker processes and connections, enabling Gzip compression, and caching for static files significantly improves Nginx performance.
Static vs Dynamic Content
Apache processes both static and dynamic content by using dynamically loaded modules and does not depend on external components.
While both servers show similar performance in dynamic content loading, Nginx uses much less memory and handles around four times more requests per second for serving static files. This is mainly because the interpreter for dynamic content is not an integral part of an Nginx server. Instead, the server passes all the dynamic web page requests to an external process, waits for the process to return the content, and then serves the content to the client. While this makes the dynamic page processing more complicated, it simplifies the process of serving static pages.
Distributed vs Centralized Configuration
Apache’s configuration is distributed. The server provides support for an .htaccess file to facilitate this type of configuration.
The .htaccess
file is a directory-level configuration file supported by several web servers, used to deal with website-access issues, such as URL redirection, URL shortening, access control (for different web pages and files), and more. The main benefit of this approach is that changes made in .htaccess
are immediately applied to the system. Altering the main configuration file requires a system restart to apply the changes.
Furthermore, on servers that host multiple websites, .htaccess
allows each of the users to make changes to their website’s configuration without altering the server’s main configuration file.
Nginx configuration is centralized and does not support .htaccess
. The reason for this is that using .htaccess
also has several disadvantages. An important one is performance loss – every time a server receives an HTTP request, it needs to check all the parent directories which can host an .htaccess
file, to see if they contain it. Another significant concern is security. Allowing non-privileged users to modify the server configuration can be unsafe if not implemented properly.
Since there is no directory level configuration in Nginx, users perform all configuration changes by writing directives into the main configuration file, nginx.conf
. The changes are applied after the server is reloaded.
Connection Handling Architecture
Apache handles client requests by utilizing its multi-processing modules. The main benefit of this is that connection handling architecture can be effortlessly replaced whenever needed. These modules are:
mpm_prefork_module
– The prefork module creates processes with a thread for each request that needs to be handled. Every child process can support one connection. While the number of created processes is larger than the number of requests, this module performs well. The modules main issue is that large request volumes negatively affect performance and significantly increase RAM consumption.mpm_worker_module
– The worker module creates child processes. Each child process manages multiple threads, with each thread supporting a connection. Since threads are more efficient than processes, this module is more performance-friendly than the prefork module.mpm_event_module
– The event module features a mechanism similar to the mechanism of the worker module. However, it also attempts to solve the ‘keep alive‘ problem – the fact that Apache, after completing the client’s first request, keeps the entire child process or thread waiting for further requests by the client. The event module deals with this problem by having a designated listener thread monitor the state of the sockets.
Nginx handles connections using an asynchronous, non-blocking, event-driven algorithm. Using a fast-looping function, the server’s worker processes can handle many connections at the same time by continuously searching for and processing events.
When a worker establishes a connection, the connection joins the event loop, where it undergoes asynchronous processing. When the connection closes, it exits the loop. This type of connection handling enables Nginx to scale and utilize resources in an economical way.
File vs URI-Based Interpretation
Apache primarily interprets requests as filesystem resources, using Directory
and File
blocks. The server starts with the DocumentRoot
and tries to locate the file using the part of the request following the host and port number.
Apache offers alternative ways of serving the request when the right match cannot be found by going through the file system:
Alias
maps URLs to filesystem locations. It allows the documents to be stored elsewhere than inDocumentRoot
.Redirect
maps an old URL into a new URL. The server asks the client to find the sought resource at a different location.Location
blocks allow for working with URIs.
Contrary to Apache, whose design primarily reflects a web server needs, Nginx architecture allows for the server to be used as a proxy. Therefore, Nginx mainly deals with URIs.
Nginx uses server and location blocks as primary configuration blocks:
- A server block is a subset of the configuration file which defines the virtual server for request handling. Since virtual servers can be assigned to handle specific connection types, server administrators can assign different blocks for different domain names, ports, and IP addresses.
- A location block is part of a server block that handles requests for various resources and URIs. It routes requests to the correct location in the file system.
Since multiple blocks can act as individual web server instances, Nginx has a system to find the best match for each request. This is performed using the listen
directive, which finds possible block matches, and the server_name
directive, which chooses the best match.
When Nginx works with static files, it maps requests to the filesystem. However, the server performs this only after selecting the server and location blocks and connecting the document root and the URI. The fact that Nginx does not deal with the filesystem until it is ready to serve the request is one reason why it does not feature an .htaccess
file equivalent.
Caching
Apache offers caching to improve a server’s performance. There are three types of caching:
- Three-state RFC2616 HTTP caching offers intelligent, HTTP-aware caching when dealing with proxied content or dynamic content stored locally.
- Two-state key/value shared object caching offers key/value-based shared object cache.
- Specialized file caching enables preloading files on start-up, improving access times for files that are frequently needed.
Nginx achieves faster load times and reduces server load by caching dynamic content for quick access. While caching helps speed up websites with high traffic and content volume, it is not recommended in other scenarios.
Basic caching in Nginx uses only two directives:
proxy_cache_path
to set the cache path and configuration andproxy_cache
to activate cache.
A useful feature of Nginx is the ability to configure the server to display cached website content when the server is down or busy.
Security
Apache and Nginx are both considered secure. Potential security risks stem mostly from a bad configuration. Hence, follow web server security best practices to enhance security:
For Apache:
- Adjust configuration to prevent DoS attacks. Some of the relevant directives for this are
RequestReadTimeout
,KeepAliveTimeout
, andTimeOut
. - Adjust permissions on
ServerRoot
directories. - Be careful with Server Side Includes (SSI).
- Be careful with CGI scripts.
- Protect system settings against
.htaccess
overrides by configuring the server configuration file. - Watch your logs.
- Keep the server up to date.
For Nginx:
- Lower the risk of potential vulnerability exploits by disabling unwanted modules.
- Disable the
server_tokens
directive, which makes your Nginx version publicly visible. - Disable unnecessary HTTP methods.
- Control resources and limits to prevent potential DoS attacks.
- Set up, configure, and monitor access and error logs to understand the state of the server better.
- Include security headers.
- Keep the server up to date.
Support and Documentation
Apache offers community support through the Apache HTTP Server Users Mailing List. Support is also available on dedicated IRC channels, Stack Overflow, etc.
The Apache HTTP Server Documentation offers reference manuals, user guides, tutorials, and other documents.
Nginx offers community support, consisting of a community-operated mailing list and a forum, as well as other support resources such as the Admin Guide and the Modules Reference Guide. Commercial support is available for the paid option, NGINX Plus.
Open-source Nginx documentation includes installation instructions, how-tos, development guides, modules references, and other documents.
Apache or Nginx
The newest market share research data shows that Apache is slightly ahead of Nginx.
However, if we only look at the top 1000 best ranking websites, Nginx is ahead by a large margin.
Both Apache and Nginx are polished, well-performing servers. When choosing the right server to install on your hardware, you need to consider what you will be using it for.
Choose Apache over Nginx if:
- In your use case, it is preferable for non-privileged users to have control over their websites. One of those cases is managing a shared hosting environment, in which Apache’s
.htaccess
file becomes practically indispensable. - You need certain modules that Nginx does not support.
Choose Nginx over Apache if:
- Your use scenario includes mostly (or exclusively) serving static content.
- You expect high traffic volume on your website.
When to Use Both Nginx and Apache
Apache and Nginx may be used side by side to create a workload-optimized server. Nginx is set up to act as a reverse proxy for Apache, which leverages Nginx’s processing speed and ability to handle large traffic volumes. Nginx processes and serves static content while forwarding dynamic content to Apache.
By letting Nginx sort requests and process the ones it can handle alone, Apache receives a smaller volume of requests. That pre-emptively resolves potential overloading issues. Since Nginx can communicate with a server pool in the backend, adding more servers and scaling out can be performed easily.
Conclusion
After reading this comparison article, you should have a better understanding of the similarities and differences between the Apache HTTP Server and Nginx. It will help you decide which solution fits your needs best.