{"id":49894,"date":"2024-10-08T08:23:15","date_gmt":"2024-10-08T00:23:15","guid":{"rendered":"https:\/\/server.hk\/cnblog\/49894\/"},"modified":"2024-10-08T08:23:16","modified_gmt":"2024-10-08T00:23:16","slug":"nginx-%e6%8a%80%e5%b7%a7%ef%bc%9a%e4%bd%bf%e7%94%a8-ngx_http_lua_module-%e5%af%a6%e7%8f%be-lua-%e8%85%b3%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/49894\/","title":{"rendered":"Nginx \u6280\u5de7\uff1a\u4f7f\u7528 ngx_http_lua_module \u5be6\u73fe Lua \u8173\u672c"},"content":{"rendered":"<h1 id=\"nginx-%e6%8a%80%e5%b7%a7%ef%bc%9a%e4%bd%bf%e7%94%a8-ngx_http_lua_module-%e5%af%a6%e7%8f%be-lua-%e8%85%b3%e6%9c%ac-AWCCCfhjZp\">Nginx \u6280\u5de7\uff1a\u4f7f\u7528 ngx_http_lua_module \u5be6\u73fe Lua \u8173\u672c<\/h1>\n<p>Nginx \u662f\u4e00\u500b\u9ad8\u6548\u80fd\u7684 HTTP \u4f3a\u670d\u5668\u548c\u53cd\u5411\u4ee3\u7406\u4f3a\u670d\u5668\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u7db2\u7ad9\u548c\u61c9\u7528\u7a0b\u5f0f\u4e2d\u3002\u96a8\u8457\u9700\u6c42\u7684\u589e\u52a0\uff0c\u958b\u767c\u8005\u5011\u5e0c\u671b\u80fd\u5920\u5728 Nginx \u4e2d\u57f7\u884c\u66f4\u8907\u96dc\u7684\u908f\u8f2f\uff0c\u9019\u6642\u5019 <code>ngx_http_lua_module<\/code> \u6a21\u7d44\u4fbf\u6210\u70ba\u4e86\u4e00\u500b\u7406\u60f3\u7684\u89e3\u6c7a\u65b9\u6848\u3002\u9019\u500b\u6a21\u7d44\u5141\u8a31\u7528\u6236\u5728 Nginx \u4e2d\u5d4c\u5165 Lua \u8173\u672c\uff0c\u5f9e\u800c\u5be6\u73fe\u66f4\u9748\u6d3b\u7684\u8acb\u6c42\u8655\u7406\u548c\u97ff\u61c9\u751f\u6210\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-ngx_http_lua_module%ef%bc%9f-AWCCCfhjZp\">\u4ec0\u9ebc\u662f ngx_http_lua_module\uff1f<\/h2>\n<p><code>ngx_http_lua_module<\/code> \u662f\u4e00\u500b Nginx \u6a21\u7d44\uff0c\u5141\u8a31\u7528\u6236\u4f7f\u7528 Lua \u8a9e\u8a00\u4f86\u7de8\u5beb Nginx \u7684\u914d\u7f6e\u548c\u908f\u8f2f\u3002\u9019\u500b\u6a21\u7d44\u7684\u4e3b\u8981\u512a\u52e2\u5728\u65bc\u5b83\u80fd\u5920\u5728 Nginx \u7684\u4e8b\u4ef6\u5faa\u74b0\u4e2d\u76f4\u63a5\u57f7\u884c Lua \u8173\u672c\uff0c\u5f9e\u800c\u63d0\u9ad8\u4e86\u6027\u80fd\u548c\u64f4\u5c55\u6027\u3002\u4f7f\u7528 Lua\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u8655\u7406\u8acb\u6c42\u3001\u751f\u6210\u97ff\u61c9\u3001\u8a2a\u554f\u6578\u64da\u5eab\u7b49\u3002<\/p>\n<h2 id=\"%e5%ae%89%e8%a3%9d-ngx_http_lua_module-AWCCCfhjZp\">\u5b89\u88dd ngx_http_lua_module<\/h2>\n<p>\u5728\u4f7f\u7528 <code>ngx_http_lua_module<\/code> \u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u78ba\u4fdd Nginx \u5df2\u7d93\u5b89\u88dd\u4e86\u8a72\u6a21\u7d44\u3002\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u6b65\u9a5f\u4f86\u5b89\u88dd\uff1a<\/p>\n<pre><code>sudo apt-get install libnginx-mod-http-lua\n<\/code><\/pre>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u547d\u4ee4\u6aa2\u67e5\u6a21\u7d44\u662f\u5426\u6210\u529f\u52a0\u8f09\uff1a<\/p>\n<pre><code>nginx -V 2&gt;&amp;1 | grep --with-http_lua_module\n<\/code><\/pre>\n<h2 id=\"%e5%9f%ba%e6%9c%ac%e7%94%a8%e6%b3%95%e7%a4%ba%e4%be%8b-AWCCCfhjZp\">\u57fa\u672c\u7528\u6cd5\u793a\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728 Nginx \u914d\u7f6e\u4e2d\u4f7f\u7528 Lua \u8173\u672c\u4f86\u8655\u7406\u8acb\u6c42\uff1a<\/p>\n<pre><code>http {\n    lua_shared_dict my_cache 10m;\n\n    server {\n        listen 80;\n        server_name example.com;\n\n        location \/hello {\n            content_by_lua_block {\n                ngx.say(\"Hello, World!\")\n            }\n        }\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u7576\u7528\u6236\u8a2a\u554f <code>\/hello<\/code> \u8def\u5f91\u6642\uff0cNginx \u5c07\u57f7\u884c Lua \u8173\u672c\u4e26\u8fd4\u56de &#8220;Hello, World!&#8221; \u7684\u97ff\u61c9\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8-lua-%e9%80%b2%e8%a1%8c%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c-AWCCCfhjZp\">\u4f7f\u7528 Lua \u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u8acb\u6c42\u8655\u7406\uff0c<code>ngx_http_lua_module<\/code> \u9084\u53ef\u4ee5\u7528\u65bc\u8207\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528 Lua \u9023\u63a5 MySQL \u6578\u64da\u5eab\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>http {\n    lua_package_path \"\/path\/to\/lua-resty-mysql\/lib\/?.lua;;\";\n\n    server {\n        listen 80;\n        server_name example.com;\n\n        location \/db {\n            content_by_lua_block {\n                local mysql = require \"resty.mysql\"\n                local db, err = mysql:new()\n\n                db:set_timeout(1000) -- 1 second\n\n                local ok, err, errcode, sqlstate = db:connect{\n                    host = \"127.0.0.1\",\n                    port = 3306,\n                    database = \"test\",\n                    user = \"user\",\n                    password = \"password\",\n                }\n\n                if not ok then\n                    ngx.say(\"failed to connect: \", err, \": \", errcode, \" \", sqlstate)\n                    return\n                end\n\n                local res, err, errcode, sqlstate = db:query(\"SELECT * FROM users\")\n                if not res then\n                    ngx.say(\"bad result: \", err, \": \", errcode, \" \", sqlstate)\n                    return\n                end\n\n                for i, row in ipairs(res) do\n                    ngx.say(row.id, \": \", row.name)\n                end\n\n                db:set_keepalive()\n            }\n        }\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u7576\u7528\u6236\u8a2a\u554f <code>\/db<\/code> \u8def\u5f91\u6642\uff0cNginx \u5c07\u9023\u63a5\u5230 MySQL \u6578\u64da\u5eab\u4e26\u67e5\u8a62\u7528\u6236\u8cc7\u6599\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-AWCCCfhjZp\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528 <code>ngx_http_lua_module<\/code>\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5728 Nginx \u4e2d\u9748\u6d3b\u5730\u57f7\u884c Lua \u8173\u672c\uff0c\u5f9e\u800c\u5be6\u73fe\u66f4\u8907\u96dc\u7684\u8acb\u6c42\u8655\u7406\u548c\u6578\u64da\u5eab\u64cd\u4f5c\u3002\u9019\u4e0d\u50c5\u63d0\u9ad8\u4e86 Nginx \u7684\u529f\u80fd\u6027\uff0c\u4e5f\u70ba\u958b\u767c\u8005\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u9748\u6d3b\u6027\u548c\u64f4\u5c55\u6027\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22 Nginx \u6280\u5de7\uff0c\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528 ngx_http_lua_module \u5be6\u73fe Lua \u8173\u672c\uff0c\u63d0\u5347\u4f3a\u670d\u5668\u6027\u80fd\u8207\u9748\u6d3b\u6027\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-49894","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/49894","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=49894"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/49894\/revisions"}],"predecessor-version":[{"id":49895,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/49894\/revisions\/49895"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=49894"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=49894"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=49894"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}