Thursday, June 18, 2015

H2OとPHPを組み合わせるの、超簡単です(もしくはmod_rewriteが不要な理由)

FastCGI対応機能がH2Oにマージされたことを受けて、uzullaさんが「H2OでPHP(がちょっとだけ動くまで)」という記事を書いてくださっています。

ありがたやありがたや。

その中で、
http://hoge/entry/1 みたいなのをphpにマップする方法はまだよくわかってません。その内しらべます

github.comを読む限り
FastCGI (or PHP) applications should be as easily configurable as it is for the Apache HTTP server
ということで、やったぜ!ってなるんですけど、nginxはもとより、Apacheにおいても現状ルーターをつかっているようなアプリだとhtaccessをいちいちかかないといけないので、Apacheみたいなスタイルが楽なのか?というとちょっと疑問があります。

(たとえば以下みたいなの)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
ここらへんがスッっとかけないと、まあApache、nginx同様にH2Oの設定のタレが必要になるかなあ、という感じはあります。
いやはや、まったくそのとおりなわけです。

ちなみにこの手のことをNginxでやろうとするともっと難しくて、以下のようにtry_filesfastcgi_split_path_infoを使わないと脆弱性が発生するなんて話があるそうです(参照: Setting up PHP-FastCGI and nginx? Don’t trust the tutorials: check your configuration! » Neal Poole)。これ、みんなちゃんとできてるんですかね?
# Pass all .php files onto a php-fpm/php-fcgi server.
location ~ \.php$ {
   try_files $uri =404;

   fastcgi_split_path_info ^(.+\.php)(/.+)$;
   include fastcgi_params;
   fastcgi_index index.php;
   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
   fastcgi_pass php;
}

こんな難しい設定、書けるわけないだろうが!!!!!!!

というわけで、H2Oの場合はもっと簡単に設定できるようになっています。以下のような感じ。
paths:
  "/":
    # /path/to/doc-root以下の静的ファイルを返す(存在した場合)
    file.dir: /path/to/doc-root
    # 存在しなければ、/index.php/ に内部転送
    redirect:                     
      url: /index.php/
      internal: YES
      status: 307

単純ですね。どうなってるかはコメントを見れば自明かとも思いますが、以下解説します。

実は、H2Oの設定ファイルにおいては、1つのパスに複数のハンドラを設定することができるのです。

複数のハンドラが設定されている場合、H2O は有効なレスポンスが生成できるまで、ハンドラを順次実行していきます。つまり、コメントにあるように、ファイルが存在すればそれを返すし、存在しなければ /index.php/ 以下にリクエストを内部的に転送し、その転送した結果の応答をクライアントに返すわけです。

わかりやすいですね。簡単ですね。


少し理屈っぽい話をすると、mod_rewriteのようなリクエストを書き換える黒魔術は、おおむね3種類の目的に使われてきました。第1の目的は、複数のローカルのディレクトリツリーを重ね合わせてひとつのhttpサイトとして表示すること。第2の目的は、uzullaさんが指摘したような、特定の条件をもつ以外のURLの処理を、FastCGIやアプリケーションサーバに委譲すること。第3の目的は、User-Agentや言語設定によって異なる応答を返すこと。

ですが、前2者を実現するためだけならば、正規表現を用いてURLを書き換えるような手法は過剰にすぎて管理がしづらかったわけです。H2OにおけるPHP対応においては、この点を意識し、複数のハンドラの連鎖を可能にすることで、設定が安全簡潔にできるようにしたのでした。

ちなみに、uzullaさんの例ではphp-fpmを使っていますが、H2Oはphp-cgiを自分で管理することができるので、小規模なウェブサイトにおいては、
file.custom-handler:          
  extension: .php
  fastcgi.spawn: "PHP_FCGI_CHILDREN=10 exec /usr/bin/php-cgi"
のように書くと、別途FastCGIデーモンを管理する必要がなくなって、より幸せになれるでしょう(名前はphp-cgiでも、この設定においてはFastCGIとして動作します)。


最後になりますが、ここで説明したFastCGI対応機能を盛り込んだH2Oバージョン1.3は今朝方リリースされたので、PHPerの皆様もそれ以外の皆様も、お試しいただければ幸甚に存じます。

リンク先のリリース告知エントリにも書いてあるけど、他のウェブサーバより表示速度(first-paint time)が、かなり速いですよ!

70 comments:

  1. Oletusasetus on kytt Helsingin seudun reittiopasta. Click To Discover More! Mind Map: by its intuitive way to show informationIit is a fantastic brain storming tool to arrange your life or ideas.

    ReplyDelete
  2. Share tips and recommendations with friends and other travelers. downlodable manuals -Designed for preschoolers- no confusing menus or navigation.

    ReplyDelete
  3. Features:- Take care of a pregnant mommy and her new born kids. Look for it Change skins in a flash without ever stopping the music.

    ReplyDelete
  4. Tidichi will together with you to build a community to search and share useful place for everyone. download torrent Search exhibitors, speakers or seminarsSearch through lists of exhibitors speakers or seminars.

    ReplyDelete
  5. And of course, you can share any item with friends via e-mail, Twitter, Facebook, or Tumblr. checkout this. We listen to our customer&'s suggestions and that&'s why version 2.

    ReplyDelete
  6. This application has subtitles in ENGLISH & CHINESE, More Languages to Follow. downloadbooks.ru Youve probably heard about the stunning flowers from Roses Only, but did you know about our other beautiful and unique gifts.

    ReplyDelete
  7. Or you can simple standing close to enemies and get experience/money when they are killed by weapon buildings, but it's less than being kill by you. download freeware Some you've probably heard of, and some you've most likely haven't.

    ReplyDelete
  8. To protect their children, Sean and Yana send them to stay with a powerful family friend on an orbiting space station. downloadgoodfilesblog.top Support locates your current location quickly without internet connection(iPhone only).

    ReplyDelete
  9. You can dl lots of shows for free like anime or movies. downloadingstarted.me Take multiple photos AND videos, and have them uploaded to JIRA, for fast triage, search and reporting.

    ReplyDelete
  10. Capture meeting notes using the voice-text capability of your phone. http://greatdownloadfiles.gdn Smash through 150 epic levels of castle crashing action, then expand your kingdom with the powerful build-and-share level editor.

    ReplyDelete
  11. The true power of the app lies in the search feature. downloadsoftwarefromalexis.online We had no idea where to even find a PIN, so we went to the Web site to see if we had to create an account to get one.

    ReplyDelete
  12. Don't be surprised if the game gives you an illusion that this is a 36 in 1 game. downloadornot.me But it's not that easy, why would monsters appear unarmed.

    ReplyDelete
  13. From manastone: Is the game Three Kingdoms too difficult. downloadfreethingshere.gdn Makes it possible to make your own games, what more can I say?

    ReplyDelete
  14. Your subscription includes access to this app, online, mobile website, e-newspaper and, if you choose, home delivery of the print edition. downloadfromthisblog.gdn As daily fluctuations of your portfolio might not show the tendency of your portfolio over time, this view helps you to get an overview.

    ReplyDelete
  15. And you can follow the many links to discover more details in a concise view. downlodable serial numbers Note: House Hunter Pro is not a search tool or tied to any MLS system.

    ReplyDelete
  16. If You Want Watch IPL Live Stream Then Visit www.crickspo.com, it also provide IPL Live ScoreCheck IPL Updates, IPL News, Ball By Ball Score, Men of the atch, men of the series, who won the toss today etc.

    ReplyDelete
  17. seo is good process for rankingWhat is seo process What is pbn networksDigital Marketing Career earn money online by Digital MarketingWhat is seo scope of seoHow to convert traffic into leads
    Digital Marketing course in faridabad

    ReplyDelete
  18. tips to earn money online How to earn money online learn at webhelpy

    what is pbn networks Learn more about PBN networks

    what is seo process Learn about seo and seo process

    ReplyDelete
  19. digital marketing course in faridabad Best digital marketing course in Faridabad at Webhelpy

    Digital Marketing Career  Build your Career with Digital Marketing

    How to convert traffic into leads  Learn lead generation at Webhelpy

    ReplyDelete
  20. buy gst ebook to make law more clear

    gst ebook to better understand gst

    buy online gst ebook

    gst ebook from lawcrux

    gst consultant in india

    buy best gst book

     

     

     

     

     

    ReplyDelete
  21. gst Advisors in Faridabad  to get more details in indirect tax
    Professional Tax Consultant in Faridabad
    digital book on gst ebook 
    gst consultants in india
    gst consultancy in india
    indirect tax consultancy in india to make the law more clear
    indirect tax consultancy in faridabad
    Professional Tax Consultant in Faridabad best tax consultant
    gst ebook To make the law more clear, we provide you with the elaborative interpretation of the law in this GST E-Book.

    ReplyDelete
  22. gst  best gst consultancygst Advisors in Faridabad  to get more details in indirect taxProfessional Tax Consultant in Faridabaddigital book on gst ebook gst consultants in indiagst consultancy in indiaindirect tax consultancy in india to make the law more clearindirect tax consultancy in faridabadProfessional Tax Consultant in Faridabad best tax consultantgst ebook To make the law more clear, we provide you with the elaborative interpretation of the law in this GST E-Book.buy gst ebookonline buy gst ebookgst ebook software gst book online gst ebookonline buy gst book  

    ReplyDelete

  23. Awesom blog and great information. Your article has really peaked my interest. I am going to bookmark your website and keep checking for new details about once per week.

    happy Holi status for whatsapp
    Holi messages for friends
    holi images for lovers
    unique WiFi names for your network

    ReplyDelete
  24. gst  best gst consultancygst Advisors in Faridabad  to get more details in indirect taxProfessional Tax Consultant in Faridabaddigital book on gst ebook gst consultants in indiagst consultancy in indiaindirect tax consultancy in india to make the law more clearindirect tax consultancy in faridabadProfessional Tax Consultant in Faridabad best tax consultantgst ebook To make the law more clear, we provide you with the elaborative interpretation of the law in this GST E-Book.buy gst ebookonline buy gst ebookgst ebook software gst book online gst ebookonline buy gst book  

    ReplyDelete
  25. Reiki first degree training in Faridabad | Vedic Seeds Astro Vastu Reiki first degree training in Faridabad | Vedic Seeds Astro Vastu – Reiki is helpful alternate medical therapy that help to recover

    ReplyDelete
  26. Reiki second degree training in Faridabad | Vedic Seeds Astro Vastu Reiki second degree training in Faridabad | Vedic Seeds Astro Vastu – Reiki is helpful alternate medical therapy that help to recover

    ReplyDelete
  27. Vedic Seeds Astro Vastu a top astrologer deals in providing career astrology based services and advice along with astro insights. Vedic seeds provide you all services remedies to help in your trouble. Do your karma make your future.We believe in results and benefits which you get.
    To Get more information visit at:www.vedicseedsastrovastu.com

    ReplyDelete
  28. Vedic seeds provide you all services remedies to help in your trouble. ``Do you karma make your future`` We believe in results and benefits which you get.If you have questions about your future, the answers are in the palm of your hands! We are with you now and after your work done, for your services.To get to the questions that arise in everyday get in here to introduce you to vedic Seeds Astro Vastu, who is one of astrologer in Faridabad in the service of society for more information .... http://www.vedicseedsastrovastu.com/

    ReplyDelete
  29. Vedic seeds provide you all services remedies to help in your trouble. ``Do you karma make your future`` We believe in results and benefits which you get.
    If you have questions about your future, the answers are in the palm of your hands! We are with you now and after your work done, for your services.To get to the questions that arise in everyday get in here to introduce you to vedic Seeds Astro Vastu, who is one of astrologer in Faridabad in the service of society more information .... http://www.vedicseedsastrovastu.com/

    ReplyDelete
  30. gst  best gst consultancygst Advisors in Faridabad  to get more details in indirect taxProfessional Tax Consultant in Faridabaddigital book on gst ebook gst consultants in indiagst consultancy in indiaindirect tax consultancy in india to make the law more clearindirect tax consultancy in faridabadProfessional Tax Consultant in Faridabad best tax consultantgst ebook To make the law more clear, we provide you with the elaborative interpretation of the law in this GST E-Book.buy gst ebookonline buy gst ebookgst ebook software gst book online gst ebookonline buy gst book  

    ReplyDelete
  31. Best GST consultancy Lawcrux advisors are the best GST consultancy in India
    gst Advisors in Faridabad  to get more details in indirect tax
    Professional Tax Consultant in Faridabad
    digital book on gst ebook 
    gst consultants in india
    gst consultancy in india
    indirect tax consultancy in india to make the law more clear
    indirect tax consultancy in Faridabad
    Professional Tax Consultant in Faridabad best tax consultant
    gst ebook To make the law more clear, we provide you with the elaborative interpretation of the law in this GST E-Book.
    Best GST ebook which is based on indirect taxes
    online buy gst ebook FTP, Fema, Custom, Service Tax, Excise
    GST ebook software for professionals
    GST Ebook best ebook based on indirect taxation
    online GST ebook on GST
    online buy GST book

    ReplyDelete
  32. https://jeff-vogel.blogspot.com/2015/07/indie-games-refunds-terror-and-taking.html?showComment=1558764476861#c8641824850897407459

    ReplyDelete
  33. https://www.theviralvista.com/new-oppo-mobile-phones-under-30000-rs/

    ReplyDelete
  34. https://www.theviralvista.com/new-oppo-mobile-phones-under-30000-rs/

    ReplyDelete

Note: Only a member of this blog may post a comment.