• Nginx vs Apache


    原文地址: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 webserver和代理server是什么。跟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 server从1995年開始被使用。是世界上使用产品最多的webserver,微博的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 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在线程进程管理中的改进。

    如今你能够依据您的需求做出最好的解决方式了。

  • 相关阅读:
    一个不错的时间日期控件My97DatePicker
    实现dropDownList listBox联动
    删除字符串最后一个字符的几种方法
    Visual Studio 2008 Express 中文版下载
    DotNet上传大文件
    asp.net下获取Excel所有的工作表名称
    无法访问http,会强制跳到https
    循环
    P7516[省选联考2021A/B卷]图函数【bfs】
    AT2371[AGC013E]Placing Squares【矩阵乘法】
  • 原文地址:https://www.cnblogs.com/tlnshuju/p/6825436.html
Copyright © 2020-2023  润新知