Nginx vs Apache 那个更好?

分类:CentOS运维 阅读:97511 次

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服务器和代理服务器是什么,跟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 web 服务器从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 web 服务器被使用了这么多年,有着这么多的用户,所以有很多的功能扩展模块——而且大部分也是开源的。比如其中一个流行的配置是,使用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 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说他的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编译时包含的模块组成。这意味着用户可以下载源码并且选择需要的模块进行编译。有些模块支持连接后端应用服务,负载均衡,代理服务器等等。没有支持PHP的模块,因为Nginx自己可以编译PHP代码。

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

下面是一张从 Andrew Alexeev 的Nginx工作方式深度分析一文中取出来的Nginx架构图:

Nginx vs Apache 那个更好?
n 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都可以用作代理服务器,但通常使用Nginx作为代理服务器,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在线程进程管理中的改进。现在你可以根据您的需求做出最好的解决方案了。