仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 702|回复: 8
打印 上一主题 下一主题

[CentOS(社区)] 带来一篇Nginx vs Apache 谁人更好?

[复制链接]
再现理想 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-14 20:17:54 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
如果您觉得本篇CentOSLinux教程讲得好,请记得点击右边漂浮的分享程序,把好文章分享给你的小伙伴们!NginxvsApache
WhatistheNginxwebandproxyserverandhowdoesitcomparetoApache?Shouldyouuseoneoftheseserversorboth?Hereweexploresomeanswerstothesequestions.
Nginxweb办事器和代办署理办事器是甚么,跟Apache对照怎样?你是利用个中一个仍是两个都利用?这里,我们来探究几个对这些成绩的回覆。
TheApachewebservershavebeeninusesince1995.Apachepowersmorewebsitesthananyotherproduct;MicrosoftIIScomesinsecond.
Apacheweb办事器从1995年入手下手被利用,是天下上利用产物最多的web办事器,微博的IIS次之。
Becausetheopen-sourceApachewebserverhasbeenavailableforsomanyyears,andhassomanyusers,lotsofmoduleshavebeenwrittentoexpanditsfunctionality-mostoftheseareopen-sourceaswell.Onepopularconfiguration,forinstance,istouseApachetoserverupstaticwebpagesandthemod_jkmoduletorunJavaandJSPcodeonTomcattomakeaninteractiveapplication.Anotherexampleisusingmod_phptoexecutephpscriptswithouthavingtousecgi.
由于开源的Apacheweb办事器被利用了这么多年,有着这么多的用户,以是有良多的功效扩大模块——并且年夜局部也是开源的。好比个中一个盛行的设置是,利用Apache作为web亮相页的办事器,mod_jk模块运转java,Tomcat运转Jsp代码来搭建一个交互式使用。另外一个实例是在没有利用cgi的情形下利用mod_php模块来实行php剧本。
ButApacheslowsdownunderheavyload,becauseoftheneedtospawnnewprocesses,thusconsumingmorecomputermemory.ItalsocreatesnewthreadsthatmustcompetewithothersforaccesstomemoryandCPU.Apachewillalsorefusenewconnectionswhentrafficreachesthelimitofprocessesconfiguredbytheadministrator.
可是Apache在高负载下会变慢,由于要衍生新的历程,这将损耗更多的盘算机内存。他还创立新的线程,这必需经由过程利用更多的内存和CPU来完成。当流量到达办理设置的历程数下限时,Apache会回绝新的毗连。
NginxisanopensourcewebserverwrittentoaddresssomeoftheperformanceandscalabilityissuesassociatedwithApache.Theproductisopensourceandfree,butNginxofferssupportifyoubuyitsNginxPlusversion.
Nginx恰是一个努力于处置Apache碰到的功能的扩大性成绩的开源web办事。Nginx是一个开源收费的产吕,可是一样撑持你付费购物它的Nginx+版本。
NginxsaysitswebserverwaswrittentoaddresstheC10Kproblem,whichisareferencetoa***writtenbyDanielKegelabouthowtogetonewebservertohandle10,000connections,giventhelimitationsoftheoperatingsystems.Inhis***,hecitedanother***byDeanGaudetwhowrote,“Whydontyouguysuseaselect/eventbasedmodellikeZeus?Itsclearlythefastest”.
Nginx说他的webserver是用于办理在DanielKegel所写的“怎样在操纵体系限定下让一个webserver处置10000个毗连”的论文中提起的C10K成绩的。在这篇论文中他提援用了另外一篇DeanGaudet写的论文——“伴计们为啥不必一个像Zeus一样的基于选择或事务的模子?这分明快良多啊”。
Nginxisindeedevent-based.Theycalltheirarchitecture“event-drivenandasynchronous”.Apachereliesonprocessesandthreads.So,what’sthedifference?
Nginx的确是基于事务的。他们称他们这个架构为“事务驱动和异步”。Apache则依附历程和线程。那末,这有何分歧?
HowApacheworksandwhyithaslimitations
Apache是怎样事情的又是为什么有范围性

Apachecreatesprocessesandthreadstohandleadditionalconnections.Theadministratorcanconfiguretheservertocontrolthemaximumnumberofallowableprocesses.Thisconfigurationvariesdependingontheavailablememoryonthemachine.Toomanyprocessesexhaustmemoryandcancausethemachinetoswapmemorytodisk,severelydegradingperformance.Plus,whenthelimitofprocessesisreached,Apacherefusesadditionalconnections.
Apache经由过程创立历程和线程来处置分外的毗连。***能够经由过程设置办事器来把持最年夜同意历程毗连数。这一项设置的巨细取决于办事器主机可用内存巨细。过量的历程耗尽内存而且招致主机互换硬盘为内存,严峻地下降了功能。另有,当历程数到达限定后,Apache就回绝分外的毗连哀求。
Apachecanbeconfiguredtoruninpre-forkedorworkermulti-processmode(MPM).Eitherwayitcreatesnewprocessesasadditionalusersconnect.Thedifferencebetweenthetwoisthatpre-forkedmodecreatesonethreadperprocess,eachofwhichhandlesoneuserrequest.Workermodecreatesnewprocessestoo,buteachhasmorethanonethread,eachofwhichhandlesonerequestperuser.Sooneworkermodeprocesshandlesmorethanoneconnectionandonepre-forkmodeprocesshandlesoneconnectiononly.
Apache也能够设置为运转pre-forked或多历程Worker形式(MPM)。二者都能为分外用户毗连创立新的历程。二者分歧在于,pre-forked形式为每个历程创立一个线程,且每个线程处置一个用户哀求。Worker形式也创立新的历程,可是一个历程创立不止一个线程,每一个线程处置一个用户的一个哀求。以是一个Worker形式的历程处置不止一个毗连,而一个预fork形式的历程只处置一个毗连。
Workermodeuseslessmemorythanforked-mode,becauseprocessesconsumemorememorythanthreads,whicharenothingmorethancoderunninginsideaprocess.
Worker形式比forked形式损耗的内存更少,由于历程比线程损耗更多的内存,没有甚么比代码跑在一个线程里更损耗内存的。
Moreover,workermodeisnotthreadsafe.Thatmeansifyouusenonthread-safemoduleslikemod_php,toserveupphppages,youneedtousepre-forkedmode,thusconsumingmorememory.So,whenchoosingmodulesandconfigurationyouhavetoconfrontthethread-versus-processoptimizationproblemandconstraintissues.
别的,worker事情不是线程宁静的。这意味着假如你要利用像mod_php如许线程宁静的模块往跑php页面的话,你必要利用pre-forked形式,这将会损耗更多的内存。以是,要选择模块和设置,你必需面临线程与历程的优化成绩和范围成绩。
ThelimitingfactorintuningApacheismemoryandthepotentialtodead-lockedthreadsthatarecontendingforthesameCPUandmemory.Ifathreadisstopped,theuserwaitsforthewebpagetoappear,untiltheprocessmakesitfree,soitcansendbackthepage.Ifathreadisdeadlocked,itdoesnotknowhowtorestart,thusremainingstuck.
调试Apache的限定要素是不异的CPU和内存中内存和潜伏的线程逝世锁的抢占。假如一个线程中断了,用户等着网页呈现,直到历程开释以后,它才干够发还页面。假如一个线程逝世锁了,不会主动重启,这个招致卡逝世了。
Nginx
NginxworksdifferentlythanApache,mainlywithregardtohowithandlesthreads.
Nginx与Apache事情纷歧样的中央,次要是关于怎样处置线程。
Nginxdoesnotcreatenewprocessesforeachwebrequest,insteadtheadministratorconfigureshowmanyworkerprocessestocreateforthemainNginxprocess.(OneruleofthumbistohaveoneworkerprocessforeachCPU.)Eachoftheseprocessesissingle-threaded.Eachworkercanhandlethousandsofconcurrentconnections.Itdoesthisasynchronouslywithonethread,ratherthanusingmulti-threadedprogramming.
Nginx不会为每个收集哀求都创立一个新历程,取而代之的是***设置分派几事情历程作为Nginx主历程(一个履历是有几CPU就建几个workerprocess。)。这些历程都是单线程的。每个worker能够处置数以千计的并发毗连。它是一个线程异步的,而不是多线程步伐。
TheNginxalsospinsoffcacheloaderandcachemanagerprocessestoreaddatafromdiskandloaditintothecacheandexpireitfromthecachewhendirected.
Nginx还轮回地经由过程缓存加载器弛缓存办理器从硬盘中读数据并加载到缓存中而且针对地从缓存中开释。
Nginxiscomposedofmodulesthatareincludedatcompiletime.Thatmeanstheuserdownloadsthesourcecodeandselectswhichmodulestocompile.Therearemodulesforconnectiontobackendapplicationservers,loadbalancing,proxyserver,andothers.ThereisnomoduleforPHP,asNginxcancompilePHPcodeitself.
Nginx编译时包括的模块构成。这意味着用户能够下载源码而且选择必要的模块举行编译。有些模块撑持毗连后端使用办事,负载平衡,代办署理办事器等等。没有撑持PHP的模块,由于Nginx本人能够编译PHP代码。
HereisadiagramoftheNginxarchitecturetakenfromAndrewAlexeev
分手快乐 该用户已被删除
沙发
发表于 2015-1-15 18:38:38 | 只看该作者

带来一篇Nginx vs Apache 谁人更好?

哎呀,你太牛了,我好崇拜你!
若天明 该用户已被删除
板凳
发表于 2015-1-20 05:15:33 | 只看该作者
上课传授的不仅仅是知识,更重要的是一些道理,包括一些做人的道理,讲课时也抓住重点,循序渐进,让同学理解很快;更可贵的是不以你过去的成绩看问题.
地板
发表于 2015-1-28 19:27:16 | 只看该作者
众所周知,目前windows操作系统是主流,在以后相当长的时间内不会有太大的改变,其方便友好的图形界面吸引了众多的用户。
再见西城 该用户已被删除
5#
发表于 2015-2-5 23:28:53 | 只看该作者
我感觉linux的学习,学习编程~!~!就去学习C语言编程!!
老尸 该用户已被删除
6#
发表于 2015-2-14 08:08:37 | 只看该作者
选择交流平台,如QQ群,网站论坛等。
小女巫 该用户已被删除
7#
发表于 2015-3-11 17:57:24 | 只看该作者
学习Linux半年了~个人认为不会的多在网上找资料网上有很多资料可以搜索到,LS那位说放手去搞。
山那边是海 该用户已被删除
8#
发表于 2015-3-19 08:13:10 | 只看该作者
Linux高手更具有鼓励新手的文化精神。如何在Linux社区获得帮助,需要说明的是你要有周全的思考,准备好你的问题,不要草率的发问。
再现理想 该用户已被删除
9#
 楼主| 发表于 2015-3-27 17:11:20 | 只看该作者
Linux最大的特点就是其开源性,这一点是十分难得的,这也是它能够存在到现在的原因之一。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-4-29 18:11

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表