標籤:nginx apache web server web伺服器
原文地址:https://anturis.com/blog/nginx-vs-apache/
Nginx vs Apache
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.
Nginx web伺服器和Proxy 伺服器是什麼,跟Apache 對比如何?你是使用其中一個還是兩個都使用?這裡,我們來探索幾個對這些問題的回答。
The Apache web servers have been in use since 1995. Apache powers more websites than any other product; Microsoft IIS comes in second.
Apache 網頁伺服器從1995年開始被使用,是世界上使用產品最多的web伺服器,微博的IIS次之。
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表態頁的伺服器,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通過建立進程和線程來處理額外的串連。管理員可以通過設定管理員來控制最大允許進程串連數。這一項配置的大小取決於伺服器主機可用記憶體大小。過多的進程耗盡記憶體並且導致主機交換硬碟為記憶體,嚴重地降低了效能。還有,當進程數達到限制後,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編譯時間包含的模組組成。這意味著使用者可以下載源碼並且選擇需要的模組進行編譯。有些模組支援串連後端應用服務,負載平衡,Proxy 伺服器等等。沒有支援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都可以用作Proxy 伺服器,但通常使用Nginx作為Proxy 伺服器,Apache作為後端伺服器。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線上程進程管理中的改進。現在你可以根據您的需求做出最好的解決方案了。
Nginx vs Apache