Which of the following is better for Nginx vs Apache?

Source: Internet
Author: User
Tags microsoft iis

Which of the following is better for Nginx vs Apache?

Nginx vs Apache

What is the Nginx web and proxy server and how does it compare to Apache? Shocould you use one of these servers or both? Here we recommend e some answers to these questions.

What is the Nginx web server and Proxy Server? What is the comparison with Apache? Are you using either of them or both? Here, we will explore several answers to these questions.

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

Apache web servers have been used since 1995 and are the most popular web servers in the world, followed by IIS on Weibo.

Because the open-source Apache web server has been available for so wide years, and has so popular users, lots of modules have been written to expand its functionality-most of these are open-source as well. one popular configuration, for instance, is to use Apache to server up static web pages and the mod_jk module to run Java and JSP code on Tomcat to make an interactive application. another example is using mod_php to execute php scripts without having to use cgi.

Because the open-source Apache web server has been used for so many years and has so many users, there are many functional extension modules-and most of them are open-source. For example, one popular configuration is that Apache is used as the server on the web statement page. The mod_jk module runs java and Tomcat runs Jsp code to build an interactive application. Another example is to use the mod_php module to execute php scripts without using cgi.

But Apache slows down 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 will also refuse new connections when traffic reaches the limit of processes configured by the administrator.

However, Apache slows down under high loads, because it will consume more computer memory to derive new processes. It also creates new threads, which must be completed by using more memory and CPU. When the traffic reaches the maximum number of processes configured for management, Apache rejects new connections.

Nginx is an open source web server written to address some of the performance and scalability issues associated with Apache. 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 designed to solve the performance scalability problems encountered by Apache. Nginx is an open-source and free product, but it also supports the Nginx + version that you pay.

Nginx says its web server was written to address the C10K problem, which is a reference to apaperwritten by Daniel Kegel about how to get one web server to handle 10,000 connections, given the limitations of the operating systems. in his paper, he cited another paper by Dean Gaudet who wrote, "Why don't you guys use a select/event based model like Zeus? It's clearly the fastest ".

Nginx said that his web server was used to solve the C10K problem mentioned in the paper "How to allow a web server to process 10000 connections under operating system restrictions" written by Daniel Kegel. In this paper, he cited another paper written by Dean Gaudet-"Why don't you use a selection-based or event-based model like Zeus? This is much 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 their architecture "event-driven and asynchronous ". Apache depends on processes and threads. So what's the difference?

How Apache works and why it has limitations

Why does Apache work?

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 varies depending on the available memory on the machine. too resume 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. The administrator can configure the server to control the maximum number of allowed processes. The size of this configuration depends on the available memory of the server host. Too many processes run out of memory and the host switches the hard disk to the memory, seriously reducing the performance. In addition, Apache rejects additional connection requests when the number of processes reaches the limit.

Apache can be configured to run in pre-forked or worker multi-process mode (MPM ). either way it creates new processes as additional users connect. the difference between the two is that pre-forked mode creates one thread per process, each of which handles one user request. worker mode creates new processes too, but each has more than one thread, each of 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 between the two lies in that the pre-forked mode creates a thread for each process and each thread processes a user request. In Worker mode, a new process is also created. However, a process creates more than one thread, and each thread processes one user request. Therefore, a Worker-mode process processes more than one connection, while a pre-fork-mode process processes only one connection.

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

The Worker mode consumes less memory than the forked mode, because the process consumes more memory than the thread, and nothing consumes more memory than the code running in a thread.

Moreover, worker mode is not thread safe. that means if you use non thread-safe modules like mod_php, to serve up php pages, you need to use pre-forked mode, thus consuming more memory. so, when choosing modules and configuration you have to confront the thread-versus-process optimization problem and constraint 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 php pages, you need to use the pre-forked mode, which will consume more memory. Therefore, to select modules and configurations, you must face the problems and limitations of thread and process optimization.

The limiting factor in tuning Apache is memory and the potential to dead-locked threads that are contending for the same CPU 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 the page. if a thread is deadlocked, it does not know how to restart, thus remaining stuck.

The constraint for debugging Apache is the preemption of memory and potential thread deadlocks in the same CPU and memory. If a thread stops, the user waits for the webpage to appear until the process is released before it can send back the page. If a thread is deadlocked, it will not be restarted automatically. This will cause it to become stuck.

Nginx

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

Nginx is different from Apache in terms of processing threads.

Nginx does not create new processes for each web request, instead the administrator configures how does worker processes to create for the main Nginx process. (One rule of thumb is to have one worker process for each CPU .) each of 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 are allocated as the main Nginx process (one experience is how many worker processes are created on the CPU .). These processes are all single-threaded. Each worker can process thousands of concurrent connections. It is a thread asynchronous, not a multi-threaded program.

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

Nginx also cyclically reads data from the hard disk through the cache loader and cache manager, loads the data to the cache, and releases the data from the cache.

Nginx is composed of modules that are supported at compile time. that means the user downloads the source code and selects which modules to compile. there are 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.

Components of Nginx compilation. This means that you can download the source code and select the required module for compilation. Some modules support connection to backend application services, Server Load balancer, proxy servers, and so on. No PHP module is supported, because Nginx can compile PHP code by itself.

Here is a distriof the Nginx architecture taken from Andrew Alexeev's deepanalysisof Nginx and how it works.

The following is an Nginx architecture diagram obtained from the in-depth analysis of Andrew Alexeev's Nginx working method:


N this distriwe see that Nginx, in this instance, is using the FasCGI process to execute Python, Ruby, or other code and the Memcache memory object caching system. the worker processes load the main ht_core Ngix process to for HTTP requests. we also see that Nginx is tightly integrated with Windows and Linux kernel features to gain a boost in performance; these kernel features have improved over time, allowing Nginx to advantake Tage.

In the example, we can see that Nginx uses the FastCGI process to execute Python, Ruby and other code, and uses the Memcache Memory Object cache system. The HTTP request generated when the worker loads the main ht_core Nginx. We can also see that Nginx is tightly integrated with Windows and Linux kernel features for high performance. These features have been improved over time so that Nginx can be fully utilized.

Nginx is said to be event-driven, asynchronous, and non-blocking. "Event" means a user connection. "Asynchronous" means that it handles 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 that case, it works on other events until the I/O is freed up.

Nginx is called event-driven, asynchronous, and non-blocking. "Event" means a user connection. "Asynchronous" means that it processes interactions between multiple connections at the same time. "No blocking" indicates that the disk I/O will not be stopped when the CPU is busy. In this case, Nginx will work on other events until the I/O is released.

Nginx compared with Apache 2.4 MPM

Comparison between Nginx and Apache2.4 MPM

Apache 2.4 events des the MPM event module. it handles some connection types in an 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 also processes some connection types in asynchronous mode like Nginx, but not exactly the same. The goal is to reduce memory requirements as the load increases.

As with earlier versions, Apache 2.4 events des the worker and pre-forked modes we mentioned above but has added the mpm_event_module (Apache MPM event module) to solve the problem of threads that are kept alive waiting for that user connection to make additional requests. MPM dedicates a thread to handle sockets that are both in listening and keep-alive state. this addresses some of the memory issues associated with older versions of Apache, by referencing the number of threads and processes created. to use this, the administrator wowould 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 includes the worker and pre-forked modes mentioned above and also adds the mpm_event_module (Apache MPM event module) to solve the problem that the thread remains alive and waits for the user to connect to process more requests. MPM is dedicated to processing sockets in two statuses: listening and saving connections. This is used to solve some memory problems encountered by earlier Apache versions by reducing the number of threads and processes created. To use this function, the Administrator must download the Apache source code containing the mpm_event_module module to compile it 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 come in (subject to the limit set by the administrator ). nginx does not set up more processes per user connection. the improvement that comes with Apache 2.4 is that those processes that are spin offs create fewer threads than normal Apache worker mode wowould. this is 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, because it still creates new processes for new requests (limited by Administrator settings. Nginx does not create more processes for each user connection. Apache2.4 improves the process by creating fewer threads than the standard Apache worker mode. This is because a thread can process more than one connection, rather than a process required for a connection.

Using Nginx and Apache both

Use both Nginx and Apache

Apache is known for its power and Nginx for speed. This means Nginx can serve up static content quicker, but Apache has des the modules needed to work with back end application servers and run scripting ages.

Apache is famous for its functions and Nginx is famous for its speed. This means that Nginx can provide services to express content more quickly. Apache contains the backend application services and modules required to run the script 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 common approach to take. nginx nodes des 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 are hardware based load balancers.

Apache and Nginx can both be used as proxy servers, but Nginx is usually used as the proxy server, and Apache is used as the backend server. Nginx has Efficient load balancing and caching capabilities. You can also use Apache. Of course, you have to deploy it in more places. To make good use of Server Load balancer, the Server Load balancer is required. Apache has a base hardware Load balancer.

Another configuration is to deploy Nginx with the separate php-fpm application. we say that php-fpm is an application, because it is not. dll or. so loaded at execution time, as is the case with Apache modules. php-fpm (the FastCGI Process Manager) can be used with Nginx to deliver php scripting ability to Nginx to render non-static content.

Another configuration is to deploy Nginx and a separate php-fpm application. We say that php-fpm is an application because it is not a. dll or. so file loaded during execution like the Apache module. Php-fpm (FastCGI Process Manager) and Nginx can be used together to provide PHP scripting capabilities for Nginx so that Nginx can render non-declarative content.

When is Apache preferred over Nginx?

When is Apache better than Nginx?

Apache comes with built in 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 that using CGI or FastCGI, because it does not have to load the Python interpreter for each request. 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 supports PHP, Python, Perl, and other languages. For example, the mod_python and mod_php Apache modules process PHP and Perl code in the Apache process. Mod_python is more efficient than CGI or FastCGI, because it does not have to load the Python interpreter for every request. Similarly, Apache can run Ruby mod_rails and mod_rack on Rails. These processes run faster within the Apache process.

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

Therefore, if your website uses Python or Ruby more, Apache is more beneficial to your applications, because it is not necessary to use CGI. For PHP, it does not matter because Nginx supports PHP internally.

Here we have given some explanation of how Nginx is different from Apache and how you might consider using one or both of them, and which of the two 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.

So far, we have explained the differences between Nginx and Apache and how you choose or use both of them. Which method is closer to your needs. In addition, we also discussed some Nginx improvements brought by Apache2.4 to Apache Web server in thread process management. Now you can make the best solution based on your needs.

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.