Mongrel2服务器配置及控制

云端轻躺 2011-08-10

介绍

Mongrel2是一款面向现代Web应用(Web2.0以及以后)而设计的Web浏览器。由干净的C语言写成,(目前)在Linux(及类似的Unix)系统上运行。它支持HTTP,Flashsockets,WebSockets,LongPolling,异步等技术,基本上走在当今Web服务端技术的最前沿。

主页

http://mongrel2.org/

手册

http://mongrel2.org/static/mongrel2-manual.html

Mongrel2的理念

按前面的描述,Mongrel2听起来是个很牛X的Web服务器,其实它真正独特的地方还不是前面的描述,而是它的理念:

  1. Web服务框架。它提供了很多基础设施,封装了大量Web处理方面的细节,并且以消息的方式为服务器更里层的应用提供多种服务;
  2. 语言中立。Mongrel2本身是用C语言写成的,但是他力求做到提供的服务语言中立(包括内部协议的设计),理论上可以为任何语言使用(只要这种语言能处理字符串)。实际上,目前已经有17种语言为它写了接口模块,所以可以说到现在为止他已经实现这个目标;
  3. 多样灵活的控制接口,为管理员的精确控制提供了方便(甚至可以精确到控制一个持续性连接);

关于语言中立,我们来八卦一下。Mongrel2的作者ZedShaw,之前是Ruby社区的一位大牛,为Ruby社区设计了Mongrel服务器(针对Ruby设计的),这是个广受好评的WebServer,效率差不多是Ruby社区最高的了。后来,不知道咋回事儿(可能是关于一些Ruby技术的发展观点的分歧),ZedShaw与Ruby界的其它某些人闹翻了。于是他一怒之下退出了Ruby圈子,发誓再也不搞Ruby了。退出之后,兴趣转向了python,写了一本书LearnPython:TheHardWay。2010年开始,带着对Ruby界的愤怒(也许是很受伤),他开始了Mongrel2的设计。Mongrel2与Mongrel理念完全不一样,Mongrel2的目标就是要语言中立,并且宣称未来的软件开发一定是语言无关的(这种观念我蛮赞同)。就这么一年多的时间,到现在为止(2011年8月10日),发展到了1.7.5版,影响力逐渐大了起来。

Mongrel2与ZeroMQ社区也很有渊源——Mongrel2是ZeroMQ的第一个比较正式的有影响力的项目。ZeroMQ宣称是最牛逼的消息队列,是个新东西,可一直没有什么有影响力的应用。这次与Mongrel2的联姻,简直就是天作之合,我看行!

我关注Mongrel2,还是在其Tir示意框架出来之后,那时Mongrel2的版本为1.4。当时我正在估摸着设计一款Lua的Web框架,用于我们以后的产品中。发现Tir后,立即对这一套系统进行了消化,觉得Mongrel2,ZeroMQ,Redis,Lua的配合绝对有戏,重要的是在Mongrel2的基础上开发Web框架,能省很多事情(Mongrel2把许多以许需要框架来完成的事情包装好了)。于是立马开始了Bamboo项目。

配置文件写法

对象

  1. Server
  2. Host
  3. Dir
  4. Proxy

内置变量

  • settings
  • servers

配置选项

先贴上,后面再翻译。

Thelistofavailablesettingsare:

control_port=ipc://run/control

ThisiswhereMongrel2willlistenwith0MQforcontrolmessages.Youshoulduseipc://forthespec,sothatonlyalocaluserwithfileaccesscangetatit.

limits.buffer_size=2*1024

InternalIObuffers,usedforthingslikeproxyingandhandlingrequests.Thisisaveryconservativesetting,soifyougetHTTPheadersgreaterthanthis,you’llwanttoincreasethissetting.You’llalsowanttoshootwhoeverissendingyouthoserequests,becausetheaverageis400-600bytes.

limits.client_read_retries=5

HowmanytimesitwillattempttoreadacompleteHTTPheaderfromaclient.Thispreventsattackswhereaclienttricklesanincompleterequestatyouuntilyourunoutofresources.

limits.connection_stack_size=32*1024

Sizeofthestackusedforconnectioncoroutines.Ifyou’retryingtocramatonofconnectionsintoverylittleRAM,seehowlowthiscango.

limits.content_length=20*1024

Maximumallowedcontentlengthonsubmittedrequests.Thisis,rightnow,ahardlimitsorequeststhatgooveritarerejected.LaterversionsofMongrel2willuseanuploadmechanismthatwillallowanysizeupload.

limits.dir_max_path=256

MaxpathlengthyoucansetforDirhandlers.

limits.dir_send_buffer=16*1024

Maximumbufferusedforfilesendingwhenweneedtouseone.

limits.fdtask_stack=100*1024

StackframesizeforthemainIOreactortask.There’sonlyone,sosetithighifyoucan,butitcouldpossiblygolower.

limits.handler_stack=100*1024

ThestackframesizeforanyHandlertasks.Youprobablywantthishigh,sincethere’snotmanyofthese,butadjustandseewhatyoursystemcanhandle.

limits.handler_targets=128

ThemaximumnumberofconnectionIDsamessagefromaHandlermaytarget.It’snotsmarttosetthisreallyhigh.

limits.header_count=128*10

Maximumnumberofallowedheadersfromaclientconnection.

limits.host_name=256

MaximumhostnameforHostspecifiersandotherDNSrelatedsettings.

limits.mime_ext_len=128

MaximumlengthofMIMEtypeextensions.

limits.proxy_read_retries=100

ThenumberofreadattemptsMongrel2shouldmakewhenreadingfromabackendproxy.Manybackendserversdon’tbuffertheirI/OproperlyandMongrel2willditchtheirHTTPresponseifitdoesn’tgetaheaderafterthismanyattempts.

limits.proxy_read_retry_warn=10

Thisisthethresholdwhereyougetawarningthataparticularbackendishavingperformanceproblems,usefulforspottingpotentialerrorsbeforetheybecomeaproblem.

limits.url_path=256

MaxURLpaths.Doesnotincludequerystring,justpath.

superpoll.hot_dividend=4

Ratioofthetotal(like1/4th,1/8th)thatshouldbeinthehotselection.Setthishigherifyouhavelotsofidleconnections;setitlowerifyouhavemoreactiveconnections.

superpoll.max_fd=10*1024

Maximumpossibleopenfiles.Donotsetthisabove64*1024,andexpectittotakeabitwhileMongrel2setsupconstantstructures.

upload.temp_store=None

Thisisnotsetbydefault.Ifyouwantlargerequeststoreachyourhandlers,thensetthistoadirectorytheycanaccess,andmakesuretheycanhandleit.ReadaboutitintheHackingsectionunderUploads.ThefilehastoendinXXXXXXcharstowork(readmanmkstemp).

upload.temp_store_mode=0666

Themodetochmodanyfilesuploadedtoupload.temp_store.

zeromq.threads=1

Numberof0MQIOthreadstorun.Careful,we’veexperiencedthreadbugsin0MQsometimeswithhighnumbersofthese.

limits.tick_timer=10

Mongrel2keepsaninternalclockforefficiencyandtorunthetimeouts.Thisishowoftenthatclockupdates,anddefaultsto10seconds.

limits.min_ping=120

Minimumtimesincelastactivitybeforeconsideringclosingasocket.Setto0todisableit.

limits.min_write_rate=300

Minimumbytes/secondwrittenbeforeconsideringclosingasocket.Setto0todisableit.

limits.min_read_rate=300

Minimumbytes/secondreadbeforeconsideringclosingasocket.Setto0todisableit.

limits.kill_limit=2

Howmanyofmin_ping,min_write_rate,andmin_read_ratehavetotriggerbeforeasocketiskilled.

m2sh管理工具

Mongrel2的源码包里面自带一个工具m2sh,可以用来方便地从命令行管理Mongrel2。

在命令行中敲入m2sh,回车。会进入下面的提示符:

==============================================

mongrel2>help

Mongrel2m2shhasthesecommandsavailable:

loadLoadaconfig.

configAliasforload.

shellStartsaninteractiveshell.

accessPrintstheaccesslog.

serversListstheserversinaconfigdatabase.

hostsListsthehostsinaserver.

routesListstheroutesinahost.

commitAddsamessagetothelog.

logPrintsthecommitlog.

startStartsaserver.

stopStopsaserver.

reloadReloadsaserver.

runningTellsyouwhat'srunning.

controlConnectstothecontrolport.

versionPrintstheMongrel2andm2shversion.

helpGethelp,listscommands.

uuidPrintsoutarandomlygeneratedUUID.

==============================================

m2shservers--dbconf/config.sqlite

SERVERS:

------

mainipaste505417b8-1de4-454f-98b6-07eb9225cca1

server2tfzhw505417b8-1de4-454f-98b6-07eb9225cca2

==============================================

mongrel2>hosts--dbconf/config.sqlite-servername

HOSTSinname:

-----

[ERROR](errno:Nosuchfileordirectory)Invalidquery,ithasnocolumns,whichisabug.

mongrel2>hosts--dbconf/config.sqlite-servermain

HOSTSinmain:

-----

1ipaste

==============================================

mongrel2>routes--dbconf/config.sqlite-servermain-hostipaste

ROUTESinhostipaste,servermain

-----

/

/media/

/favicon.ico

==============================================

mongrel2>log--dbconf/config.sqlite

LOGMESSAGES:

------

2011-01-1205:41:34rootlegerobot-officeconf/mongrel2.confloadcommand

2011-01-1806:26:54rootlegerobot-officeconf/mongrel2.confloadcommand

2011-01-1806:29:54rootlegerobot-officeconf/mongrel2.confloadcommand

2011-01-1806:46:34rootlegerobot-officeconf/mongrel2.confloadcommand

2011-01-1806:47:15rootlegerobot-officeconf/mongrel2.confloadcommand

2011-01-1806:51:39rootlegerobot-officeconf/mongrel2.confloadcommand

2011-01-2707:47:56rootlegerobot-officeconf/mongrel2.confloadcommand

==============================================

mongrel2>access--dbconf/config.sqlite

[WARN](errno:Nosuchfileordirectory)Nooption--loggiven,using"logs/access.log"asthedefault.

[1312945845]192.168.0.101:2633ipaste"GET/get/HTTP/1.1"2000

[1312945874]192.168.0.101:2634ipaste"GET/HTTP/1.1"2000

[1312945874]192.168.0.101:2634ipaste"GET/media/js/jquery.min.jsHTTP/1.1"3040

[1312945874]192.168.0.101:2635ipaste"GET/media/css/fileuploader.cssHTTP/1.1"3040

[1312945874]192.168.0.101:2636ipaste"GET/media/js/fileuploader.jsHTTP/1.1"3040

[1312945874]192.168.0.101:2637ipaste"GET/get/HTTP/1.1"2000

[1312945875]192.168.0.101:2639ipaste"GET/HTTP/1.1"2000

[1312945875]192.168.0.101:2639ipaste"GET/media/js/jquery.min.jsHTTP/1.1"3040

[1312945875]192.168.0.101:2640ipaste"GET/media/css/fileuploader.cssHTTP/1.1"3040

[1312945875]192.168.0.101:2641ipaste"GET/media/js/fileuploader.jsHTTP/1.1"3040

[1312945875]192.168.0.101:2642ipaste"GET/get/HTTP/1.1"2000

[1312945879]192.168.0.101:2644ipaste"GET/get/HTTP/1.1"2000

==============================================

mongrel2>stop--dbconf/config.sqlite-namemain

mongrel2>servers--dbconf/config.sqlite

SERVERS:

------

mainipaste505417b8-1de4-454f-98b6-07eb9225cca1

server2tfzhw505417b8-1de4-454f-98b6-07eb9225cca2

mongrel2>running--dbconf/config.sqlite-namemain

mongrel2atPID2854running.

mongrel2>running--dbconf/config.sqlite-namemain

mongrel2atPID3132running.

mongrel2>running--dbconf/config.sqlite-nameserver2

mongrel2atPID3191running.

mongrel2>running--dbconf/config.sqlite-namemain

[ERROR](errno:Nosuchfileordirectory)Couldn'treadthePIDfrom.//run/mongrel2.pid

mongrel2isnotrunningbecausepid_fileisn'tthere.

==============================================

mongrel2>start--dbconf/config.sqlite-namemain

[INFO](src/mime.c:49)MAXlimits.mime_ext_len=128

[INFO](src/host.c:62)MAXlimits.url_path=256,limits.host_name=256

[INFO](src/handler.c:338)MAXlimits.handler_stack=102400

[INFO](src/config/config.c:84)Loadedhandler1:tcp://127.0.0.1:9999:e884a439-31be-4f74-8050-a93565795b25:tcp://127.0.0.1:9998:

[INFO](src/config/config.c:226)Loadedroute1:/:handlerforhost1:ipaste

[INFO](src/dir.c:175)MAXlimits.dir_send_buffer=16384,limits.dir_max_path=256

[INFO](src/config/config.c:135)Loadeddirectory1:sites/ipaste/:index.html

[INFO](src/config/config.c:226)Loadedroute2:/media/:dirforhost1:ipaste

[INFO](src/config/config.c:135)Loadeddirectory1:sites/ipaste/:index.html

[INFO](src/config/config.c:226)Loadedroute3:/favicon.ico:dirforhost1:ipaste

[INFO](src/config/config.c:274)Loaded1hostsforserver1:505417b8-1de4-454f-98b6-07eb9225cca1

[INFO](src/server.c:416)LOADINGHandlertcp://127.0.0.1:9999

[INFO](src/superpoll.c:285)MAXopenfiledescriptorsis10240now.

[ERROR](src/unixy.c:99:errno:Nosuchfileordirectory)FailedtoopenPIDfile.//run/mongrel2.pidforreading.

[INFO](src/unixy.c:138)NopreviousMongrel2running,continuingon.

[INFO](src/mongrel2.c:200)Allloadedup,timetoturnintoaserver.

mongrel2>start--dbconf/config.sqlite-nameserver2

[INFO](src/mime.c:49)MAXlimits.mime_ext_len=128

[INFO](src/host.c:62)MAXlimits.url_path=256,limits.host_name=256

[INFO](src/handler.c:338)MAXlimits.handler_stack=102400

[INFO](src/config/config.c:84)Loadedhandler2:tcp://127.0.0.1:9997:e884a439-31be-4f74-8050-a93565795b24:tcp://127.0.0.1:9996:

[INFO](src/config/config.c:226)Loadedroute4:/:handlerforhost2:tfzhw

[INFO](src/dir.c:175)MAXlimits.dir_send_buffer=16384,limits.dir_max_path=256

[INFO](src/config/config.c:135)Loadeddirectory2:sites/tfzhw/:index.html

[INFO](src/config/config.c:226)Loadedroute5:/media/:dirforhost2:tfzhw

[INFO](src/config/config.c:135)Loadeddirectory2:sites/tfzhw/:index.html

[INFO](src/config/config.c:226)Loadedroute6:/favicon.ico:dirforhost2:tfzhw

[INFO](src/config/config.c:274)Loaded1hostsforserver2:505417b8-1de4-454f-98b6-07eb9225cca2

[INFO](src/server.c:416)LOADINGHandlertcp://127.0.0.1:9997

[INFO](src/superpoll.c:285)MAXopenfiledescriptorsis10240now.

[ERROR](src/unixy.c:99:errno:Nosuchfileordirectory)FailedtoopenPIDfile.//run/mongrel2_server2.pidforreading.

[INFO](src/unixy.c:138)NopreviousMongrel2running,continuingon.

[INFO](src/mongrel2.c:200)Allloadedup,timetoturnintoaserver.

==============================================

运行期控制

在m2sh提示符下,执行下面语句,会进行远程控制提示符

mongrel2>control--dbconf/config.sqlite-nameserver2

[INFO]Connectingtocontrolportipc://.//run/control

==============================================

m2[server2]>help

namehelp

stopstoptheserver(SIGINT)

reloadreloadtheserver

helpthiscommand

control_stopstopcontrolport

killkillaconnection

statusstatus,what=['net'|'tasks']

terminateterminatetheserver(SIGTERM)

timetheserver'stime

uuidtheserver'suuid

infoinformationaboutthisserver

==============================================

m2[server2]>statuswhat=net

idfdtypelast_pinglast_readlast_writebytes_readbytes_written

m2[server2]>statuswhat=tasks

idsystemnamestatestatus

1falseSERVERreadfdidle

2falseHandler_taskreadhandleridle

3falsecontrolreadhandlerrunning

4falsetickeridle

5truefdtaskyieldready

==============================================

m2[server2]>time

time:1312946823

==============================================

m2[server2]>uuid

uuid:505417b8-1de4-454f-98b6-07eb9225cca2

==============================================

m2[server2]>info

port:6768

bind_addr:0.0.0.0

uuid:505417b8-1de4-454f-98b6-07eb9225cca2

chroot:./

access_log:/logs/access_server2.log

error_log:/logs/error_server2.log

pid_file:/run/mongrel2_server2.pid

default_hostname:tfzhw

==============================================

m2[main]>statuswhat=net

idfdtypelast_pinglast_readlast_writebytes_readbytes_written

16361717171896176

937171717120525019

173916071711358117

1441171717121115019

m2[main]>killid=16

status:OK

m2[main]>killid=9

status:OK

m2[main]>killid=17

status:OK

m2[main]>killid=14

status:OK

m2[main]>statuswhat=net

idfdtypelast_pinglast_readlast_writebytes_readbytes_written

18301313104480

19311111104790

20361111106790

21371111104870

==============================================

注:使用类似m2shstart--dbconf/config.sqlite-namemain之类的命令也是可以的。

相关推荐

bjkamui / 0评论 2019-11-04
MingoJiang / 0评论 2019-10-29