Nginx and Apache differences

I, Arslan Mirbzergi, intend to share my personal experiences with you. Onmywebsite,   you can see examples and practical projects and implement them in your own projects. In this section, we will briefly explain the important differences between Nginx and Apache.
The Internet began its global “takeover” in the 1990s. The entire “Web” protocol can be summarized as a visitor requesting a document from a specific web address, which transfers the request to the appropriate computer with DNS and IP. This computer, which hosts the requested web page, displays the web page to the visitor or “serves”.
Web pages are essentially HTML documents. In order to provide visitors with different web pages, the “serving” machine needs a server program. Software such as Nginx or Apache controls requests, analyzes them, and then delivers the relevant documents for viewing in the visitor’s browser.

1- Nginx vs. Apache

Nginx and Apache are popular Web server that are used to transfer web pages to a user’s browser. In a nutshell:
• Apache was published in 1995 and then Nginx was presented in 2004.
• Both are used by large companies around the world.
• Nginx’s market share has been growing continuously for years.
• In some cases, Nginx has a competitive advantage in terms of performance.

2-  Apache

After Tim Berners-Lee’s CERN HTTPd and NCSA HTTPd first emerged in the first few years of the Internet, Apache was first released in 1995 and quickly captured the market and became the world’s most popular Web server. Today it is still famous on the market for the same old reasons and is being developed and maintained under apache license by the Apache Foundation.
There are two different stories about how to name Apache. One version says the name originates from the famous Native American heritage, while another says it comes from a “Patchy server” that displays a series of software patches.

2-1- Linux

Apache’s huge market share is partly because it has all the major Linux distributions, such as Red Hat/Centos and Ubuntu.
An example of Apache’s important role in the Linux world is that its server process name is HTTPd, making Apache synonymous with Web server software. In addition to being the first Apache in the Web server market, part of apache’s expansion is due to its Configuration system and httaccess file. It is.

2-2- httaccess.

Apache for your Configuration of httaccess. uses. Given the many Flexibility that Apache provides for how Configure handles incoming requests, it’s about how Configure, edit and work with the httaccess file. There is also a lot of training. For example: various Redirection rules, maximum upload file size, URL overwriting, memory limitation, Directory protection (htpasswd), expired headers, cache-control headers, Encoding headers, cookies, manipulation of query strings.
Kinsta, on the other hand, uses Nginx, which uses htaccess files. Does not support. However, the settings and rules in the htaccess files can be easily translated into Nginx rewriting Syntax.
One of apache’s main “advantages” is that on the main server root or directory of the website, any Level or Directory in Directory tree can file httaccess. its own configuration.
For Shared hosting providers, this is a dream because they can support hundreds of users on the same system and configure how their websites are serviced without affecting each other. Customers can also configure a lot of details in a Limited Shared hosting environment without the overall configuration of the server.
As stated in the official documentation:
“In general, you should only use htaccess files if you do not have access to the server’s original Configuration file. Use.”
It has flexibility per htaccess files. It’s going to hit Performance, even if you don’t really use them!
Whenever the files htaccess. Enabled, Apache must traverse the entire Directory tree from the URL or file requested through all the higher levels to server’s root directory, and then load them for each request. It should then process these files and re-configure itself for each configure directory in this way.
With WordPress websites, things get quite complicated. A typical WordPress website can have hundreds of requests from different Directory. The /wp-content/uploads/yyyy/mm type usually loads several requests on one page, often consisting of directories of different months. Then there will be /wp-content/themes/parent-theme static resources and /wp-content/themes/child-theme resources, including JavaScript, CSS files and image. Then there are /wp-content/plugins with Static files downloaded from dozens of subcategory from sub-plugins, which for each of these resources, Apache for plugin searches must process its entire Directory tree.
An analysis has shown that a typical WordPress setting, which is not the typical setting for websites in Shared hosting, involves 42 separate htaccess implementations. And 249 separate searches for htaccess files. would be.
It’s only at web server level, and the visitor will still have to wait for PHP to run the entire WordPress Call stacks process and create a database request and give it to MySQL to assemble the web page and send it to the visitor.

2-3- Modules

Another case that made Apache popular is its modularity.
Modules, as a feature that allows users to expand web server functionality, are available both in Nginx and Apache. Apache allows users to install modules after installing and deploying the web server, and then enable or disable them if necessary. Debian-based Distributions have a command that enables and disables these Modules without having to edit Configuration files: a2enmod and a2dismod.
The official list of Modules offered as part of apache standard distribution includes Compression, Encryption, Logging, Redirection to more advanced items such as editing requests and responding with Advanced Syntax.

3-  Nginx

Nginx was first published in 2004 by Igor Sysoev, a Russian Developer. As Owen Garrett, Nginx project manager said:
“Nginx is specifically written to remove performance restrictions on Apache Web server.”
The server was first created as a tool for Scaling rambler.ru website in 2002 and has two versions: Open Source, licensed by BSD and Nginx Plus, with support and other Enterprise features.
After publication, Nginx was mostly used to service Static files and as Load-balancer or Reverse proxy in the Apache installation. As the web evolved and needed to speed up and increase hardware performance, more websites began to fully replace Apache with Nginx thanks to having fully fledged software.
In March 2019, Nginx Inc was bought by F5 Network for $670 million. At that moment, as Techcrunch reported, Nginx’s server supplied 375 million websites with about 1,500 customers.
According to w3techs data, Nginx’s market share is constantly growing, pushing Apache out and dismissing it from the start:
This data relates to overall Web server globally, but if we take samples from a million top websites, Nginx has been there for some time:
Google’s search process also reflects this fact:
Netcraft poll shows Nginx surpassed Apache in April 2019.

4-  Nginx Configuration

Nginx does not have a Configuration system like Apache, so, despite much greater performance and speed, it is not widely used in Hosting providers and does not shine like Apache in shared environments.
On the other hand, as stated above, by not allowing Configuration to be applied at directory level, Nginx has a significant advantage over Apache. There is an article on the Nginx wiki that compares the performance impact as follows:

Nginx vs. Apache performance

Nginx Modules

Nginx Modules are another thing that makes Nginx a better option. Nginx Modules usually need to be activated at the time of construction, which means there is more technical skill involved, and adding Modules after installation is a little more complicated.
In 2016, with version 1.9.11, everything has changed and the dynamic module/official verified repository is dedicated to Paying users. Starting in 2019, starting development and support for QUIC and HTTP/3 were announced.

Caching: Nginx vs Apache

Caching is meant to prepare content for website visitors before they visit, so you don’t need to search for the content they want, and you’ve already prepared it, and they’ll get the content without waiting.
Like Apache, Nginx would normally be placed between servers and end user to reduce Performance hit on other infrastructure. In these cases, it can cache static content from fetch’s protected server at any time.
If we use Nginx as a standalone Web server, there is no need for cache, and Nginx alone is very efficient at providing Static content. As is the case with Kinsta LXC containers.
Then there is the Dynamic cache or Page cache issue. On WordPress websites, this means storing all wordPress pages generated for each URL in memory or disk.
FastCGI caching is typically available in the Nginx standard installation, which is simple and very powerful and is a less commonly used feature of Nginx.
Apache, on the other hand, has Mod_cache modules that appear to be inconsistent with other Modules, so the standard Caching solution with Apache is the Varnish HTTP accelerator. Although Varnish is a proprietary solution for the industry, some recent experiments suggest Nginx has better Caching than Varnish.

Handling Requests: Nginx vs Apache

The greatest difference between Apache and Nginx in architecture is how to handle requests.
Apache processes requests with MPM-s or Multi-Processing-Modules, which are “responsible for connecting to network ports on the device, accepting requests and dispatch processors to handle requests.”
The oldest MPM dating from the beginning of Apache is the Prefork module. This Module alone can be considered as the reason for apache’s notoriety. Under these conditions, Apache generates new processes with one Thread per request.
This Module is used with mod_php and means that the Apache server has embedded a PHP interpreter in each process, even if it has to serve CSS files or imagery.
Prefork comes with Apache as the default Module and limits the connection to HTTP/1.
In later years, Apache, Multi-threaded for mpm and later, event mpm developed, both of which reduce many of Apache’s performance problems. php-fpm makes it possible for Apache to come today along with removing the use of htaccess. Remain in the competition.
Nginx uses asynchronous and non-blocking architecture.
In the Linux/Unix world, processes run apps. Threads are a subset of processes and can be multiple threads in process execution, such as multiple Tabs in the browser window. This way an app can use Multiple CPUs and Multi-core and Multi-thread CPUs to run faster.
In short, Apache uses processes for each connection (and uses Threads with mpm) and becomes quickly and overly expensive at a time of increased traffic.
One can depict a new process or thread creation, such as setting up a computer or launching programs. Even on the fastest computers it still takes a while. Today, websites load hundreds of requests on one page.
Event mpm is slightly ahead in terms of optimization, but some tests show that it cannot leave Nginx behind. Especially when it comes to Static files, the speed of requests that Nginx serves us is twice as fast as Apache.
In ideal mode, Nginx has a Worker process per CPU/core. The difference between Worker Nginx processes is that each can manage hundreds of thousands of incoming network connections for each Worker, and there is no need to create threads or new processes to create each connection.
This is because major Content Delivery networks, such as Cloudflare, MaxCDN and KeyCDN, or websites like Netflix, consider Nginx very important to their Content Delivery.
The list of companies that use Nginx is too long. Private company WordPress.com and Automattic is one such case. Automattic converted all its Load-balancers to Nginx in 2008 for WordPress.com, and Stack moved its server entirely to Nginx.
If we want to check what websites have used in their production, we can usually find this in HTTP headers. This means that we need to right-click on a website and hit the option, and then in the Developer Tool section, select the Network Panel option and reload the website, and eventually we will see all the resources that the website loads. If we select a specific resource and its headers tab, we will usually see server information. If the website uses a CDN, we might see something like Cloudflare and if it uses an HTTP accelerator, something like Varnish on the server line.
This is an example of a WordPress website that uses the usual Shared hosting settings with cPanel, Apache and PHP:
And this is a website in Nginx:
On the left, if we expand it, we can analyze the time of each source and see its impact on the load time of the page.

And in the end,

In this paper, we investigated the differences between Nginx and Apache and explained the main differences in the architecture of the two that caused Nginx to attract more attention in webServer space.
Not every item in use, of course, has the same priorities, and Apache or other tools like Lighttpd, IIS, LiteSpeed, Caddy can also be good solutions.
At Kinsta, Nginx is used as part of Performance-optimized hosting solutions for WordPress and WooCommerce. Each WordPress site is located in its own separate Container, which has all the software resources needed to run it (Nginx, Linux, PHP, MySQL). Resources are 100% private and are not shared between any other site.
Kinsta, a hosting company that has provided a scalable and secure solution for WordPress. To use Kinsta’s features, you can visit https://kinsta.com/.