Lighttpd是另外一種低內(nèi)存占用率服務(wù)器,其性能卓異,而且穩(wěn)定、靈活、高速。Lighttpd由Jan Kneschke開(kāi)發(fā),2003年發(fā)布。其CPU負(fù)載低,一大特色是應(yīng)用了速度優(yōu)化技術(shù)。Lighttpd對(duì)于那些飽受負(fù)載過(guò)高之苦的地方而言無(wú)異于雪中送炭。根據(jù)Netcraft公司2011年三月市場(chǎng)調(diào)查顯示,Lighttpd目前排名第五,是YouTube、The Pirate Bay和Meebo等網(wǎng)站的首選網(wǎng)絡(luò)服務(wù)器。
Lighttpd 是一個(gè)德國(guó)人領(lǐng)導(dǎo)的開(kāi)源Web服務(wù)器軟件,其根本的目的是提供一個(gè)專門(mén)針對(duì)高性能網(wǎng)站,安全、快速、兼容性好并且靈活的web server環(huán)境。具有非常低的內(nèi)存開(kāi)銷、cpu占用率低、效能好以及豐富的模塊等特點(diǎn)。
Lighttpd是眾多OpenSource輕量級(jí)的web server中較為優(yōu)秀的一個(gè)。支持FastCGI,CGI,Auth,輸出壓縮(output compress),URL重寫(xiě),Alias等重要功能;而Apache之所以流行,很大程度也是因?yàn)楣δ茇S富,在lighttpd上很多功能都有相應(yīng)的實(shí)現(xiàn)了,這點(diǎn)對(duì)于apache的用戶是非常重要的,因?yàn)檫w移到lighttpd就必須面對(duì)這些問(wèn)題。
特點(diǎn):
相較于其他的網(wǎng)頁(yè)服務(wù)器,lighttpd僅需少量的內(nèi)存及CPU資源即可達(dá)到同樣的效能。
lighttpd
Lighttpd是一個(gè)具有非常低的內(nèi)存開(kāi)銷,cpu占用率低,效能好,以及豐富的模塊等特點(diǎn)。lighttpd是眾多OpenSource輕量級(jí)的web server中較為優(yōu)秀的一個(gè)。支持FastCGI, CGI, Auth, 輸出壓縮(output compress), URL重寫(xiě), Alias等重要功能。
Lighttpd使用fastcgi方式運(yùn)行php,它會(huì)使用很少的PHP進(jìn)程響應(yīng)很大的并發(fā)量。
Fastcgi的優(yōu)點(diǎn)在于:
從穩(wěn)定性上看, fastcgi是以獨(dú)立的進(jìn)程池運(yùn)行來(lái)cgi,單獨(dú)一個(gè)進(jìn)程死掉,系統(tǒng)可以很輕易的丟棄,然后重新分配新的進(jìn)程來(lái)運(yùn)行邏輯.
從安全性上看, fastcgi和宿主的server完全獨(dú)立, fastcgi怎么down也不會(huì)把server搞垮,
從性能上看, fastcgi把動(dòng)態(tài)邏輯的處理從server中分離出來(lái), 大負(fù)荷的IO處理還是留給宿主server, 這樣宿主server可以一心一意作IO,對(duì)于一個(gè)普通的動(dòng)態(tài)網(wǎng)頁(yè)來(lái)說(shuō), 邏輯處理可能只有一小部分, 大量的圖片等靜態(tài)IO處理完全不需要邏輯程序的參與(注1)
從擴(kuò)展性上講, fastcgi是一個(gè)中立的技術(shù)標(biāo)準(zhǔn), 完全可以支持任何語(yǔ)言寫(xiě)的處理程序(php,java,python...)
nginx與lighttpd性能簡(jiǎn)單對(duì)比
做nginx和lighttpd的性能對(duì)比,可以在選擇webserver上做個(gè)參考。
測(cè)試環(huán)境如下
Dell2950 4G內(nèi)存,F(xiàn)reeBSD 8.2 release
#sysctl -a | grep CPU
hw.model: Intel(R) Xeon(R) CPU 5130 @ 2.00GHz
2x2顆
內(nèi)存4G
#uname -a
FreeBSD localhost.localdomain 8.2-RELEASE
php5.2.17,fastcgi方式,默認(rèn)5進(jìn)程測(cè)試,失敗請(qǐng)求太多,改用20進(jìn)程,無(wú)請(qǐng)求失敗。
Nginx主要配置
events {
use kqueue;
worker_connections 102400;
}
寫(xiě)了個(gè)phpinfo用ab測(cè)試,內(nèi)網(wǎng)IP,不考慮帶寬因素。
# ab -n 100000 -c 1000 http://192.168.100.28/info.php
This is ApacheBench, Version 1.3d <$Revision: 1.73 $> apache-1.3
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright (c) 2006 The Apache Software Foundation, http://www.apache.org/
Benchmarking 192.168.100.28 (be patient)
Completed 10000 requests
Completed 20000 requests
Completed 30000 requests
Completed 40000 requests
Completed 50000 requests
Completed 60000 requests
Completed 70000 requests
Completed 80000 requests
Completed 90000 requests
Finished 100000 requests
Server Software: nginx/1.0.0
Server Hostname: 192.168.100.28
Server Port: 80
Document Path: /info.php
Document Length: 49457 bytes
Concurrency Level: 1000
Time taken for tests: 86.578 seconds
Complete requests: 100000
Failed requests: 0
Broken pipe errors: 0
Total transferred: 4977355920 bytes
HTML transferred: 4961202790 bytes
Requests per second: 1155.03 [#/sec] (mean)
Time per request: 865.78 [ms] (mean)
Time per request: 0.87 [ms] (mean, across all concurrent requests)
Transfer rate: 57489.84 [Kbytes/sec] received
Connnection Times (ms)
min mean[+/-sd] median max
Connect: 0 297 888.8 11 6414
Processing: 20 555 649.9 421 9989
Waiting: 18 555 649.9 421 9988
Total: 20 852 1065.3 473 10222
Percentage of the requests served within a certain time (ms)
50% 473
66% 584
75% 699
80% 776
90% 3308
95% 3473
98% 3688
99% 3817
100% 10222 (last request)
iostat -w 1
tty mfid0 da0 cd0 cpu
tin tout KB/t tps MB/s KB/t tps MB/s KB/t tps MB/s us ni sy in id
0 23 19.12 1 0.01 0.01 0 0.00 0.00 0 0.00 0 0 0 0 100
0 235 0.00 0 0.00 0.00 0 0.00 0.00 0 0.00 0 0 0 0 100
0 79 2.00 3 0.01 0.00 0 0.00 0.00 0 0.00 0 0 0 0 100
4 393 2.00 2 0.00 0.00 0 0.00 0.00 0 0.00 10 0 4 2 85
0 78 123.85 54 6.52 0.00 0 0.00 0.00 0 0.00 76 0 17 7 0
0 76 128.00 1 0.12 0.00 0 0.00 0.00 0 0.00 72 0 16 8 5
0 79 128.00 1 0.12 0.00 0 0.00 0.00 0 0.00 81 0 12 5 2
0 79 128.00 1 0.12 0.00 0 0.00 0.00 0 0.00 74 0 15 7 3
0 79 16.95 19 0.31 0.00 0 0.00 0.00 0 0.00 71 0 19 8 2
0 78 21.60 10 0.21 0.00 0 0.00 0.00 0 0.00 74 0 17 7 2
0 78 0.00 0 0.00 0.00 0 0.00 0.00 0 0.00 74 0 17 8 1
0 78 53.33 3 0.16 0.00 0 0.00 0.00 0 0.00 68 0 21 10 1
0 104 20.20 20 0.39 0.00 0 0.00 0.00 0 0.00 68 0 18 10 5
磁盤(pán)壓力只在測(cè)試開(kāi)始時(shí)大,并且因?yàn)樗袞|西都放在一臺(tái)服務(wù)器,瞬間最高把CPU空閑率干到了0.3%,但實(shí)際上占用CPU最多的是ab,占了40%多,nginx占用不到40%
------------------------------------------------換lighttpd
server.max-fds = 8192
##
## Stat() call caching.
##
## lighttpd can utilize FAM/Gamin to cache stat call.
##
## possible values are:
## disable, simple or fam.
##
server.stat-cache-engine = "simple"
##
## Fine tuning for the request handling
##
## max-connections == max-fds/2 (maybe /3)
## means the other file handles are used for fastcgi/files
##
Server.max-connections = 4096
也是使用kqueue隊(duì)列
# ab -n 100000 -c 1000 http://192.168.100.28/info.php
This is ApacheBench, Version 1.3d <$Revision: 1.73 $> apache-1.3
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright (c) 2006 The Apache Software Foundation, http://www.apache.org/
Benchmarking 192.168.100.28 (be patient)
Completed 10000 requests
Completed 20000 requests
Completed 30000 requests
Completed 40000 requests
Completed 50000 requests
Completed 60000 requests
Completed 70000 requests
Completed 80000 requests
Completed 90000 requests
Finished 100000 requests
Server Software: lighttpd/1.4.28
Server Hostname: 192.168.100.28
Server Port: 80
Document Path: /info.php
Document Length: 49127 bytes
Concurrency Level: 1000
Time taken for tests: 83.107 seconds
Complete requests: 100000
Failed requests: 0
Broken pipe errors: 0
Total transferred: 4932575365 bytes
HTML transferred: 4915304142 bytes
Requests per second: 1203.27 [#/sec] (mean)
Time per request: 831.07 [ms] (mean)
Time per request: 0.83 [ms] (mean, across all concurrent requests)
Transfer rate: 59352.10 [Kbytes/sec] received
Connnection Times (ms)
min mean[+/-sd] median max
Connect: 0 19 206.2 1 3113
Processing: 79 807 155.8 823 3891
Waiting: 66 807 155.8 823 3890
Total: 79 826 251.0 826 4248
Percentage of the requests served within a certain time (ms)
50% 826
66% 843
75% 854
80% 862
90% 894
95% 939
98% 1006
99% 1094
100% 4248 (last request)
# iostat -w 1
tty mfid0 da0 cd0 cpu
tin tout KB/t tps MB/s KB/t tps MB/s KB/t tps MB/s us ni sy in id
0 23 19.25 1 0.01 0.01 0 0.00 0.00 0 0.00 0 0 0 0 100
0 387 0.00 0 0.00 0.00 0 0.00 0.00 0 0.00 0 0 0 0 99
0 78 0.00 0 0.00 0.00 0 0.00 0.00 0 0.00 0 0 0 0 100
4 2185 0.00 0 0.00 0.00 0 0.00 0.00 0 0.00 47 0 14 7 33
0 78 120.00 2 0.23 0.00 0 0.00 0.00 0 0.00 68 0 23 9 1
0 2405 128.00 1 0.12 0.00 0 0.00 0.00 0 0.00 77 0 14 6 2
0 79 128.00 1 0.12 0.00 0 0.00 0.00 0 0.00 71 0 17 10 2
0 1852 128.00 1 0.12 0.00 0 0.00 0.00 0 0.00 72 0 18 8 2
0 79 128.00 1 0.12 0.00 0 0.00 0.00 0 0.00 62 0 22 7 10
0 1143 128.00 1 0.12 0.00 0 0.00 0.00 0 0.00 68 0 20 9 3
0 79 128.00 1 0.12 0.00 0 0.00 0.00 0 0.00 65 0 25 8 2
0 2074 128.00 1 0.12 0.00 0 0.00 0.00 0 0.00 69 0 22 8 2
0 79 128.00 1 0.12 0.00 0 0.00 0.00 0 0.00 65 0 23 10 3
0 1917 128.00 1 0.12 0.00 0 0.00 0.00 0 0.00 62 0 26 10 3
0 79 128.00 1 0.12 0.00 0 0.00 0.00 0 0.00 63 0 25 8 5
0 2247 128.00 1 0.12 0.00 0 0.00 0.00 0 0.00 66 0 23 8 3
0 79 128.00 1 0.12 0.00 0 0.00 0.00 0 0.00 66 0 25 7 2
0 1373 48.00 7 0.33 0.00 0 0.00 0.00 0 0.00 66 0 22 8 4
看起來(lái),lighttpd在磁盤(pán)上并沒(méi)有nginx那樣的瞬間壓力。
總結(jié):
一開(kāi)始的失敗請(qǐng)求主要來(lái)自php-cgi,在此服務(wù)器上,默認(rèn)進(jìn)程數(shù)無(wú)法處理大并發(fā)訪問(wèn)量。改為20以上就不存在失敗了,再高收益不大。從高負(fù)載動(dòng)態(tài)頁(yè)面吞吐率上說(shuō),lighttpd可能要稍稍高于nginx。
附ab靜態(tài)頁(yè)面吞吐率
lighttpd Requests per second: 9045.68 [#/sec] (mean)
nginx Requests per second: 11622.50 [#/sec] (mean)
處理靜態(tài)頁(yè)面上,nginx要比lighttpd快不少。如果去掉ab對(duì)cpu的壓力,兩款軟件加php對(duì)cpu的壓力應(yīng)該不超過(guò)70%。
在處理諸如php,ror等應(yīng)用方面的極端壓榨,可以考慮使用lighttpd。處理靜態(tài)頁(yè)面可以考慮使用nginx。篇幅有限,vmstat和truss跟蹤的結(jié)果就不貼了。