Nginx vs Apache

原文地址:https://anturis.com/blog/nginx-vs-apache/php

Nginx vs Apachehtml

What is 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.java

Nginx webserver和代理server是什麼。跟Apache 對照方何?你是使用當中一個仍是兩個都使用?這裏,咱們來探索幾個對這些問題的回答。python

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

Apache web server從1995年開始被使用。是世界上使用產品最多的webserver,微博的IIS次之。web

Because the open-source Apache web server has been available for so many years, and has so many 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.apache

因爲開源的Apache web server被使用了這麼多年。有着這麼多的用戶,因此有很是多的功能擴展模塊——而且大部分也是開源的。後端

比方當中一個流行的配置是,使用Apache做爲web表態頁的server。mod_jk 模塊執行java,Tomcat執行Jsp 代碼來搭建一個交互式應用。緩存

還有一個實例是在沒有使用cgi 的狀況下使用mod_php 模塊來執行php腳本。安全

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.

但是Apache在高負載下會變慢,因爲要衍生新的進程,這將消耗不少其它的計算機內存。他還建立新的線程。這必須經過使用不少其它的內存和CPU來完畢。當流量達到管理配置的進程數上限時。Apache會拒絕新的鏈接。

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 正是一個致力於處理Apache遇到的性能的擴展性問題的開源web 服務。Nginx是一個開源免費的產呂,但是相同支持你付費購物它的Nginx+版本號。

Nginx says its web server was written to address the C10K problem, which is a reference to a paper written 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說他的web server 是用於解決在Daniel Kegel所寫的「怎樣在操做系統限制下讓一個web server處理10000個鏈接」的論文中提起的C10K 問題的。在這篇論文中他提引用了還有一篇Dean Gaudet寫的論文——「夥計們爲啥不用一個像Zeus同樣的基於選擇或事件的模型?這明顯快很是多啊」。

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 確實是基於事件的。他們稱他們這個架構爲「事件驅動和異步」。

Apache則依賴進程和線程。那麼,這有何不一樣?

How Apache works and why it has limitations

Apache是怎樣工做的又是爲什麼有侷限性


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 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經過建立進程和線程來處理額外的鏈接。管理員可以經過配置server來控制最大讚成進程鏈接數。這一項配置的大小取決於server主機可用內存大小。

過多的進程耗盡內存並且致使主機交換硬盤爲內存,嚴重地減小了性能。還有,當進程數達到限制後,Apache就拒絕額外的鏈接請求。

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也可以配置爲執行pre-forked 或多進程Worker模式(MPM)。

二者都能爲額外用戶鏈接建立新的進程。二者不一樣在於,pre-forked 模式爲每一個進程建立一個線程。且每一個線程處理一個用戶請求。Worker 模式也建立新的進程。但是一個進程建立不止一個線程。每一個線程處理一個用戶的一個請求。

因此一個Worker模式的進程處理不止一個鏈接,而一個預fork模式的進程僅僅處理一個鏈接。

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.

Worker模式比forked模式消耗的內存更少,因爲進程比線程消耗不少其它的內存,沒有什麼比代碼跑在一個進程裏更消耗內存的。

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.

另外 ,worker工做不是線程安全的。這意味着假設你要使用像mod_php這樣線程安全的模塊去跑php頁面的話,你需要使用pre-forked模式,這將會消耗不少其它的內存。因此。要選擇模塊和配置,你必須面對線程與進程的優化問題和侷限問題。

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.

調試Apache的限制因素是一樣的CPU和內存中內存和潛在的線程死鎖的搶佔。

假設一個線程中止了,用戶等着網頁出現,直到進程釋放以後,它才能夠發回頁面。假設一個線程死鎖了。不會本身主動從新啓動。這個致使卡死了。

Nginx

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

Nginx 與Apache工做不同的地方,主要是關於怎樣處理線程。

Nginx does not create new processes for each web request, instead the administrator configures how many 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不會爲每一個網絡請求都建立一個新進程。取而代之的是管理員配置分配多少工做進程做爲Nginx主進程(一個經驗是有多少CPU就建多少個worker process。)。這些進程都是單線程的。每一個worker 可以處理數以千計的併發鏈接。

它是一個線程異步的。而不是多線程程序。

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還循環地經過緩存載入器和緩存管理器從硬盤中讀數據並載入到緩存中並且針對地從緩存中釋放。

Nginx is composed of modules that are included 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.

Nginx編譯時包括的模塊組成。

這意味着用戶可以下載源代碼並且選擇需要的模塊進行編譯。有些模塊支持鏈接後端應用服務,負載均衡,代理server等等。沒有支持PHP的模塊,因爲Nginx本身可以編譯PHP代碼。

Here is a diagram of the Nginx architecture taken from Andrew Alexeev´s deep analysis of Nginx and how it works.

如下是一張從 Andrew Alexeev 的Nginx工做方式深度分析一文中取出來的Nginx架構圖:


In this diagram we 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 take advantage.

上圖中的實例中。咱們看到。Nginx是使用FastCGI進程來運行Python, Ruby和其它代碼,並且使用Memcache內存對象緩存系統的。工做進程載入主ht_core Nginx過程當中的HTTP請求。咱們相同能看到Nginx是牢牢集成在Windows和Linux內核特性上來獲取高性能的;這些特性改隨着時間的推移獲得改善,讓Nginx能被充分利用。

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被稱爲事件驅動的,異步的。無堵塞的。「事件」意思是一個用戶鏈接。「異步」的意思是它同一時候處理多個鏈接的交互。「無堵塞」表示CPU忙碌的時候不會中止磁盤I/O,在這樣的狀況下,Nginx將工做在其它事件上。直到I/O獲得釋放。

Nginx compared with Apache 2.4 MPM

Nginx與Apache2.4 MPM 對照

Apache 2.4 includes 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 包括了MPM事件模塊。

它處理一些鏈接類型也跟Nginx同樣是用異步的方式,但不是全然同樣的方式。目標是隨着負載的添加下降內存的需求。

As with earlier versions, Apache 2.4 includes 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 reducing the number of threads and processes created. To use this, 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.

如同較早的版本號同樣,Apache2.4包括了前文說起的worker和pre-forked模式而且還添加了mpm_event_module(Apache MPM 事件模塊)來解決線程保持存活來等待用戶鏈接的問題以處理不少其它的請求。MPM致力於一個線程處理監聽和保存鏈接兩種狀態下的套接字。這用於經過下降線程和進程的建立數來解決一些舊版Apache遇到的內存問題。要使用這個功能。管理員必須下載包括mpm_event_module 模塊的Apache 源代碼來編譯以替代二進制發行版。

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 would. This is because one thread can handle more than one connection, instead of requiring a new process for each connection.

Apache MPM 事件模塊也不是全然跟Nginx同樣的,因爲它仍爲新進的請求(受限於管理員設置)建立新的進程。Nginx不會爲每一個用戶鏈接建立不少其它的進程。Apache2.4帶來的提高是進程將比標準的Apache worker模式建立更少的線程。

這是因爲一個線程可以處理不止一個的鏈接了,而不是一個鏈接需要一個進程。

Using Nginx and Apache both

同一時候使用Nginx和Apache

Apache is known for its 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 application servers and run scripting languages.

Apache以功能聞名而Nginx以速度聞名。這意味着Nginx能更快的爲表態內容提供服務,而Apache包括了後端應用服務和執行腳本語言需要到的模塊。

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 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 are hardware based load balancers.

Apache和Nginx都可以用做代理server,但一般使用Nginx做爲代理server。Apache做爲後端server。Nginx擁有高效的負載均衡和緩存能力。

Apache也可以,固然了。得部署在不少其它的地方。爲了利用好負載均衡,負載均衡器是需要的。Apache有一個基硬件的負載均衡器。

Another configuration is to deploy Nginx with the separate php-fpm application. We say that php-fpm is an application, because it is not a .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.

還有一種配置是將Nginx和單獨的 php-fpm 應用一塊兒部署。咱們說php-fpm 是一個應用程序,是因爲他不是一個像 Apache 模塊那樣在運行時載入的 .dll 或者 .so文件。Php-fpm (FastCGI 進程管理器)與Nginx一塊兒使用能爲Nginx提供PHP腳本語言能力讓Nginx可以渲染非表態內容。

When is Apache preferred over Nginx?

Apache何時比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天生支持PHP,Python,Perl和其它一些語言。好比 mod_python 和 mod_php 兩個Apache模塊在Apache進程中處理PHP和Perl代碼。mod_python 比使用CGI 或者FastCGI更高效,因爲它沒必要爲每一個請求載入Python解釋器。

相同的還有可以讓Apache在Rails上執行Ruby 的 mod_rails 和 mod_rack。

這些過程在Apache進程內部執行得更快。

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.

所以假設你的站點 使用Python或Ruby不少其它的話。Apache更故意於你的應用程序,因爲不是非用CGI不可的。對於PHP來講的話就沒有關係因爲Nginx內部支持PHP。

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.

至此咱們已經就Nginx與Apache的不一樣之處以及您怎樣選用或者怎樣同一時候使用兩者,哪種方式更接近您的需求給出了一些解釋了。

另外咱們還討論了Apache2.4爲Apache Web server 帶來的一些Nginx在線程進程管理中的改進。

現在你可以依據您的需求作出最好的解決方式了。

相關文章
相關標籤/搜索