{"id":210,"date":"2013-12-05T14:27:41","date_gmt":"2013-12-05T06:27:41","guid":{"rendered":"http:\/\/www.xiaoxiaoliang.com\/?p=210"},"modified":"2013-12-05T14:27:41","modified_gmt":"2013-12-05T06:27:41","slug":"nginx%e4%bd%bf%e7%94%a8lua%e6%89%a7%e8%a1%8clinux%e8%84%9a%e6%9c%ac%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/www.xiaoxiaoliang.com\/?p=210","title":{"rendered":"Nginx\u4f7f\u7528lua\u6267\u884clinux\u811a\u672c\u6587\u4ef6"},"content":{"rendered":"<p>\u901a\u5e38\u60c5\u51b5\u4e0b;PHP\u670d\u52a1\u5668\u4f1a\u5bf9\u4e00\u4e9b\u7279\u6b8a\u7684\u51fd\u6570\u8fdb\u884c\u5c4f\u853d(\u5982:CHMOD),\u6216\u8005\u5f00\u542f\u5b89\u5168\u6a21\u5f0f; \u4f46\u6709\u65f6\u5019\u5728\u52a8\u6001\u811a\u672c\u7684\u65f6\u5019\u9700\u8981\u5b9e\u73b0\u6267\u884c\u811a\u672c,\u6216\u8bbe\u7f6e\u6743\u9650\u7b49\u64cd\u4f5c;\u5c31\u6ca1\u6cd5\u8fdb\u884c\u4e86; \u8fd9\u79cd\u670d\u52a1\u5668\u914d\u7f6e\u662f\u4e3a\u4e86\u5b89\u5168\u671f\u95f4;\u4ee5\u9632\u88ab\u4e0a\u4f20\u7684PHP\u4ee3\u7801\u6216\u4e00\u4e9b\u5176\u5b83\u6f0f\u6d1e(\u5982:eval\u7b49)\u6076\u610f\u6267\u884c\u7834\u574f\u6027\u8f83\u5927\u7684\u64cd\u4f5c.<\/p>\n<p>\u6240\u4ee5\u672c\u6587\u901a\u8fc7Nginx\u7684lua\u6a21\u5757\u6765\u5b9e\u73b0linux\u7684sh\u811a\u672c\u6587\u4ef6:<\/p>\n<pre class=\"lang:lua decode:true\">\t\tlocation \/swith_dev_lua {\n\t\t\tdefault_type 'text\/plain';\n\t\t\tcontent_by_lua '\n\t\t\t\tlocal log=\"\/home\/Www\/xxx\/swith_dev\/bash\/bash.log\"\n\t\t\t\tlocal shellfile=\"\/home\/Www\/xxx\/swith_dev\/bash\/bash\"\n\t\t\t\tlocal status = os.execute (\"\/bin\/bash \"..shellfile..\" &gt; \"..log..\" 2&gt;&amp;1\");\n\t\t\t\tlocal message=\"\"\n\t\t\t\tlocal file = io.open(log,\"r\")\n\t\t\t\tif file then\n\t\t\t\t\tfor line in file:lines() do\n\t\t\t\t\t\t\tmessage=message..line\t\t\t\n\t\t\t\t\tend\n\t\t\t\tend\n\t\t\t\tngx.say([[{\"status\" :\"]].. status ..[[\",\"message\":\"]].. ngx.encode_base64(message) ..[[\"}]]);\n\t\t\t';\n\t\t}<\/pre>\n<p>\u5728server\u6a21\u5757\u4e0b\u9762\u4f7f\u7528location\u914d\u7f6e\u9879\u6307\u5b9a\u4e00\u4e2a\u8def\u5f84,\u5982\u7b2c\u4e00\u884c\u8def\u5f84\u4e3aswitch_dev_lua,\u5c31\u8868\u793a\u5728\u8bbf\u95ee\u7ad9\u70b9switch_dev_lua(\u5982:http:\/\/URL\/switch_dev_lua)\u5c31\u4f1a\u6267\u884c\u76f8\u5e94location\u4e0b\u6240\u5b9a\u4e49\u7684\u914d\u7f6e<\/p>\n<p>content_by_lua \u540e\u7528\u5355\u5f15\u53f7\u6765\u5f15\u7528lua\u8bed\u8a00,\u81f3\u4e8enginx\u7684lua\u6a21\u5757\u7684\u4f7f\u7528\u53ef\u4ee5\u53c2\u8003\u4ee5\u4e0b\u624b\u518c:http:\/\/wiki.nginx.org\/HttpLuaModule<\/p>\n<p>\u5355\u5f15\u53f7\u91cc\u9762\u7684\u662flua\u8bed\u8a00; ngx.say()\u65b9\u6cd5\u662f\u5bf9\u5916\u8f93\u51fa\u5b57\u7b26\u4e32,os.execute()\u65b9\u6cd5\u662flua\u8bed\u8a00\u6267\u884c\u5916\u90e8\u547d\u4ee4\u7684\u4e00\u4e2a\u65b9\u6cd5,\u53ea\u6709\u4e00\u4e2a\u53c2\u6570,\u7528\u4e8e\u8868\u793a\u9700\u8981\u6267\u884c\u7684\u547d\u4ee4,\u6539\u51fd\u6570\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u6570\u503c\u7c7b\u578b,\u5f53\u6267\u884c\u6210\u529f\u540e\u8fd4\u56de\u96f6,\u975e\u96f6\u7684\u503c\u90fd\u662f\u6267\u884c\u5931\u8d25,\u800c\u6807\u51c6\u7684\u8f93\u51fa\u548c\u9519\u8bef\u8f93\u51fa\u662f\u9700\u8981\u901a\u8fc7\u5728\u6267\u884c\u547d\u4ee4\u65f6\u5019\u91cd\u5b9a\u5411\u5230\u4e00\u4e2a\u65e5\u5fd7\u6587\u4ef6\u4e2d,\u7136\u540e\u83b7\u53d6\u65e5\u5fd7\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\u4ece\u800c\u5f97\u5230\u547d\u4ee4\u6267\u884c\u7684\u8be6\u7ec6\u72b6\u51b5;<\/p>\n<p>\u6709\u5173\u4e8elua\u8bed\u8a00\u7684\u4e00\u4e9b\u8be6\u7ec6\u7684\u7528\u6cd5\u53ef\u4ee5\u53c2\u8003\u5176\u5b98\u65b9\u6587\u6863:http:\/\/www.lua.org\/manual\/5.2\/<\/p>\n<p>\u7b14\u8005\u5982\u6709\u673a\u4f1a,\u4f1a\u53e6\u5f00\u535a\u6587\u8be6\u7ec6\u4ecb\u7ecdnginx\u7684lua\u6a21\u5757\u7684\u8be6\u7ec6\u7528\u6cd5;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u901a\u5e38\u60c5\u51b5\u4e0b;PHP\u670d\u52a1\u5668\u4f1a\u5bf9\u4e00\u4e9b\u7279\u6b8a\u7684\u51fd\u6570\u8fdb\u884c\u5c4f\u853d(\u5982:CHMOD),\u6216\u8005\u5f00\u542f\u5b89\u5168\u6a21\u5f0f; \u4f46\u6709\u65f6\u5019\u5728\u52a8\u6001\u811a\u672c\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6,1],"tags":[18],"_links":{"self":[{"href":"https:\/\/www.xiaoxiaoliang.com\/index.php?rest_route=\/wp\/v2\/posts\/210"}],"collection":[{"href":"https:\/\/www.xiaoxiaoliang.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xiaoxiaoliang.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xiaoxiaoliang.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xiaoxiaoliang.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=210"}],"version-history":[{"count":0,"href":"https:\/\/www.xiaoxiaoliang.com\/index.php?rest_route=\/wp\/v2\/posts\/210\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.xiaoxiaoliang.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xiaoxiaoliang.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xiaoxiaoliang.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}