さくら VPS の Web サーバを Apache2 から nginx へ変更することを検討しています。特に理由はないのですが、強いて言えば

  • レスポンスの向上
  • 軽量な Web サーバを試してみたかった!

といったところです。

軽量な Web サーバを調べてみた

Apache以外の軽量なWebサーバとして有名なものとして、以下の二つを発見しました:

  • lighttpd
  • nginx

lighttpd

軽量な Web サーバとしてはじめに検討したのが、lighttpd でした。これはネーミングからして簡単に推測できます。Light な httpd です。こいつは Web サーバのみの機能を持ち、軽量なようです。しかし、次の記事を読んで nginx に心移りしてしまったのです。

nginx

nginx は Engine X (エンジン・エックス) と発音します。軽量な Web サーバ かつ、リバースプロキシサーバです。プロキシサーバも兼ねる、という点が私の心にヒットしました。普通はこのような構成になるはずなんです:

f:id:sirocco634:20110202223001p:image

それが nginx を導入するだけで、

f:id:sirocco634:20110202223000p:image

という構成が簡単に作れるのです。nginxを覚えれば、一度で Web サーバとリバースプロキシーを作れちゃうのがお得♪という感じなわけです。

まとめ

とりあえず nginx で頑張ってみる。ローカルの環境だと、なぜかリバースプロキシーをかませるとレスポンス悪くなるんだけど、これはなぜなのだろう。。。もう少し検証してみることにしよう。

追記

nginx.conf

# You may add here your
# server {
#  ...
# }
# statements for each of your virtual hosts
server {
listen   8001;
server_name srv634;
access_log  /var/log/nginx/nginx.access.log;
location / {
root   /var/www/nginx-default;
index  index.html index.htm index.php;
}
location /wordpress {
root /var/www/nginx-default;
index index.php;
if (-f $request_filename) {
expires 30d;
break;
}
if (!-e $request_filename) {
rewrite ^.+?(/wp-.*) $1 last;
rewrite ^.+?(/.*\.php)$ $1 last;
rewrite ^ /wordpress/index.php last;
}
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass    127.0.0.1:9000;
fastcgi_index   index.php;
fastcgi_param   SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_name;
}
#error_page  404  /404.html;
# redirect server error pages to the static page /50x.html
#
#error_page   500 502 503 504  /50x.html;
#location = /50x.html {
#  root   /var/www/nginx-default;
#}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
#proxy_pass   http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
#fastcgi_pass   127.0.0.1:9000;
#fastcgi_index  index.php;
#fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
#includefastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
#deny  all;
#}
}
upstream backend {
ip_hash;
server 127.0.0.1:8001;
}
server {
listen 80;
server_name srv634;
access_log  /var/log/nginx/front_proxy.access.log;
error_log  /var/log/nginx/front_proxy.error.log;
location /wp-admin { proxy_pass http://backend; }
location /wp-login.php { proxy_pass http://backend; }
location / {
proxy_pass http://backend;
proxy_cache czone;
proxy_cache_key $scheme$proxy_host$uri$is_args$args;
proxy_cache_valid  200 10m;
}
}

abの結果(リバースプロキシーをかませない場合)

kazu634@kazu634% ab -n 5000 -c 100 http://srv634:8001/wordpress                                                                              ~ [1682]
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking srv634 (be patient)
Completed 500 requests
Completed 1000 requests
Completed 1500 requests
Completed 2000 requests
Completed 2500 requests
Completed 3000 requests
Completed 3500 requests
Completed 4000 requests
Completed 4500 requests
Completed 5000 requests
Finished 5000 requests
Server Software:        nginx/.7.65
Server Hostname:        srv634
Server Port:            8001
Document Path:          /wordpress
Document Length:        185 bytes
Concurrency Level:      100
Time taken for tests:   1.890 seconds
Complete requests:      5000
Failed requests:        
Write errors:           
Non-2xx responses:      5000
Total transferred:      1920000 bytes
HTML transferred:       925000 bytes
Requests per second:    2645.01 [#/sec] (mean)
Time per request:       37.807 [ms] (mean)
Time per request:       .378 [ms] (mean, across all concurrent requests)
Transfer rate:          991.88 [Kbytes/sec] received
Connection Times (ms)
min  mean[+/-sd] median   max
Connect:            1   .4      1       4
Processing:     6   36   2.6     37      41
Waiting:        6   36   2.6     37      41
Total:          9   37   2.4     38      43
Percentage of the requests served within a certain time (ms)
50%     38
66%     38
75%     38
80%     38
90%     39
95%     39
98%     39
99%     39
100%     43 (longest request)

abの結果(プロキシサーバをかませた場合):

kazu634@kazu634% ab -n 5000 -c 100 http://srv634/wordpress                                                                                   ~ [1683]
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking srv634 (be patient)
Completed 500 requests
Completed 1000 requests
Completed 1500 requests
Completed 2000 requests
Completed 2500 requests
Completed 3000 requests
Completed 3500 requests
Completed 4000 requests
Completed 4500 requests
Completed 5000 requests
Finished 5000 requests
Server Software:        nginx/.7.65
Server Hostname:        srv634
Server Port:            80
Document Path:          /wordpress
Document Length:        185 bytes
Concurrency Level:      100
Time taken for tests:   5.583 seconds
Complete requests:      5000
Failed requests:        
Write errors:           
Non-2xx responses:      5000
Total transferred:      1920000 bytes
HTML transferred:       925000 bytes
Requests per second:    895.52 [#/sec] (mean)
Time per request:       111.666 [ms] (mean)
Time per request:       1.117 [ms] (mean, across all concurrent requests)
Transfer rate:          335.82 [Kbytes/sec] received
Connection Times (ms)
min  mean[+/-sd] median   max
Connect:            1   .6      1       7
Processing:    18  110   7.5    111     126
Waiting:       18  110   7.5    111     126
Total:         24  111   7.    112     127
Percentage of the requests served within a certain time (ms)
50%    112
66%    112
75%    112
80%    112
90%    113
95%    113
98%    114
99%    114
100%    127 (longest request)

1秒あたりの処理数はプロキシサーバをかまさないほうが早いのだが。。。同じルーターにぶら下がっているから、ネットワークの影響殆ど無いから?