Search Posts

Nginx and Apache comparison and advantages and disadvantages

Today is a more detailed comparison of the similarities and differences, advantages and disadvantages of apache httpd and nginx web servers. Since I am not doing web development, so please understand that there are any misunderstandings. I want to understand them because I sometimes use it in my work. I have used nginx+apache in my system. The vast majority of the information in this article is excerpted from the Internet. All that you do is to integrate the scattered resources on the Internet and add your own insights. Simply put, apache httpd and nginx are web servers, but the two adapt to different scenarios, that is, the two focus on solving different problems. Apache httpd: stable, strong dynamic request processing, but at the same time high performance when the concurrent performance is weak, costing more resources. Nginx: High concurrent processing capability, good at handling static requests, reverse proxy, and balanced load. In this article, the 13 similarities and differences between apache and nginx are listed in detail. Let’s analyze the principle one by one.

1, nginx relative to apache advantages: lightweight, also from the web service, occas less memory and resources, anti-concurrency, nginx processing requests are asynchronous non-blocking, and apache is blocking type, in high Concurrently, nginx can keep low resources, low consumption, high performance, highly modular design, relatively simple writing modules, active community, and various high-performance modules are produced quickly.

The advantages of apache over nginx: rewrite, stronger than nginx’s rewrite, and many modules, the basic thoughts can be found less bugs, nginx bugs are relatively more stable. Existence is the reason, in general, a web service that requires performance, using nginx. If you don’t need performance and only want stability, then apache. The latter’s various functional modules are implemented better than the former, for example, the ssl module is better than the former, and there are many configurable items. It should be noted here that epoll (kqueue on freebsd) network IO model is the fundamental reason for the high performance of nginx processing, but not all cases are epoll wins, if you provide static services, only a few Files, apache’s select model may be more efficient than epoll. Of course, this is just a hypothesis based on the principle of the network IO model. The real application still needs to be measured.

##For the network IO reuse model, I don’t understand it myself, but I explained it to Baidu Encyclopedia on epoll: epoll is an enhanced version of multiplexed IO interface select/poll under Linux, which can significantly improve the program in a large number of concurrent connections. There is only a small amount of active system CPU utilization, because it reuses the file descriptor set to pass the result without forcing the developer to re-prepare the set of file descriptors to be listened to each time before waiting for the event. One reason is that when acquiring an event, it does not have to traverse the entire set of listeners being listened to, as long as it traverses the set of descriptors that are asynchronously awakened by the kernel IO event and added to the Ready queue. From this point, it can be seen that the epoll used by nginx is more efficient than the select algorithm in theory (why is theoretically, let me give an example to an O(N) and O(N^2) algorithm if it is to be processed. The size of the data is 1 or very small, can the two reflect the difference in efficiency?) The other is that the select IO model has restrictions on the file descriptors opened by each process, so I also affect the performance of apache high concurrency. A factor.

2. As a Web server: Compared to Apache, Nginx uses fewer resources, supports more concurrent connections, and achieves higher efficiency, which makes Nginx especially popular with web hosting providers. In the case of high connectivity concurrency, Nginx is a good alternative to the Apache server: Nginx is one of the software platforms that are often chosen by the owners of the web hosting business in the US. It can support up to 50,000 concurrent connections, thanks to Nginx. We chose epoll and kqueue as the development model. Nginx acts as a load balancing server: Nginx can directly support Rails and PHP programs internally, or it can support external services as an HTTP proxy server. Nginx is written in C, whether it is System resource overhead or CPU usage efficiency is much better than Perlbal. As a mail proxy server: Nginx is also a very good mail proxy server (one of the earliest development of this product is also a mail proxy server), Last.fm describes success. And wonderful experience. Nginx is a very simple installation, the configuration file is very simple (also supports perl syntax), Bugs very few servers: Nginx startup is particularly easy, and can almost always run 7*24, even if Not running for several months Need to restart. You can also upgrade the software version without interruption.

##nginx supports higher concurrent connections than Apache, which is more efficient. This is related to the first point. The network IO model is different. The other is that nginx is asynchronously processing requests, while apache is synchronous processing. Each process corresponds to a request. The shortcomings of apache using one request per process are discussed later.

3, Nginx configuration is simple, Apache complex, Nginx static processing performance is more than 3 times higher than Apache, Apache support for PHP is relatively simple, Nginx needs to be used with other backends, Apache components are more than Nginx, now Nginx is the preferred choice for Web servers

##Nginx is weak for dynamic processing requests. I want to relate to its own implementation. It needs to be combined with other modules to support PHP and other languages. Apache supports it better. If you find the root cause of the underlying implementation, welcome. Message guidance~

4, the core difference is that apache is a synchronous multi-process model, a connection corresponds to a process; nginx is asynchronous, multiple connections (10,000 levels) can correspond to a process

##The different models for requesting requests directly lead to two points: a>nginx has a lot of anti-concurrency ability. b>nginx has fewer resources, because apache process and request have one-to-one correspondence, when the request is very large Resource requirements are large, and process creation costs are expensive. But now seems to have made improvements, prefork can pre-create some processes according to the needs, this is somewhat similar to the concept of thread pool.

5, nginx processing static files is good, less memory. But no doubt apache is still the current mainstream, there are many rich features. So you need to match. Of course, if you can determine that nginx is suitable for demand, then using nginx will be more economical The way.

## This should be related to the points mentioned above: nginx uses the epoll IO reuse model; requests are processed asynchronously; threads and requests are one-to-many relationships.

6, from the personal use of the past, the load capacity of nginx is much higher than apache. The latest server has also changed to nginx. And nginx changed the configuration can -t test the configuration there is no problem, apache restarts when the configuration is found wrong, it will be very crash, change will be very cautious now see a lot of cluster stations, front-end nginx anti-concurrency, back-end apache cluster The cooperation is not bad.

## In this point, we mainly focus on this: nginx+apache combined use. Since both have their own advantages, then we will use our strengths to avoid weaknesses. Nginx is responsible for anti-concurrency, load balancing, and static file caching. The backend uses apache to handle dynamic requests.

7, nginx processing dynamic requests is a chicken rib, the general dynamic request to apache to do, nginx only suitable for static and reverse.

##nginxWhen dealing with dynamic requests is the reason for the chicken ribs, who can help explain the principle? Is it because the language support for PHP is not good enough? What is suitable for nginx is static request and reverse proxy. What is reverse proxy? To put it simply, the client uses the nginx server as its target machine and sends the request to the nginx machine. As for the nginx machine, the resource that the client needs is obtained from the client. The client does not care (this has a difference and a positive Agent, in the forward proxy I can’t access the target machine, because I will send the request to your proxy machine, and then get the resources I need in your name).

8, from my personal experience, nginx is a very good front-end server, load performance is very good, open nginx on the old run, use webbench to simulate 10,000 static file requests without any effort. Apache support for php and other languages ​​is very good, in addition apache has a strong support network, development time is longer than nginx, bugs but Apache has innate do not support the disadvantages of multi-core processing load, it is recommended to use nginx as front end, backend Use apache. Large website recommends using nginx self-generation cluster function

## This is still saying that nginx+apache is a good choice.

9, Nginx is better than Apache’s two main points: 1. Nginx itself is a reverse proxy server 2. Nginx supports 7-layer load balancing; other, of course, Nginx may support higher concurrency than Apache, but according to NetCraft statistics In April 2011, Apache still holds 62.71%, while Nginx is 7.35%, so in general, Aapche is still the first of most companies, because its mature technology and development community is also very good performance. .

##apache appeared early, people did not choose at the beginning, and the small pressure of the website is also enough to deal with the request pressure with apache, so there is a gap between the two market share.

10. Your needs for the web server determine your choice. In most cases, nginx is better than APACHE, such as static file processing, PHP-CGI support, reverse proxy functionality, front-end Cache, maintenance connectivity, and more. In Apache+PHP (prefork) mode, if the processing of PHP is slow or the front-end pressure is high, it is easy to see the number of Apache processes soaring, thus denying service.

##apache’s defect, anti-pressure is not good, and because the number of threads soars, the resource demand is also great

11, you can look at the nginx lua module: https://github.com/chaoslaw…apache more than nginx module, you can directly use lua to achieve apache is the most popular, why? Most people are too lazy to update to nginx or learn new things.
##…

12, for nginx, I like its configuration file is very simple to write, regular configuration makes a lot of things simple and efficient, low resource consumption, powerful proxy, very suitable for front-end response server

##Look, the configuration file of nginx is indeed more concise and easy to understand.

13, Apache has advantages in processing dynamics, Nginx concurrency is better, CPU memory usage is low, if rewrite is frequent, it is still Apache
##rewriteThis is not very well understood, not much to say

Leave a Reply