h2oの機能と設定まとめ:その1

すろっくさんです。

会社のテックリさんが

「ねえねえすろっくさんmrubyくわしい? むしろh2oくわしい? じゃあ年明けからめっちゃレビューだすからよろぴく!」

などと無茶振りお願いされたので年明けからずっとh2oを調査検証してました。近々ブログサーバのお引越しもあるので、ちょうどいいタイミングだしnginxからの乗り換えも見越してやってみるかと。

まずは基本これを改造していきます。


user: www
listen:
  port: 80

hosts:
  "0.0.0.0":
    paths:
      "/":
        file.dir: /var/www/html
access-log: /var/log/h2o/access.log
error-log: /var/log/h2o/error.log

レスポンス。まあこんなもんかなあ。


[root@localhost h2o]# curl http://127.0.0.1
Welcome to H2O - an optimized HTTP server
It works!

mrubyとかつかってみる。


user: www
listen:
  port: 80

hosts:
  "0.0.0.0":
    paths:
      "/":
        ## ここから追加
        mruby.handler: |
          Proc.new do |env|
           [200, {'content-type' => 'text/plain'}, ["h2o in mruby work!\n"]]
          end
                ## ここまで
        file.dir: /var/www/html
access-log: /var/log/h2o/access.log
error-log: /var/log/h2o/error.log

さっきのindex.htmlじゃなくてmrubyでかいた200がかえります。h2oはデフォルトでmruby使えます。いいですね。ちなみにyumでいれました。


[root@localhost h2o]# curl http://127.0.0.1
h2o in mruby work!

PHPとか実行したいので、さっきのmrubyは消して設定追加します。PHP実行のバックエンドはApache + mod-phpでもphp-fpmでも好きなものを用意してください。

<pre>
<code>
user: www
listen:
  port: 80
## ここから追加
file.custom-handler:
  extension: .php
  fastcgi.connect:
    host: <PHPのバックエンド>
    port: <PHPのバックエンドのポート>
    type: tcp
## ここまで
hosts:
  "0.0.0.0":
    paths:
      "/":
        file.dir: /var/www/html
access-log: /var/log/h2o/access.log
error-log: /var/log/h2o/error.log
</code>
</pre>

適当なPHPファイルをドキュメントルートの下におきます。


print("Hello This PHPer Site.\n");

PHPがかえります。いいですね。


[root@localhost h2o]# curl http://127.0.0.1/index.php
Hello This PHPer Site.

ただh2oはデフォルトだとnginxとApacheみたいに末尾がhtmlがついたものをメインのインデックスファイルとしてみています。これをindex.phpにしたいなあ、ってときは明示的にindex.phpが最初に参照されるようにします。


user: www
listen:
  port: 80
file.custom-handler:
  extension: .php
  fastcgi.connect:
    host: 
    port: 
    type: tcp
## これ追加
file.index: [ 'index.php','index.html' ]
## ここまで
hosts:
  "0.0.0.0":
    paths:
      "/":
        file.dir: /var/www/html
access-log: /var/log/h2o/access.log
error-log: /var/log/h2o/error.log


これでindex.phpなしでアクセスしてもindex.phpがかえります。いいですね。


[root@localhost h2o]# curl http://127.0.0.1/
Hello This PHPer Site.

さわりとしてはこんな感じですかね。なかなか設定ファイルわかりやすくていいです。設定項目一通り調べましたが、しんどいこともだいぶなくていいなと思いました。

調査検証してたら膨大な量になったのでここではさわりだけ。続きはまた。

in h2o | 246 Words