ãããã¯ãã§Webãµã¼ãä¸ã®ç¾å¨æå»åºãããè¦ä»¶ãããã¾ããã
ãããããã®å¦çãªãã¢ããªã±ã¼ã·ã§ã³ä»ããã¨ãnginxã ãã§ããããã§ããã
ç¾å¨æå»ãããã¼ã«ã¤ãããªãnginx標準機能ã§åºæ¥ãããªãã§ããã
深淵ãªãçç±ããã³ã³ãã³ãã¨ãã¦æ¬²ããã¨ã®ãã¨ã
ä»å¾ãNginx+Lua試ãã¦ã¿ãããªã¼ã¨ããè¦ä»¶ãããã¾ãããã
ãã£ãããªã®ã§ãã®æ©è½ãLuaã¹ã¯ãªããã§æ¸ãã¦ã¿ããã¨ã«ãã¾ããã
Nginxã§Luaãåããç°å¢
ã¾ãç°å¢æ´ããã®ãè¥å¹²å¤§å¤ã§ããã*1
以ä¸ã®ãããªæé ã§ã¤ã³ã¹ãã¼ã«ããããã§ãã
ã©ã¤ãã©ãªã®ãã¹ãªã©æ°ãã¤ããªãã¨ãããããããã¾ãããã
nginxã¬ã·ã
opscodeã®nginxã¯ãã¯ããã¯ã«ã¯luaをインストールするレシピãããã¾ããã
ããããä¾ã®ãã¨ããã®ã¾ã¾ã§ã¯åãããªãã£ãã®ã§
ä¸é¨ãªã¼ãã¼ã©ã¤ãããã¯ãã¯ããã¯ãæ¸ãã¦ã¿ã¾ããã
AWSã®çAMIã«ããããã®ã¾ã¾ãããã¸ã§ãã³ã°ããã¨Nginx+Luaãèµ·åãããã¨ã確èªãã¦ãã¾ãã
https://github.com/toritori0318/chef-nginx-lua-sample
ç´ããã¨ãã
recipes/lua.rbã«ããããå½ã¦ãã®ã¨ã
recipes/source.rbã«luaãã¤ã³ã¹ãã¼ã«ããå¦çãå·®ãè¾¼ãã ãããã§ãã
ã¡ãªã¿ã«cjsonãå
¥ãã¦ããã®ã¯ãä»åjsonå½¢å¼ã§ã¬ã¹ãã³ã¹ããããã£ãããã§ãã
Luaãåããã ãã§ããã°cjsonãå
¥ããå¿
è¦ã¯ããã¾ããã
ãµã³ãã«ã¢ããªä»æ§
- /current_timeã«ã¢ã¯ã»ã¹ããã¨ãplainããã¹ãã§datetimeãè¿å´ããã
- df=epoch ãæå®ããã¦ããã¨ãã¨ããã¯ç§ãè¿å´ããã
- rf=json ãæå®ããã¦ããã¨ãjsonå½¢å¼ã§è¿å´ããã
ãµã³ãã«å®è£
/etc/nginx/sites-available/lua_current_time
nginxã®ã³ã³ãã£ã°ã§ãã
ç¹ã«èªãã¨ããã¯ããã¾ããã
# Lua soã©ã¤ãã©ãªã®ãã¹æå®ãä»åã¯cjsonã®ããï¼ãã¡ã¤ã«ãã¿ã¼ã³ãæå®ãå¿ è¦ãããï¼ lua_package_cpath "/usr/local/lib/lua/5.1/?.so"; server { listen 80; access_log /var/log/nginx/access.log; location /current_time { # å¤é¨Luaã¹ã¯ãªããèªã¿è¾¼ã¿ content_by_lua_file /etc/nginx/lua/current_time.lua; } location / { default_type 'text/plain'; # ã³ã¼ãããã®ã¾ã¾æ¸ãã content_by_lua "ngx.say('Hello,lua!')"; } }
/etc/nginx/lua/current_time.lua
ãã©ã¡ã¼ã¿ãåãåã£ã¦ã´ãã§ã´ãã§ãã¦ãã¾ãã
ç¹ã«èªãã¨ããã¯ããã¾ããã
local cjson = require "cjson" local ctime = "" local args = ngx.req.get_uri_args() if args.df == "epoch" then ctime = os.date("%s") else ctime = os.date("%Y-%m-%dT%H:%M:%S %z") end if args.rf == "json" then ngx.header.content_type = "application/json"; ngx.print(cjson.encode({current_time = ctime})) else ngx.header.content_type = "text/plain"; ngx.print(ctime) end
é常ã«ç°¡åã§ããã
ãã³ããã¼ã¯
abãã¦ã¿ã¾ããã
ã³ãã³ããã©ã¡ã¼ã¿ã¯ãããªæãã§ãã
ab -n 20000 -c 2 http://127.0.0.1/xxxx
/ (Hello Lua)
Server Software: nginx/1.4.3 Server Hostname: 127.0.0.1 Server Port: 80 Document Path: / Document Length: 11 bytes Concurrency Level: 2 Time taken for tests: 2.018 seconds Complete requests: 20000 Failed requests: 0 Write errors: 0 Total transferred: 3060000 bytes HTML transferred: 220000 bytes Requests per second: 9912.15 [#/sec] (mean) Time per request: 0.202 [ms] (mean) Time per request: 0.101 [ms] (mean, across all concurrent requests) Transfer rate: 1481.02 [Kbytes/sec] received
/current_time
Server Software: nginx/1.4.3 Server Hostname: 127.0.0.1 Server Port: 80 Document Path: /current_time Document Length: 25 bytes Concurrency Level: 2 Time taken for tests: 2.339 seconds Complete requests: 20000 Failed requests: 0 Write errors: 0 Total transferred: 3340000 bytes HTML transferred: 500000 bytes Requests per second: 8549.87 [#/sec] (mean) Time per request: 0.234 [ms] (mean) Time per request: 0.117 [ms] (mean, across all concurrent requests) Transfer rate: 1394.36 [Kbytes/sec] received
/static/index.html(éçãã¡ã¤ã«é ä¿¡ããã®ãã¡ã¤ã«ã¯æåã§ä½ãã¾ãã)
Server Software: nginx/1.4.3 Server Hostname: 127.0.0.1 Server Port: 80 Document Path: /static/index.html Document Length: 6 bytes Concurrency Level: 2 Time taken for tests: 1.942 seconds Complete requests: 20000 Failed requests: 0 Write errors: 0 Total transferred: 4680000 bytes HTML transferred: 120000 bytes Requests per second: 10298.39 [#/sec] (mean) Time per request: 0.194 [ms] (mean) Time per request: 0.097 [ms] (mean, across all concurrent requests) Transfer rate: 2353.34 [Kbytes/sec] received
éçãã¡ã¤ã«é
ä¿¡ãããããã«è½ã¡ã¾ããååæ©ãã
Nginx+Luaãç°å¢è¨å®ãè¥å¹²é¢åã§ããã
ä¸è¨ã¯ãã¯ããã¯ã使ãã¨ä¸çºã§ç°å¢ä½ãã¾ããã
Luaã¹ã¯ãªããããæ軽ã«æ¸ããããªã®ã§è©¦ãã¦ã¿ã¦ã¯ãããã§ããããã
(ï¾Ðï¾)ï½µLua!ï½µLua!
*1:â»è¿½è¨ openrestyã使ãã¨æ¥½ã§ããããã§ãï¼ http://openresty.org/