In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "Why Nginx is more efficient than Apache". In daily operation, I believe many people have doubts about why Nginx is more efficient than Apache. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "Why is Nginx more efficient than Apache?" Next, please follow the editor to study!
Nginx has won the Web server in just a few years. As we all know, Nginx is significantly more efficient than Httpd in dealing with large concurrent static requests, and even can easily solve the C10K problem.
In the case of high concurrent connections, Nginx is a good alternative to Apache servers. Nginx can also be used as a 7-tier load balancing server. According to my test results, Nginx + PHP (FastCGI) can withstand more than 30, 000 concurrent connections, which is 10 times that of Apache in the same environment.
Generally speaking, servers with 4GB memory + Apache (prefork mode) generally can only handle 3000 concurrent connections because they will occupy more than 3GB memory and have to reserve 1GB memory for the system. I once had two Apache servers, because the MaxClients set in the configuration file was 4000, and when the number of Apache concurrent connections reached 3800, the server ran out of memory and Swap space and crashed.
While this Nginx + PHP (FastCGI) server consumes 150m memory (15M*10=150M) for 10 Nginx processes and 1280m memory (20M*64=1280M) for 64 php-cgi processes under 30,000 concurrent connections, together with the memory consumed by the system itself, a total of less 2GB memory is consumed. If the server memory is small, it is possible to start only 25 php-cgi processes, so that the total amount of memory consumed by php-cgi is only 500m.
Under 30 000 concurrent connections, the PHP program that accesses the Nginx+ PHP (FastCGI) server is still very fast.
Why Nginx is better than httpd in dealing with high concurrency, let's start with the working principles and working modes of the two web servers.
1. Three working modes of Apache
We all know that Apache has three working modules: prefork, worker, and event.
Prefork: multiple processes, each request is responded by a process, which is notified using the select mechanism.
Worker: multithreaded, a process can generate multiple threads, each thread responds to a request, but the notification mechanism is still select, but can accept more requests.
Event: based on the asynchronous Ithumb O model, a process or thread, each process or thread responds to multiple user requests, which is based on event-driven (that is, epoll mechanism) implementation.
1. The working principle of prefork
If you do not use "- with-mpm" to explicitly specify a certain MPM,prefork, it is the default MPM on the Unix platform. The pre-spawning child process adopted by it is also the mode adopted in Apache1.3. Prefork itself does not use threads, version 2.0 uses it to maintain compatibility with version 1.3; on the other hand, prefork uses separate child processes to handle different requests, which are independent of each other, which makes it one of the most stable MPM.
2. The working principle of worker
Relative to prefork,worker, it is a new MPM in version 2.0 that supports a mixed model of multi-threading and multi-process. Because threads are used to process, it is possible to handle a relatively large number of requests, and the overhead of system resources is less than that of process-based servers. However, worker also uses multiple processes, and each process generates multiple threads to achieve process server-based stability. This MPM workside style will be the development trend of Apache2.0.
3. The characteristics of event based on event mechanism
A process responds to multiple user requests and uses the callback mechanism to reuse sockets. After the request comes, the process does not process the request, but directly leaves it to other mechanisms to inform whether the request is completed through the epoll mechanism. In this process, the process itself has been idle and can always receive user requests. A process can be implemented to respond to multiple user requests. Support a large number of concurrent connections and consume less resources.
Second, how to improve the concurrent connection processing ability of Web server
There are several basic conditions:
1. Based on thread, that is, a process generates multiple threads, and each thread responds to each request of the user.
2. In the event-based model, a process processes multiple requests and notifies the user that the request is completed through the epoll mechanism.
3. Disk-based AIO (Asynchronous Icano)
4. Support mmap memory mapping, mmap traditional web server, when page input, the disk page is first inputted into the kernel cache, and then copied to the web server from the kernel cache. The mmap mechanism is to map the kernel cache to the disk, and the web server can directly copy the page content. There is no need to input the pages on the disk into the kernel cache first.
As it happens, Nginx supports all of the above features. So there is a basis for Nginx to say that Nginx supports 50000 concurrency.
III. The advantages of Nginx
Traditionally, Web services based on process or thread model handle concurrent connection requests per process or thread, which is bound to block during network and Ibank O operations, and another inevitable result is low utilization of memory or CPU.
Generating a new process / thread requires preparing its runtime environment in advance, which includes allocating heap memory and stack memory, and creating a new execution context for it. These operations take up CPU, and too many processes / threads can lead to thread jitter or frequent context switching, resulting in further degradation of system performance.
Another high-performance web server / Web server reverse proxy: Nginx,Nginx mainly focuses on its high performance and high-density utilization of physical computing resources, so it adopts different architecture models. Inspired by the advanced processing mechanism based on "event" in a variety of operating system designs, Nginx adopts modular, event-driven, asynchronous, single-threaded and non-blocking architecture, as well as a large number of multiplexing and event notification mechanisms.
In Nginx, connection requests are processed by a few single-thread process Worker with an efficient run-loop mechanism, while each Worker can process thousands of concurrent connections and requests in parallel.
Fourth, the working principle of Nginx
Nginx runs multiple processes as needed: a main process (master) and several worker processes (worker). When caching is configured, there are cache loader processes (cache loader) and cache manager processes (cache manager). All processes contain only one thread, and inter-process communication is mainly achieved through the mechanism of "shared memory". The main process runs as root, while worker, cache loader, and cache manager should all run as unprivileged users.
In the case of high connection concurrency, Nginx is a good alternative to Apache servers.
Nginx installation is very simple, the configuration file is very simple (and can also support perl syntax), servers with very little Bugs: Nginx is very easy to start, and can run almost 24 hours without reboot, even if it runs for several months. You can also upgrade the software version without interruption of service.
Fifth, the birth of Nginx mainly solves the C10K problem.
Finally, we analyze the multiplexing IO model used by each.
1. Select model: (apache is used, but not much due to the limitation of modules, etc.)
There is a maximum limit on the number of file descriptors that a single process can monitor
The data structure maintained by select () stores a large number of file descriptors. With the increase in the number of file descriptors, the overhead caused by copying in user mode and kernel address space will increase linearly.
Due to the delay of network response time, a large number of TCP connections are inactive, but calling select () still makes a linear scan of all socket, which will cause some overhead.
2. Poll:poll is re-implemented by unix using select itself, and the only problem solved is that poll does not have a limit on the maximum number of file descriptors; 3. Epoll model: (used by Nginx)
Epoll brings two benefits, significantly improving performance:
(1) based on the event ready notification method, select/poll mode, the kernel scans all monitored file descriptors only after a certain method is called, and the epoll event registers a file descriptor through epoll_ctl (). Once a file descriptor is ready, the kernel uses a callback mechanism similar to call back to activate the file descriptor quickly, and epoll_wait () will be notified.
(2) when epoll_wait () is called once to get the ready file descriptor, the value returned is not the actual descriptor, but a value that represents the number of ready descriptors. Take these values and go to an array specified by epoll to get the corresponding number of file descriptors in turn. Memory mapping (mmap) technology is used here to avoid the overhead of copying a large number of file descriptors.
(3) of course, epoll also has some limitations. Epoll is implemented only by Linux2.6, but not on other platforms, which obviously runs counter to apache, an excellent cross-platform server.
(4) to put it simply, epoll is an upgraded version of select, and there is no maximum limit for file descriptors managed by a single process. But epoll is only available on the linux platform. Apache as a cross-platform does not use
At this point, the study on why Nginx is more efficient than Apache is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.