Pro Yearly is on sale from $80 to $50! »

Nginx + mrubyを動かす / run the ngx_mruby

Nginx + mrubyを動かす / run the ngx_mruby

93c80c388fe9d8f9df7d030549a0ff0b?s=128

Takeshi Kondo

May 23, 2018
Tweet

Transcript

  1. /HJOY NSVCZΛಈ͔͢ LBXBTBLJSCप೥ه೦-5େձ LPOEPUBLFTIJ

  2. /HJOY NSVCZΛಈ͔͢ LBXBTBLJ SC प೥ه೦-5େձ LPOEPUBLFTIJ

  3. 1SPpMF w LPOEPUBLFTIJ w 4PGUXBSF&OHJOFFS w 4JUF3FMJBCJMJUZ&OHJOFFS w DIBTQZ@ w

    DIBTQZ w DIBTQZ w CMPHDIBTQZNF ༗څফԽதʂ
  4. /HJOY w ܰྔͰߴ͍εέʔϥϏϦςΟΛތΔΦʔϓϯιʔεͷ )551αʔόͰ͢ɻʮ͑Μ͡Μ͑ͬ͘͢ʯͱಡΈ·͢ɻ OHJOY࣮ફೖ໳OHJOY

  5. /HJOY w ΞΫηε੍ޚ w 63*3FXSJUF w H[JQѹॖ w ϦόʔεϓϩΩγ w

    -ϩʔυόϥϯε w ίϯςϯπΩϟογϡ w 44-λʔϛωʔγϣϯɺ)551ήʔτ΢ΣΠ w -ʢ5$1ʣϩʔυόϥϯε w ϝʔϧϓϩΩγ OHJOY࣮ફೖ໳OHJOY
  6. /HJOY w ΞΫηε੍ޚ w 63*3FXSJUF w H[JQѹॖ w ϦόʔεϓϩΩγ w

    -ϩʔυόϥϯε w ίϯςϯπΩϟογϡ w 44-λʔϛωʔγϣϯɺ)551ήʔτ΢ΣΠ w -ʢ5$1ʣϩʔυόϥϯε w ϝʔϧϓϩΩγ OHJOY࣮ફೖ໳OHJOY ͳΜͰ΋Ͱ͖͗͢
  7. NSVCZ w ૊ΈࠐΈ޲͚ܰྔ3VCZ w 3VCZޓ׵ w ଍Γͳ͍Ϋϥεɾػೳ͋Γ w ݴޠ࢓্༷ͷࠩҟ͋Γ ·ͭ΋ͱΏ͖ͻΖ௚఻ɹ૊ࠐ3VCZʮNSVCZʯͷ͢΂ͯ૯ूฤ

  8. OHY@NSVCZ w IUUQTHJUIVCDPNNBUTVNPUPSZOHY@NSVCZ w OHJOYͷNSVCZ֦ு w OHY@MVBͷNSVCZ൛ w NPE@NSVCZͷOHJOY൛

  9. /HJOYͷϑΣʔζͱϑοΫϙΠϯτ mruby nginx આ໌ mruby_server_rewrite_handle server rewrite αʔόίϯςΩετͷrewrite find config

    ֘౰͢Δlocation configΛ୳͢ mruby_rewrite_handler rewrite locationͷrewriteॲཧ post rewrite pre-access mruby_access_handler access ϑΝΠϧ΁ͷΞΫηε post-access try-files try_filesͷॲཧ mruby_content_handler content bodyͷੜ੒ mruby_log_handler log ϩάॻ͖ࠐΈ )551SFRVFTUQSPDFTTJOHQIBTFTJO/HJOYIUUQXXXOHJOYHVUTDPNQIBTFT
  10. ʢແବʹʣ΍ͬͨ͜ͱ w p[[CV[[ʹ(&5͢Δͱp[[CV[[ͷ݁ՌΛฦ͢ w SFEJSFDUʹ(&5͢Δͱp[[CV[[ʹϦμΠϨΫτ͢Δ IUUQTHJUIVCDPNDIBTQZWBHSBOUOHJOY

  11. ͜͜ʹp[[CV[[͕͋Δ͡ΌΖʁ r = Nginx::Request.new n = r.var.arg_n.to_i if n %

    15 == 0 ans = "FizzBuzz" elsif n % 3 == 0 ans = "Fizz" elsif n % 5 == 0 ans = "Buzz" else ans = n end Nginx.echo(ans) ʢ΅΍͖ʣIJHIMJHIU࢖͚ͬͨͲΠϚΠνͩͳʜ
  12. OHJOYDPOG http { server { location /fizzbuzz { mruby_content_handler /usr/local/nginx/conf/fizzbuzz.rb;

    } location /redirect { mruby_rewrite_handler_code ' Nginx.redirect "/fizzbuzz", Nginx::HTTP_MOVED_PERMANENTLY '; } } }
  13. ֬ೝ require 'net/http' (0..30).each{|n| Net::HTTP.get_print("192.168.33.100", "/fizzbuzz?n=#{n}") }

  14. খωλ w OJMUP@JͬͯͳΜͰ͢Ͷɻɻɻ [vagrant@nginx ~]$ /usr/local/src/ngx_mruby/mruby/bin/mirb mirb - Embeddable Interactive

    Ruby Shell > nil.to_i => 0
  15. ·ͱΊͱࠓޙ w OHJOYDPOG͕ΧΦεʹͳΔ৔߹ʹྑͦ͞͏ w JGσΟϨΫςΟϒ͸͋Δ͚ͲFMTFɺ"/% 03ͳ͍͠ w IUUQͱSVCZ෮श͠Α

  16. 5IBOLZPV