NginxとApacheの比較:NginxはCGIスクリプトを普通では利用できない件

NginxはApacheより処理速度が早いとされて、昨今人気が出てきている。


NginxとApacheの速度比較を検討するとともに、速度と機能の等価交換について
検討したい。



まずは、ここに簡潔でいいデータがある。



参考:mod_php(Apache)とspawn-fcgi(nginx)でのパフォーマンス比較




本サイトが見れなかったので、キャッシュから一部引用させてもらう。
Host名は抽象化して掲載。


1接続で100アクセス:Apache VS NGINX

Apache

まずはWebスタックの大御所「Apache

Apache


ab -c 1 -n 100 -H "Host: xxx.xxx" http://xxx.xxx:82/
This is ApacheBench, Version 2.0.40-dev <$Revision: 1.146 $> apache-2.0
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright 2006 The Apache Software Foundation, http://www.apache.org/

Benchmarking xxx.xxx (be patient).....done

Server Software: Apache/2.2.3
Server Hostname: xxx.xxx
Server Port: 82

Document Path: /
Document Length: 18653 bytes

Concurrency Level: 1
Time taken for tests: 9.100542 seconds
Complete requests: 100
Failed requests: 0
Write errors: 0
Total transferred: 1886900 bytes
HTML transferred: 1865300 bytes
Requests per second: 10.99 [#/sec] (mean)
Time per request: 91.005 [ms] (mean)
Time per request: 91.005 [ms] (mean, across all concurrent requests)
Transfer rate: 202.41 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 73 90 23.3 77 157
Waiting: 62 79 23.3 66 146
Total: 73 90 23.3 77 157

Percentage of the requests served within a certain time (ms)
50% 77
66% 79
75% 115
80% 115
90% 117
95% 153
98% 155
99% 157
100% 157 (longest request)

Nginx

対するはWebスタックの新旗手「Nginx」

Nginx


ab -c 1 -n 100 -H "Host: xxx.xxx" http://xxx.xxx:81/
This is ApacheBench, Version 2.0.40-dev <$Revision: 1.146 $> apache-2.0
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright 2006 The Apache Software Foundation, http://www.apache.org/

Benchmarking xxx.xxx (be patient).....done

Server Software: nginx/0.7.64
Server Hostname: xxx.xxx
Server Port: 81

Document Path: /
Document Length: 18653 bytes

Concurrency Level: 1
Time taken for tests: 9.789250 seconds
Complete requests: 100
Failed requests: 0
Write errors: 0
Total transferred: 1886000 bytes
HTML transferred: 1865300 bytes
Requests per second: 10.22 [#/sec] (mean)
Time per request: 97.893 [ms] (mean)
Time per request: 97.893 [ms] (mean, across all concurrent requests)
Transfer rate: 188.06 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 73 97 127.7 75 1340
Waiting: 62 73 23.4 65 143
Total: 73 97 127.7 75 1340

Percentage of the requests served within a certain time (ms)
50% 75
66% 76
75% 77
80% 80
90% 149
95% 151
98% 153
99% 1340
100% 1340 (longest request)


ApacheがRequests per second(秒間処理数)が10.99[#/sec] (mean)に対して、
Nginxの場合、10.22 [#/sec] (mean)となっている。100回アクセスであれば、若干Apacheが優位。






同時10接続で20アクセス:Apache VS Nginx

次の条件は、同時10接続で20アクセスをした場合。実際のWebは同時アクセスが多いだろうから、
こういう条件ほうが実践向きだ。


Apache(同時10接続で20アクセス)

Apache


date; ab -c 10 -t 20 -H "Host: xxx.xxx" http://xxx.xxx/;date
2010年 2月 11日 木曜日 16:07:23 JST
This is ApacheBench, Version 2.0.40-dev <$Revision: 1.146 $> apache-2.0
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright 2006 The Apache Software Foundation, http://www.apache.org/

Benchmarking xxx.xxx (be patient)
Finished 611 requests

Server Software: Apache/2.2.3
Server Hostname: xxx.xxx
Server Port: 82

Document Path: /
Document Length: 18376 bytes

Concurrency Level: 10
Time taken for tests: 37.188158 seconds
Complete requests: 611
Failed requests: 0
Write errors: 0
Total transferred: 11364056 bytes
HTML transferred: 11231864 bytes
Requests per second: 16.43 [#/sec] (mean)
Time per request: 608.644 [ms] (mean)
Time per request: 60.864 [ms] (mean, across all concurrent requests)
Transfer rate: 298.40 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 95 244 289.4 188 2277
Waiting: 83 204 215.6 170 1851
Total: 95 244 289.4 188 2277

Percentage of the requests served within a certain time (ms)
50% 188
66% 209
75% 229
80% 240
90% 271
95% 312
98% 1736
99% 1795
100% 2277 (longest request)

Nginx(同時10接続で20アクセス)

Nginx


date; ab -c 10 -t 20 -H "Host: xxx.xxx" http://xxx.xxx:81/;date
2010年 2月 11日 木曜日 16:10:52 JST
This is ApacheBench, Version 2.0.40-dev <$Revision: 1.146 $> apache-2.0
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright 2006 The Apache Software Foundation, http://www.apache.org/

Benchmarking xxx.xxx (be patient)
Finished 868 requests

Server Software: nginx/0.7.64
Server Hostname: xxx.xxx
Server Port: 81

Document Path: /
Document Length: 18376 bytes

Concurrency Level: 10
Time taken for tests: 20.1645 seconds
Complete requests: 868
Failed requests: 0
Write errors: 0
Total transferred: 16146515 bytes
HTML transferred: 15966632 bytes
Requests per second: 43.40 [#/sec] (mean)
Time per request: 230.434 [ms] (mean)
Time per request: 23.043 [ms] (mean, across all concurrent requests)
Transfer rate: 788.34 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 134 228 49.6 223 526
Waiting: 121 215 48.9 210 513
Total: 134 228 49.6 223 526

Percentage of the requests served within a certain time (ms)
50% 223
66% 243
75% 260
80% 268
90% 290
95% 308
98% 339
99% 360
100% 526 (longest request)

ApacheがRequests per second(秒間処理数)が16.43[#/sec] (mean)に対して、
Nginxの場合、43.40[#/sec] (mean)となっている。


同時10接続の20回アクセス条件であれば、圧倒的にApacheよりNGINXが優位。


Nginxの速度と機能

Nginxは軽量であり、パワフルなWebサーバだ。
ロードバランサの機能を備えており、スケールアウト構成や同時アクセスの処理にも長けている。

これはApacheが機能拡充を目指したのに対して、NGINXは大量処理、スピード重視を徹底的に追求した結果だ。


「Simple is best」という哲学

例えば興味深い機能の哲学として、純粋なCGIスクリプトの取り扱いがある。



NGINXのマニュアル「ThttpdCGI」の項には
以下のようにある。

Nginx doesn't serve CGI scripts. While there's all sorts of hacks to work around this, none of them are really satisfactory or general-purpose.


意訳すると、


「わ、わたし(Nginx)は、CGIスクリプトなんか扱わないんだから。
CGIスクリプト周りって、いろんなハックするものがあるし、
そういうのに構うのって本当にいやだし、なんか普通じゃないしね」


となる。

でもどうしても使いたいならって、Nginxはおともだちの軽量Webサーバ
「thttpd」の紹介(プロキシ方法)を載せている。またFastCGIだったら使える。
PerlPHPPythonFastCGI上で動作させる)


このあたりも、器用で少し遅いApacheを紹介しないように、スピード重視の哲学が
貫徹している。




Simple is best:一つのことをうまくやれ


Unixの世界には、「Simple is best」という哲学がある。「一つのことをうまくやれ」だ。
これは間違いなくUnixの世界では真理だし、コンピューティングの世界において汎用的な法則の一つだ。


Nginxはこの成功例だろう。



Nginxは機能をApacheと比較して減らす分、先に述べた大量処理を手にいれいている。



これはこれで成功している例だろう。




今日の名言:一つのことをうまくやれ