Nginx vs Apache

Source: Internet
Author: User
Tags what is nginx ruby on rails microsoft iis

Original address:https://anturis.com/blog/nginx-vs-apache/

Nginx vs Apache

What's the Nginx Web and proxy server and how does it compare to Apache? Should you use one of these servers or both? Here we explore some answers to these questions.

What is Nginx Web server and proxy server and how is it compared with Apache? Do you use one or two of them? Here, let's explore a few answers to these questions.

The Apache Web servers has been in use since 1995. Apache powers more websites than any other product; Microsoft IIS comes in second.

Apache Web server has been used since 1995, is the world's most used products Web server, Weibo, the second.

Because the Open-source Apache Web server have been available for so many years, and have so many users, lots of modules hav e been written to expand its functionality-most of these is open-source as well. One popular configuration, for instance, is-to-use Apache-Server up-static web pages and the MOD_JK module to run Java and JSP code on the Tomcat to make an interactive application. Another example is using the mod_php to execute PHP scripts without have to use CGI.

Because the open source Apache Web server has been in use for so many years, with so many users, there are a lot of extension modules--and most of them are open source. One of the popular configurations, for example, is to use Apache as a Web page server, and the MOD_JK module runs java,tomcat to run JSP code to build an interactive application. Another example is the use of the mod_php module to execute PHP scripts without using CGI.

But Apache slows-under heavy load, because of the need to spawn new processes, thus consuming more computer memory. It also creates new threads that must compete with others for access to memory and CPU. Apache would also refuse new connections when traffic reaches the limit of processes configured by the administrator.

But Apache slows down under high load, because it consumes more computer memory to derive new processes. He also creates new threads, which must be done by using more memory and CPU. When traffic reaches the limit of the number of processes configured for management, Apache rejects the new connection.

Nginx is a open source Web server written to address some of the performance and scalability issues associated with Apach E. The product is open source and free, but Nginx offers support if you buy its Nginx Plus version.

Nginx is an open source Web service dedicated to addressing the scalability issues encountered by Apache. Nginx is an open source free of charge, but it also supports you to purchase its nginx+ version.

Nginx says its web server were written to address the c10k problem, which are a reference to a paper written by Dan Iel Kegel about-get-one Web server to handle connections, given the limitations of the operating systems. In the He paper, he cited another paper by Dean Gaudet The WHO wrote, "Why don ' t you guys use a select/event based model like Zeu S? It ' s clearly the fastest.

Nginx said that his Web server was used to address c10k issues raised in a paper written by Daniel Kegel on "How to make a Web server handle 10,000 connections under operating system restrictions". In this paper, he cites another paper written by Dean Gaudet-"Why don't you guys use a model like Zeus based on choice or event?" This is obviously a lot faster. "

Nginx is indeed event-based. They call their architecture "Event-driven and Asynchronous". Apache relies on processes and threads. So, what ' s the difference?

Nginx is indeed event-based. They call this architecture "event-driven and asynchronous." Apache relies on processes and threads. So, what's the difference?

How Apache works and what it has limitations

How Apache works and why it has limitations


Apache creates processes and threads to handle additional connections. The administrator can configure the server to control the maximum number of allowable processes. This configuration is varies depending on the available memory in the machine. Too many processes exhaust memory and can cause the machine to swap memory to disk, severely degrading performance. Plus, when the limit of processes is reached, Apache refuses additional connections.

Apache processes additional connections by creating processes and threads. Administrators can configure the server to control the maximum number of process connections allowed. The size of this configuration depends on the available memory size of the server host. Excessive processing runs out of memory and causes the host to swap hard disks for memory , severely reducing performance. Also, when the number of processes reaches the limit, Apache rejects additional connection requests.

Apache can be configured to run in pre-forked or worker multi-process mode (MPM). Either it creates new processes as additional users connect. The difference between the and that's pre-forked mode creates one thread per process, each of the which handles one user requ Est. Worker mode creates new processes too, but each have more than one thread, and each of the which handles one request per user. So one worker mode process handles more than one connection and one pre-fork mode process handles one connection only.

Apache can also be configured to run pre-forked or multi-process worker mode (MPM). Both can create new processes for additional user connections. The difference is that the pre-forked mode creates a thread for each process, and each thread processes a user request. The Worker pattern also creates a new process, but a process creates more than one thread, and each thread processes one request from a user. So a worker-mode process processes more than one connection, whereas a process with a pre-fork mode processes only one connection.

Worker mode uses less memory than forked-mode, because processes consume more memory than threads, which is nothing more than code running inside a process.

The worker pattern consumes less memory than the forked mode because the process consumes more memory than the thread, and there is nothing more memory consuming than running a line thread the code.

Moreover, worker mode is not thread safe. That means if-non thread-safe modules like mod_php, to serve up PHP pages, you need-use pre-forked mode, thus C onsuming more memory. So, when choosing modules and configuration you had to confront the thread-versus-process optimization problem and Constr Aint issues.

In addition, worker work is not thread-safe. This means that if you want to use a thread-safe module like mod_php to run a PHP page, you need to use pre-forked mode, which will consume more memory. So, to select modules and configurations, you have to face threading and process optimization problems and limitations.

The limiting factor in tuning Apache was memory and the potential to dead-locked threads that was contending for the same C PU and memory. If A thread is stopped, the user waits for the Web page to appear, until the process makes it free, so it can send back th E page. If a thread is deadlocked, it does isn't know how to restart, thus remaining stuck.

The limiting factor for debugging Apache is the same CPU and in-memory memory and potential thread deadlock preemption. If a thread is stopped, the user waits for the page to appear until the process is released before it can be sent back to the page. If a thread is deadlocked, it does not restart automatically, which causes the card to die.

Nginx

Nginx works differently than Apache, mainly with regard to how it handles threads.

Nginx does not work the same as Apache, mainly about how to handle threads.

Nginx does not create new processes for each Web request, instead the administrator configures how many worker processes T o Create for the main Nginx process. (One rule of thumb is to the one worker process for each CPU.) Each of the these processes is single-threaded. Each worker can handle thousands of concurrent connections. It does this asynchronously with one thread, rather than using multi-threaded programming.

Nginx does not create a new process for each network request, instead the administrator configures how many worker processes to allocate as the Nginx master process (an experience is how many worker process is built with the number of CPUs. )。 These processes are single-threaded. Each worker can handle thousands of concurrent connections. It is a thread that is asynchronous rather than multithreaded.

The Nginx also spins off cache loader and cache manager processes to read data from disk and load it into the cache and ex Pire it from the cache when directed.

Nginx also loops through the cache loader and the cache manager to read the data from the hard disk and load it into the cache and release it from the cache against the ground.

Nginx is composed of modules, is included at compile time. That means the user downloads the source code and selects which modules to compile. There is modules for connection to back end application servers, load Balancing, proxy server, and others. There is no module for PHP, as Nginx can compile PHP code itself.

The modules that are included in Nginx compilation. This means that the user can download the source code and select the required modules to compile. Some modules support connection of backend application services, load Balancing, proxy servers, and so on. There is no PHP-supported module, because nginx can compile PHP code.

Here are a diagram of the Nginx architecture taken from Andrew alexeev′s deep analysis of Nginx and how it works.

Here is an image of the nginx frame from the deep analysis of the Nginx working mode of Andrew Alexeev:


In this diagram we see so Nginx, in this instance, are using the fascgi process to execute Python, Ruby, or other code an d The Memcache memory object caching system. The worker processes load the main ht_core Ngix process to for HTTP requests. We also see this Nginx is tightly integrated with Windows and Linux kernel features to gain a boost in performance; These kernel features has improved over time and allowing Nginx to take advantage.

In the example, we see that Nginx is using the fastcgi process to execute Python, Ruby and other code, and uses the Memcache memory object to cache the system. The worker process loads the HTTP request in the main ht_core nginx process. We can also see that nginx is tightly integrated in the Windows and Linux kernel features to get high performance, these features changed over time to improve, so that nginx can be fully utilized.

Nginx is said-be event-driven, asynchronous, and non-blocking. "Event" means a user connection. "Asynchronous" means that it handles the user interaction for more than one user connection at a time. "Non-blocking" means it does not stop disk I/O because the CPU is busy; In this case, it works on other events until the I/O is freed up.

Nginx is called event-driven, asynchronous, non-blocking. "Event" means a user connection. "Async" means that it handles the interaction of multiple connections at the same time. "No blocking" means that the CPU does not stop disk I/O when it is busy, in which case Nginx will work on other events until I/O is released.

Nginx compared with Apache 2.4 MPM

Comparison of Nginx with Apache2.4 MPM

Apache 2.4 includes the MPM event module. It handles some connection types in a asynchronous manner, as does Nginx, but not in exactly the same manner. The goal is to reduce memory requirements as load increases.

Apache 2.4 contains the MPM event module. It handles some connection types as well as nginx, but not exactly the same way. The goal is to reduce the need for memory as the load increases.

As with earlier versions, Apache 2.4 includes the worker and pre-forked modes we mentioned above but have added the Mpm_eve Nt_module (Apache MPM event module) to solve the problem of threads that is kept alive waiting for that user connection T o make additional requests. MPM dedicates a thread to handle sockets that is both in listening and keep-alive state. This addresses some of the memory issues associated with older versions of Apache, by reducing the number of threads and P Rocesses created. The administrator would need to download the Apache source code and include the Mpm_event_module, and compile Apache instead of using a binary distribution.

As in earlier versions, Apache2.4 contains the worker and pre-forked patterns mentioned earlier and also adds the Mpm_event_module (Apache MPM event module) To resolve the problem of threads remaining alive to wait for user connections to handle more requests. MPM is committed to a thread that handles listening and saving a socket in two different states. This is used to solve some of the memory problems encountered by older Apache by reducing the number of threads and process creation. To use this feature, the administrator must download the Apache source code containing the Mpm_event_module module to compile to replace the binary release.

The Apache MPM event module is not exactly the same as Nginx, because it still spins off new processes as new requests COM E in (subject to the limit set by the administrator). Nginx does not set to more processes per user connection. The improvement, comes with Apache 2.4 was that those processes, is spin offs create fewer threads than normal APA Che worker mode would. This was because one thread can handle more than one connection, instead of requiring a new process for each connection.

The Apache MPM event module is not exactly the same as Nginx, as it still creates a new process for new requests (limited by administrator settings). Nginx does not create more processes for each user connection. Apache2.4 brings a boost that the process will create fewer threads than the standard Apache worker pattern. This is because a thread can handle more than one connection, rather than a single process.

Using Nginx and Apache both

Use both Nginx and Apache

Apache is known for it power and Nginx for speed. This means Nginx can serve up static content quicker, but Apache includes the modules needed to work with back end Applica tion servers and run scripting languages.

Apache is famous for its function and Nginx is famous for its speed. This means that nginx can serve the content more quickly, while Apache includes the backend application service and the modules that are required to run the scripting language.

Both Apache and Nginx can be used as proxy servers, but using Nginx as a proxy server and Apache as the-back end is a comm On approach to take. Nginx includes advanced load balancing and caching abilities. Apache servers can, of course, be deployed in great numbers. A load balancer is needed in order to exploit this. Apache has a Load balancer module too, plus there is hardware based load balancers.

Both Apache and Nginx can be used as proxy servers, but Nginx is usually used as a proxy server and Apache as a back-end server. Nginx has an efficient load balancing and caching capability. Apache can, of course, have to be deployed in more places. In order to take advantage of good load balancing, load balancer is needed. Apache has a load balancer for the base hardware.

Another configuration is-to-deploy Nginx with the separate PHP-FPM application. We say that php-fpm are an application, because it's not a. dll or. So loaded at execution time, as is the case with Apach E modules. PHP-FPM (The FastCGI Process Manager) can be used and nginx to deliver PHP scripting ability to Nginx to render Non-stati C content.

Another configuration is to deploy nginx together with a separate PHP-FPM application. We say that PHP-FPM is an application because he is not a. dll or. So file that is loaded at execution time as the Apache module does. The PHP-FPM (FastCGI process Manager) is used with Nginx to provide PHP scripting language capabilities for Nginx to render non-verbal content.

When was Apache preferred over Nginx?

When is Apache superior to Nginx?

Apache comes with built on support for PHP, Python, Perl, and other languages. For example, the Mod_python and mod_php Apache modules process PHP and Perl code inside the Apache process. Mod_python is more efficient this using CGI or FastCGI, because it does not has to load the Python interpreter for each R Equest. The same is true for Mod_rails and Mod_rack, which give Apache-the ability to run Ruby on rails. These processes run faster inside the Apache process.

Apache natively supports Php,python,perl and some other languages. For example, the Mod_python and mod_php two Apache modules handle PHP and Perl code in the Apache process. Mod_python is more efficient than using CGI or fastcgi because it does not have to load the Python interpreter for every request. There are also mod_rails and mod_rack that allow Apache to run Ruby on rails. These processes run faster inside the Apache process.

So if your website are predominately Python or Ruby, Apache might be preferred for your application, as Apache does not hav E to use CGI. For PHP, it does matter as Nginx supports PHP internally.

So if your site uses Python or ruby more, Apache is more useful for your application because it is not a CGI-only thing. For PHP It doesn't matter because Nginx supports PHP internally.

Here we have given some explanation of what Nginx is different from Apache and how do you might consider using one or both of them, and which of the might fit your needs more closely. Plus we have discussed how Apache 2.4 brings some of the Nginx improvements in thread and process management to the Apache Web server. So you can choose the best solution for your needs.

At this point we have explained the differences between Nginx and Apache, and how you choose or how to use them at the same time, which approach is closer to your needs. In addition, we discussed some of the improvements that Apache2.4 has brought to Apache WEB server for the management of Nginx thread processes. Now you can make the best solution according to your needs.

Nginx vs Apache

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.