{"id":697,"date":"2014-07-15T23:57:06","date_gmt":"2014-07-15T14:57:06","guid":{"rendered":"http:\/\/tech.akat.info\/?p=697"},"modified":"2014-12-31T21:09:13","modified_gmt":"2014-12-31T12:09:13","slug":"lua-nginx-memcached","status":"publish","type":"post","link":"https:\/\/tech.akat.info\/?p=697","title":{"rendered":"nginx-lua"},"content":{"rendered":"<h1>\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h1>\n<p>Lua\u306fC\u8a00\u8a9e\u306e\u30db\u30b9\u30c8\u30d7\u30ed\u30b0\u30e9\u30e0\u306b\u7d44\u307f\u8fbc\u307e\u308c\u308b\u3053\u3068\u3092\u76ee\u7684\u306b\u8a2d\u8a08\u3055\u308c\u3066\u304a\u308a\u3001\u9ad8\u901f\u3067\u52d5\u4f5c\u3059\u308b<br \/>\ndebian\u3067\u306f\u3001nginx-extras \u3082\u3057\u304f\u306f lua-nginx-module \u3092\u4f7f\u3046\u3068 Lua\u306e\u30b3\u30fc\u30c9\u3092\u901a\u3057\u3066Nginx\u3092\u5236\u5fa1\u53ef\u80fd<\/p>\n<pre class=\"brush: plain; title: nginx-extras; notranslate\" title=\"nginx-extras\">\r\nroot@hostname:\/home\/shimizu# aptitude install nginx-extras\r\n(\u4e2d\u7565)\r\nroot@hostname:\/home\/shimizu# aptitude search nginx | grep ^i\r\ni A nginx-common                    - small, powerful, scalable web\/proxy server\r\ni   nginx-extras                    - nginx web\/proxy server (extended version)\r\nroot@hostname:\/home\/shimizu# aptitude show nginx-extras | grep Lua\r\n THIRD PARTY MODULES: Auth PAM, Chunkin, DAV Ext, Echo, Embedded Lua,\r\n<\/pre>\n<pre class=\"brush: plain; title: lua-nginx-module; notranslate\" title=\"lua-nginx-module\">\r\nroot@hostname:\/home\/shimizu# lv \/etc\/apt\/sources.list\u3000# jessie(debian8)\u306e\u30d1\u30c3\u30b1\u30fc\u30b8\u306b\u5b58\u5728\r\n=============\r\ndeb http:\/\/ftp.jp.debian.org\/debian\/ jessie main contrib non-free\r\ndeb-src http:\/\/ftp.jp.debian.org\/debian\/ jessie main contrib non-free\r\n\r\ndeb http:\/\/security.debian.org\/ jessie\/updates main contrib non-free\r\ndeb-src http:\/\/security.debian.org\/ jessie\/updates main contrib non-free\r\n\r\n# jessie-updates, previously known as 'volatile'\r\ndeb http:\/\/ftp.jp.debian.org\/debian\/ jessie-updates main contrib non-free\r\ndeb-src http:\/\/ftp.jp.debian.org\/debian\/ jessie-updates main contrib non-free\r\n=============\r\nroot@hostname:\/home\/shimizu# aptitude install lua-nginx-memcached\r\n...(\u7565)\r\nroot@hostname:\/home\/shimizu# aptitude search nginx | grep ^i\r\ni   lua-nginx-memcached             - Pure Lua memcached client driver for the n\r\ni A nginx-common                    - small, powerful, scalable web\/proxy server\r\ni A nginx-extras                    - nginx web\/proxy server (extended version)\r\n<\/pre>\n<p>lua-nginx-memcached\u306f\u3001OpenResty\u3092\u3082\u3068\u306b\u3057\u305f\u30d1\u30c3\u30b1\u30fc\u30b8<br \/>\nOpenResty\u306f nginx-lua \u306e\u30e1\u30f3\u30c6\u30ca\u306e agentzh\u3055\u3093\u304c\u63d0\u4f9b\u3057\u3066\u3044\u308b\u30d0\u30f3\u30c9\u30eb<\/p>\n<h1>\u6982\u8981<\/h1>\n<p>nginx\u306f [server selection]\u2192[post read]\u2192&#8230;\u2192[post action]\u3068\u3044\u3046\u30d5\u30a7\u30fc\u30ba\u3067\u51e6\u7406\u3059\u308b<br \/>\nLua\u306fRewrite,Access,Content,Log\u306b\u3066\u30d5\u30c3\u30af\u3059\u308b\u4ed5\u7d44\u307f\u3092\u63d0\u4f9b\u3057\u3066\u3044\u308b<br \/>\nLua\u30b9\u30af\u30ea\u30d7\u30c8\u304b\u3089\u306f\u3001ngx.get_phase \u3092\u5229\u7528\u3057\u3066\u3001\u73fe\u5728\u306e\u30d5\u30a7\u30fc\u30ba\u3092\u53d6\u5f97\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b<br \/>\n<a href=\"http:\/\/tech.akat.info\/wp-content\/uploads\/2014\/07\/2014-07-20_195841.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/tech.akat.info\/wp-content\/uploads\/2014\/07\/2014-07-20_195841.png\" alt=\"2014-07-20_195841\" width=\"717\" height=\"628\" class=\"alignnone size-full wp-image-712\" \/><\/a><\/p>\n<h1>\u5229\u7528\u65b9\u6cd5<\/h1>\n<h2>rewrite_by_lua<\/h2>\n<p>access\u30d5\u30a7\u30fc\u30ba\u306e\u76f4\u524d\u306b\u5b9f\u884c\u3055\u308c\u308b<br \/>\n\u3064\u307e\u308a\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3060\u3068\u3001\/bar\u306b\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u3055\u308c\u3001500\u30a8\u30e9\u30fc\u306f\u8fd4\u5374\u3055\u308c\u306a\u3044<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nlocation \/foo {\r\n        rewrite_by_lua 'ngx.exit(503)';\r\n        rewrite ^ \/bar;\r\n}\r\n<\/pre>\n<h3>\u3061\u3087\u3063\u3068\u8907\u96d1\u306b\u307f\u3048\u308b\u4f8b<\/h3>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nlocation \/foo {\r\n        set $a 12; # create and initialize $a\r\n        set $b ''; # create and initialize $b\r\n        rewrite_by_lua 'ngx.var.b = tonumber(ngx.var.a) + 1';\r\n        if ($b = '13') {\r\n                rewrite ^ \/bar redirect;\r\n                break;\r\n        }\r\n        default_type 'text\/plain';\r\n        echo &quot;res = $b&quot;;\r\n}\r\n<\/pre>\n<p>\u5225\u30b5\u30fc\u30d0\u3088\u308a\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068\u3001<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nroot@debian:\/var\/www\/htdocs\/test# curl http:\/\/jpdebian.cloudapp.net\/foo\r\nres = 13\r\n<\/pre>\n<p>\u3068\u306a\u308a\u3001\/bar\u306b\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u3055\u308c\u306a\u3044\u3002\u3053\u308c\u306frewrite_by_lua\u3088\u308a\u3082if\u304c\u5148\u306b\u5b9f\u884c\u3055\u308c\u308b\u305f\u3081\u3067\u3042\u308a\u3001\u8a18\u8f09\u306e\u9806\u756a\u901a\u308a\u306b\u52d5\u4f5c\u3057\u306a\u3044<br \/>\n\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u3055\u305b\u308b\u305f\u3081\u306b\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8a18\u8f09\u3059\u308b(\/bar\u306b\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u3055\u308c\u3001echo\u306f\u5b9f\u884c\u3055\u308c\u306a\u3044)<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nlocation \/foo {\r\n    set $a 12; # create and initialize $a\r\n    set $b ''; # create and initialize $b\r\n    rewrite_by_lua '\r\n        ngx.var.b = tonumber(ngx.var.a) + 1\r\n        if tonumber(ngx.var.b) == 13 then\r\n            return ngx.redirect(&quot;\/bar&quot;);\r\n        end\r\n    ';\r\n\r\n    default_type 'text\/plain';\r\n    echo &quot;res = $b&quot;;\r\n}\r\n<\/pre>\n<h2>set_by_lua<\/h2>\n<p>\u5b9f\u884c\u3057\u305f\u30b3\u30fc\u30c9\u306e\u8fd4\u308a\u5024\u3092\u3001$res\u306b\u5165\u308c\u308b<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nlocation \/ {\r\n   # MIME type determined by default_type:\r\n    default_type 'text\/plain';\r\n    set $a &quot;hello&quot;;\r\n    set $b &quot;world&quot;;\r\n    # inline Lua script\r\n    set_by_lua $res &quot;return ngx.arg&#x5B;1]..ngx.arg&#x5B;2]&quot; $a $b;\r\n    echo $res;\r\n}\r\n<\/pre>\n<p>\u5225\u30b5\u30fc\u30d0\u3088\u308a\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068\u3001<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nroot@debian:\/var\/www\/htdocs\/test# curl http:\/\/jpdebian.cloudapp.net\r\nhelloworld\r\n<\/pre>\n<h2>ngx.say<\/h2>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nlocation \/ {\r\n        # MIME type determined by default_type:\r\n        default_type 'text\/plain';\r\n        content_by_lua &quot;ngx.say('Hello,world!')&quot;;\r\n}\r\n<\/pre>\n<p>\u5225\u30b5\u30fc\u30d0\u3088\u308a\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068\u3001<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nroot@debian:\/var\/www\/htdocs\/test# curl http:\/\/jpdebian.cloudapp.net\r\nhelloworld\r\n<\/pre>\n<p>\u30ea\u30af\u30a8\u30b9\u30c8\u30d8\u30c3\u30c0\u306e\u5185\u5bb9\u3092\u53d6\u5f97\u3059\u308b\u3053\u3068\u3082\u53ef\u80fd<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nlocation \/ {\r\ndefault_type 'text\/plain';\r\ncontent_by_lua 'ngx.say(&quot;Host: &quot;, ngx.req.get_headers()&#x5B;&quot;Host&quot;])';\r\n}\r\n<\/pre>\n<h2>ngx.location.caputure<\/h2>\n<p>\u30b5\u30d6\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u98db\u3070\u3059\u3053\u3068\u304c\u53ef\u80fd<br \/>\n\u305f\u3060\u3057\u3001\u4efb\u610f\u306eURL\u3078\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u304c\u3067\u304d\u308b\u308f\u3051\u3067\u306f\u306a\u304f\u3001nginx \u5185\u306e\u30a4\u30f3\u30bf\u30fc\u30ca\u30eb\u306a\u30ea\u30af\u30a8\u30b9\u30c8\u3057\u304b\u3067\u304d\u306a\u3044<br \/>\nrewrite_by_lua*, access_by_lua*, content_by_lua* \u306b\u3066\u5229\u7528\u53ef\u80fd<br \/>\n\u307e\u305f\u8fd4\u308a\u5024\u306f\u3001status, header, body \u306e3\u3064\u304b\u3089\u306a\u308b\u30c6\u30fc\u30d6\u30eb\u306b\u306a\u3063\u3066\u3044\u308b<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nlocation \/ {\r\ndefault_type 'text\/plain';\r\ncontent_by_lua '\r\nlocal res = ngx.location.capture(&quot;\/auth&quot;)\r\nngx.say(&quot;status: &quot;, res.status ,&quot; body: &quot;, res.body )\r\n';\r\n}\r\n\r\nlocation \/auth {\r\n     internal;\r\n     set $memcached_key abcde;\r\n     memcached_pass 127.0.0.1:11211;\r\n}\r\n<\/pre>\n<p>\u5225\u30b5\u30fc\u30d0\u3088\u308a\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068\u3001<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nroot@debian:\/var\/www\/htdocs\/test# curl http:\/\/jpdebian.cloudapp.net\r\nstatus: 200 body: 1\r\n<\/pre>\n<h2>content_by_lua<\/h2>\n<p>body\u751f\u6210\u51e6\u7406\u3059\u308b<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nlocation \/ {\r\n    # MIME type determined by default_type:\r\n    default_type 'text\/plain';\r\n\r\n    # try access \/?a=hello,world\r\n    content_by_lua &quot;ngx.print(ngx.var&#x5B;'arg_a'], '\\\\n')&quot;;\r\n}\r\n<\/pre>\n<p>\u5225\u30b5\u30fc\u30d0\u3088\u308a\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068\u3001<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nroot@debian:\/var\/www\/htdocs# curl http:\/\/jpdebian.cloudapp.net\/?a=hello,world!\r\nhello,world!\r\n<\/pre>\n<h2>ngx.var.(\u5909\u6570)<\/h2>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nlocation \/ {\r\n        set $value &quot;hoge&quot;;\r\n        default_type 'text\/plain';\r\n        content_by_lua 'ngx.say(ngx.var.value)';\r\n        }\r\n<\/pre>\n<p>\u5225\u30b5\u30fc\u30d0\u3088\u308a\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068\u3001<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nroot@debian:\/var\/www\/htdocs# curl http:\/\/jpdebian.cloudapp.net\r\nhoge\r\n<\/pre>\n<p>ngx.var.value \u3060\u3068 nginx\u5909\u6570$value\u3092\u53d6\u5f97\u3059\u308b(ngx.var.uri\u306a\u3069\u3082\u3088\u304f\u5229\u7528\u3055\u308c\u308b)<br \/>\n\u3053\u306e\u5909\u6570\u306f\u30ea\u30af\u30a8\u30b9\u30c8\u304c\u7d42\u308f\u308b\u305f\u3073\u306b\u30e1\u30e2\u30ea\u304b\u3089\u89e3\u653e\u3055\u308c\u308b\u306e\u3067\u3001\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u307e\u305f\u304c\u3063\u3066\u4fdd\u6301\u3057\u305f\u3044\u3082\u306e\u306f\u3001ngx.shared.DICT\u3092\u5229\u7528\u3059\u308b<\/p>\n<h2>if<\/h2>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nlocation \/ {\r\n    # MIME type determined by default_type:\r\n    default_type 'text\/plain';\r\n\r\n    content_by_lua '\r\n       local num = tonumber(ngx.var.arg_num) or 0\r\n\r\n       if num &gt; 50 then\r\n           ngx.say(&quot;num too big&quot;)\r\n           return\r\n       end\r\n\r\n       ngx.say(&quot;num is: &quot;, num)\r\n\r\n       if num &gt; 0 then\r\n           res = ngx.location.capture(&quot;\/recur?num=&quot; .. tostring(num - 1))\r\n           ngx.print(&quot;status=&quot;, res.status, &quot; &quot;)\r\n           ngx.print(&quot;body=&quot;, res.body)\r\n       else\r\n           ngx.say(&quot;end&quot;)\r\n       end\r\n       ';\r\n}\r\n<\/pre>\n<p>\u5225\u30b5\u30fc\u30d0\u3088\u308a\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068\u3001<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nroot@debian:\/var\/www\/htdocs# curl http:\/\/jpdebian.cloudapp.net\/?num=5\r\n<\/pre>\n<h1>\u53c2\u8003<\/h1>\n<p>http:\/\/wiki.nginx.org\/HttpLuaModule<br \/>\nhttp:\/\/d.hatena.ne.jp\/hiboma\/20120205\/1328448746<br \/>\nhttp:\/\/qiita.com\/kz_takatsu\/items\/e94805a8e3cc285f9b33<\/p>\n<div class=\"oceanwp-oembed-wrap clr\"><iframe loading=\"lazy\" src=\"https:\/\/www.slideshare.net\/slideshow\/embed_code\/key\/uSE98fgAjqpdF0\" width=\"427\" height=\"356\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" style=\"border:1px solid #CCC; border-width:1px; margin-bottom:5px; max-width: 100%;\" allowfullscreen> <\/iframe> <\/p>\n<div style=\"margin-bottom:5px\"> <strong> <a href=\"https:\/\/www.slideshare.net\/harukayon\/ngx-lua-public\" title=\"Using ngx_lua \/ lua-nginx-module in pixiv\" target=\"_blank\">Using ngx_lua \/ lua-nginx-module in pixiv<\/a> <\/strong> from <strong><a href=\"http:\/\/www.slideshare.net\/harukayon\" target=\"_blank\">Shunsuke Michii<\/a><\/strong> <\/div>\n<\/div>\n<div class=\"oceanwp-oembed-wrap clr\"><iframe loading=\"lazy\" src=\"https:\/\/www.slideshare.net\/slideshow\/embed_code\/key\/4NgMPGgcq5jsES\" width=\"427\" height=\"356\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" style=\"border:1px solid #CCC; border-width:1px; margin-bottom:5px; max-width: 100%;\" allowfullscreen> <\/iframe> <\/p>\n<div style=\"margin-bottom:5px\"> <strong> <a href=\"https:\/\/www.slideshare.net\/moriyoshi\/nginx-lua\" title=\"Nginx lua\" target=\"_blank\">Nginx lua<\/a> <\/strong> from <strong><a href=\"http:\/\/www.slideshare.net\/moriyoshi\" target=\"_blank\">Moriyoshi Koizumi<\/a><\/strong> <\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb Lua\u306fC\u8a00\u8a9e\u306e\u30db\u30b9\u30c8\u30d7\u30ed\u30b0\u30e9\u30e0\u306b\u7d44\u307f\u8fbc\u307e\u308c\u308b\u3053\u3068\u3092\u76ee\u7684\u306b\u8a2d\u8a08\u3055\u308c\u3066\u304a\u308a\u3001\u9ad8\u901f\u3067\u52d5\u4f5c\u3059\u308b debian\u3067\u306f\u3001nginx-extras \u3082\u3057\u304f\u306f lua-nginx-module \u3092\u4f7f\u3046\u3068 Lua\u306e\u30b3\u30fc\u30c9\u3092\u901a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[18],"tags":[],"_links":{"self":[{"href":"https:\/\/tech.akat.info\/index.php?rest_route=\/wp\/v2\/posts\/697"}],"collection":[{"href":"https:\/\/tech.akat.info\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tech.akat.info\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tech.akat.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tech.akat.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=697"}],"version-history":[{"count":18,"href":"https:\/\/tech.akat.info\/index.php?rest_route=\/wp\/v2\/posts\/697\/revisions"}],"predecessor-version":[{"id":1365,"href":"https:\/\/tech.akat.info\/index.php?rest_route=\/wp\/v2\/posts\/697\/revisions\/1365"}],"wp:attachment":[{"href":"https:\/\/tech.akat.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=697"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tech.akat.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=697"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tech.akat.info\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=697"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}