高速Webサーバ Nginxの組み込みPerlを使ってみる
NginxというWebサーバがあります。
ロシアの方がつくったもので、rambler.ruでも使われてます。
日本語情報がほぼ皆無ですが、このあたりで紹介されています。
機能的には、epoll,kqueueやsendfileなどがサポートされ、
- rewrite, header書き換え
- deflate
- FastCGI
- SSL
- シンプルな負荷分散付きreverse proxy
等すでにLighttpdと比較しても遜色ない十分な機能が実装されてます。
パフォーマンス的にもLighttpdと同等かそれ以上でます。
珍しい機能として、「perlインタプリタの組み込み」があるので早速試してみます。Fedora
Core6ではnginxはyumでインストールできます。perl_moduleも有効になってます
perl関連のドキュメントはここ。
nginx.confはこんな感じ。
全体的には個人的はlighttdよりわかりやすいと思う。
events {
use epoll;
}
http {
sendfile on;
perl_modules /home/kazeburo/develop/lib;
perl_require NginxTest/Calc.pm;
server {
listen 8080;
location / {
root /home/kazeburo/html;
index index.html;
}
location /calctgt {
perl NginxTest::Calc::handler;
}
}
}
perl_modulesでライブラリのパス、perl_requireで読み込むモジュール
「perl_require NginxTest/Calc.pm」がかなりイマイチ。
NginxTestは
package NginxTest::Calc;
use strict;
use warnings;
use nginx;
sub handler {
my $r = shift;
my $uri = URI->new( $r->uri, 'http' );
$uri->query($r->args);
$uri->query_param('cm', 0) unless $uri->query_param('cm');
my $hyde;
eval {
$hyde = to_hyde( $uri->query_param('cm') );
};
$r->send_http_header('Content-Type', 'text/html; charset=utf-8');
$r->print( "to_hyde(" . JSON::objToJson( { cm => $uri->query_param('cm'), hyde => $hyde } ) . ");" );
return OK;
}
use行ははしょった。
$rがngixのオブジェクトでmod_perlのそれに似てる。
んで、nginxの起動
$ /usr/sbin/nginx -c nginx.conf
curlでテスト
$ curl "http://localhost:8080/calctgt=156"
to_hyde({"hyde":1.00,"cm":156});
ktkr!
ついでにApacheBenchでテスト。結構性能いいかも
$ ab -n 1000 -c 10 "http://localhost:8080/calctgt?cm=156" Requests per second: 1457.78 [#/sec] (mean) Time per request: 6.860 [ms] (mean) Time per request: 0.686 [ms] (mean, across all concurrent requests)
デモはあとで用意する。かな