Home  >  Article  >  Operation and Maintenance  >  Which engine is better, Apache HTTP or Nginx?

Which engine is better, Apache HTTP or Nginx?

王林
王林forward
2023-05-11 15:49:06866browse

HTTP middleware

When we enter a web link in the browser, the browser sends a request to the corresponding server based on the HTTP(s) transmission protocol, and the server receives The corresponding request is processed and the corresponding information is returned to the browser. The browser then parses the content in http and displays it in the form of a web page.

The server is responsible for receiving the request and returning the corresponding data after processing, which can be subdivided into the service part that processes the http connection and the application part that executes the service content ( WordPress uses PHP to generate the required pages, which belongs to the application part)

Regardless of what kind of application the application part executes, it is the part that handles the http connection They are almost the same, so middleware has emerged that specifically handles http connections. Currently, the most common ones are Apache and Nginx.

Apache

The official name is "Apache HTTP Server", which is an open source HTTP server middleware. It was born in 1995. It is the leader in the HTTP service field, with a large number of users and rich community resources. One of the great advantages of Apache is that it is easy to integrate with Wordpress and other CMS software. You can build a CMS-based website with only simple settings.

Apache's internal processing model

In terms of internal structure, Apache adopts a multi-process approach. Each time there is a connection, a process will be opened for this connection, specifically for Handle requests on this connection until the connection ends. The advantage of this is:

  • Connections from different clients will be responded immediately without interfering with each other, and other connections will not be affected because one service takes a long time. No response.

But the shortcomings are also obvious:

  • When there are many simultaneous accesses, Apache will create a large number of Process, occupying too much memory resources.

  • Scheduling between a large number of threads will also cause a lot of waste of CPU processing power.

This gave rise to a problem called C10K, C is the client, and 10K refers to 10,000, regardless of the performance of the server and the network No matter how high the bandwidth is, it will be difficult for Apache to handle more than 10,000 connections at the same time.

Nginx

is pronounced as Engine-X, and like Apache, it is also an open source middleware for HTTP service, born in 2004. Nginx has a shorter history than Apache, but precisely because it is a latecomer, Nginx learned the lessons of Apache and implemented it in the early design stage. Taking into account the efficiency issue when handling a large number of connections, it solves the problems that arise with the growth of the Internet, such as C10K.

Nginx’s internal processing model

Nginx adopts non-blocking IO and asynchronous message-driven methods, which is called worker uses a loop in the thread to process the connection request in the queue. Depending on the hardware, multiple worker threads can be set to make full use of CPU's core resources.

  • It solves the problem of excessive memory consumption and low scheduling efficiency when processing a large number of connections, and at the same time, it can fully utilize all CPU cores. The ability to handle concurrent connections on the same hardware is 10 to 100 times that of Apache.

ButNginxThis method is not without its shortcomings.

  • When the single-core performance of the server is poor, a dynamic website based on CMS may take a long time to execute a request. At this time, requests from other clients will not be executed immediately. It will be more obvious when CPU has fewer cores and worker threads are insufficient.

Fortunately, the performance of servers is getting stronger and stronger. Under the leadership of AMD, the number of CPU cores is also increasing,# The shortcomings of ##Nginx are enough to be compensated, and the advantages of high efficiency are becoming more and more apparent.

Comprehensive comparison

Apache NginxThe processing capacity is limited 10-100 times. Will it be blocked by complex tasks? Is it possible to set the difficulty to be relatively simple and relatively complex? The community has rich resources and relatively Less

In recent years, the market share of

Nginx has continued to increase, and in 2019 it has reached the same level as Apache. For large websites with a huge number of visits, you can see that the greater the number of visits, the higher the proportion of Nginx will be. This also confirms the superior performance of Nginx when handling a large number of accesses.

Load Balancing

NginxIn addition to being used as an HTTP server, its powerful reverse proxy function is also widely used as a load balancing front-end server, gradually replacing the Hardware load balancer.

Several backend servers can be configured in

Nginx. Nginx will follow certain rules (polling, IP Hash, priority random), etc. forward the request to the back-end server to achieve average or weighted distribution of load on multiple servers.

At the same time, as a load balancing front-end, it can also cache the data returned by the back-end to relieve the pressure on the back-end server. The front-end uses Nginx for load balancing to limit the number of connections to each server, and it is not uncommon for the back-end server to run Apache.

The industry leader of hardware load balancersF5 networks acquired Nginx in 2019 and launched a load balancing solution including paid servicesNginx .

The above is the detailed content of Which engine is better, Apache HTTP or Nginx?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete