nginx初试及apache对比测试

nginx是个比较简便的httpserver,今天下午有空就试了下安装,包括php-fastcgi,并和apache做了一下对比测试。结果令人对nginx刮目相看,在html的测试中表现非常优秀。

nginx的wiki:http://wiki.codemongers.com

首先看看nginx的安装。

1.先安装php for FastCGI
# tar zxvf php-5.2.1.tar.gz
# ./configure  –prefix=/opt/php-cgi –enable-fastcgi –with-gd –with-zlib-dir=/usr/lib –with-png-dir=/usr/lib
–with-freetype-dir=/usr/include/freetype2 –with-mysql=/usr/local/mysql
# make
# make install
# cp php.ini-dist /opt/php-cgi/lib/php.ini

2. 启动php for fastcgi
/opt/php-cgi/bin/php -q -b 127.0.0.1:8888

3. 安装nginx
需要pcre支持,下载一个编译安装即可
# tar zxvf nginx-0.5.35.tar.gz
# ./confgure –prefix=/opt/nginx
# make
# make install

修改一下nginx的配置文件,apache在80端口listen,nginx在8080端口listen,在/opt/nginx/conf/nginx.conf中设置php的fastcgi项目如下:

location ~ \.php$ {
            fastcgi_pass   127.0.0.1:8888;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /opt/nginx/html$fastcgi_script_name;
       }

启动nginx即可
# /opt/nginx/bin/nginx -t /opt/nginx/conf/nginx.conf  测试配置文件
# /opt/nginx/bin/nginx -c /opt/nginx/conf/nginx.conf  启动

好了,下面看看和apache的对比测试。测试了两个页面,一个是简单的html页面,一个是phpinfo的php页面。

以下是html页面在nginx上的测试结果:

# /usr/local/apache/bin/ab -n 10000 -c 200 http://xx.xx.xx.xx:8080/index.html

Server Software:        nginx/0.5.35
Server Hostname:        xx.xx.xx.xx
Server Port:            8080

Document Path:          /index.html
Document Length:        151 bytes

Concurrency Level:      200
Time taken for tests:   4.166183 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      3620000 bytes
HTML transferred:       1510000 bytes
Requests per second:    2400.28 [#/sec] (mean)
Time per request:       83.324 [ms] (mean)
Time per request:       0.417 [ms] (mean, across all concurrent requests)
Transfer rate:          848.50 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       36   38   0.8     38      41
Processing:    42   43   1.3     44      46
Waiting:        4   20   9.6     21      39
Total:         81   82   0.6     82      84

Percentage of the requests served within a certain time (ms)
  50%     82
  66%     82
  75%     82
  80%     83
  90%     83
  95%     83
  98%     83
  99%     84
100%     84 (longest request)

以下是html页面在apache上的测试结果:

# /usr/local/apache/bin/ab -n 10000 -c 200 http://xx.xx.xx.xx/nginx.html

Server Software:        Apache/2.0.53
Server Hostname:        xx.xx.xx.xx
Server Port:            80

Document Path:          /nginx.html
Document Length:        151 bytes

Concurrency Level:      200
Time taken for tests:   8.305257 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      4197042 bytes
HTML transferred:       1523439 bytes
Requests per second:    1204.06 [#/sec] (mean)
Time per request:       166.105 [ms] (mean)
Time per request:       0.831 [ms] (mean, across all concurrent requests)
Transfer rate:          493.42 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   40 237.3     12    3024
Processing:    10  121 178.0    103    2997
Waiting:        5  109 177.3     95    2968
Total:         33  161 397.8    114    5990

Percentage of the requests served within a certain time (ms)
  50%    114
  66%    124
  75%    146
  80%    148
  90%    151
  95%    265
  98%    285
  99%    293
100%   5990 (longest request)

以下是php页面在nginx+fastcgi/php上的测试结果:

# /usr/local/apache/bin/ab -n 5000 -c 100 http://xx.xx.xx.xx:8080/info.php

Server Software:        ngin
x/0.5.35
Server Hostname:        xx.xx.xx.xx
Server Port:            8080

Document Path:          /info.php
Document Length:        34747 bytes

Concurrency Level:      100
Time taken for tests:   25.987194 seconds
Complete requests:      5000
Failed requests:        0
Write errors:           0
Total transferred:      175453256 bytes
HTML transferred:       174713846 bytes
Requests per second:    192.40 [#/sec] (mean)
Time per request:       519.744 [ms] (mean)
Time per request:       5.197 [ms] (mean, across all concurrent requests)
Transfer rate:          6593.29 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0  124  98.5    112     482
Processing:    14  391 115.1    416    1059
Waiting:        2  215 160.7    169     963
Total:         38  515  81.5    516    1114

Percentage of the requests served within a certain time (ms)
  50%    516
  66%    520
  75%    521
  80%    521
  90%    523
  95%    629
  98%    767
  99%    769
100%   1114 (longest request)

以下是php页面在apache+php上的测试结果:

# /usr/local/apache/bin/ab -n 5000 -c 100 http://xx.xx.xx.xx/phpinfo.php

Server Software:        Apache/2.0.53
Server Hostname:        211.67.66.171
Server Port:            80

Document Path:          /phpinfo.php
Document Length:        43141 bytes

Concurrency Level:      100
Time taken for tests:   30.651270 seconds
Complete requests:      5000
Failed requests:        0
Write errors:           0
Total transferred:      218779452 bytes
HTML transferred:       217940922 bytes
Requests per second:    163.13 [#/sec] (mean)
Time per request:       613.025 [ms] (mean)
Time per request:       6.130 [ms] (mean, across all concurrent requests)
Transfer rate:          6970.38 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   99  57.8    104     384
Processing:    75  503 177.2    474    2508
Waiting:       10  195 171.1    139    2086
Total:         85  603 168.8    585    2655

Percentage of the requests served within a certain time (ms)
  50%    585
  66%    588
  75%    590
  80%    591
  90%    619
  95%    725
  98%    902
  99%   1749
100%   2655 (longest request)

处理html静态页面,nginx基本超过了apache两倍,处理php页面两者差别不太大,看来在一些特殊web服务,如flv视频,图片,静态html信息页面等使用nginx作为专门的服务器也许能更好的利用设备能力。

nginx初试及apache对比测试》有2条评论

    1. 呵呵,从应用的角度来讲是过于简单和片面,不过我这里仅仅只是对nginx和apache作个比较而已,希望多交流

发表评论