{"id":375,"date":"2019-06-11T11:09:35","date_gmt":"2019-06-11T03:09:35","guid":{"rendered":"https:\/\/www.aiqinqin.info\/?p=375"},"modified":"2019-06-11T11:16:17","modified_gmt":"2019-06-11T03:16:17","slug":"%e7%bb%93%e5%90%88redis%e5%92%8clua%e5%ae%9e%e7%8e%b0%e4%b8%aa%e6%80%a7%e5%8c%96%e5%9f%9f%e5%90%8d%e7%99%bb%e5%bd%95","status":"publish","type":"post","link":"https:\/\/www.aiqinqin.info\/?p=375","title":{"rendered":"\u7ed3\u5408redis\u548clua\u5b9e\u73b0\u4e2a\u6027\u5316\u57df\u540d\u767b\u5f55"},"content":{"rendered":"<blockquote><p>\n  \u4f7f\u7528\u573a\u666f\uff1a \u5ba2\u6237\u53ea\u8981\u901a\u8fc7\u540e\u53f0\u7ed1\u5b9a\u4e86\u81ea\u5df1\u7684\u57df\u540d\uff0c\u89e3\u6790\u5230\u6211\u4eec\u670d\u52a1\u5668\u4e4b\u540e\u3002\u5c31\u80fd\u901a\u8fc7\u7ed1\u5b9a\u7684\u57df\u540d\u7c7b\u578b\u8bbf\u95ee\u5230\u76f8\u5e94\u7684\u5bf9\u5e94\u8d44\u6e90\u3002\u5b9e\u73b0\u539f\u7406\u662f\uff1a\u5728\u7ed1\u5b9a\u7684\u65f6\u5019\u7a0b\u5e8f\u4f1a\u5c06\u7ed1\u5b9a\u57df\u540d\u548c\u7c7b\u578b\u5199\u5165\u5230redis\u4e2d\uff0c\u5e76\u6c38\u4e45\u4fdd\u5b58\uff0cNGINX\u901a\u8fc7lua\u8bfb\u53d6redis\u91cc\u9762\u7684\u6570\u636e\uff0c\u6307\u5b9a\u4e0d\u540c\u7684root\u8def\u5f84\u3002\n<\/p><\/blockquote>\n<h4>\u5b89\u88c5<\/h4>\n<ul>\n<li>\u5b89\u88c5luajit<\/li>\n<\/ul>\n<pre><code class=\"\">yum -y install lua lua-devel  ## \u7f16\u8bd1\u7684\u65f6\u5019\u4f1a\u4f9d\u8d56\u8fd9\u4e2a\ncd \/opt &amp;&amp; wget http:\/\/luajit.org\/download\/LuaJIT-2.0.5.tar.gz\ntar zxvf LuaJIT-2.0.5.tar.gz &amp;&amp; cd LuaJIT-2.0.5\nmake install PREFIX=\/opt\/luajit\nexport LUAJIT_LIB=\/opt\/luajit\/lib   ## \u7f16\u8bd1nginx\u9700\u8981\u7684\u53d8\u91cf\nexport LUAJIT_INC=\/opt\/luajit\/include\/luajit-2.0  ## \u7f16\u8bd1nginx\u9700\u8981\u7684\u53d8\u91cf\n<\/code><\/pre>\n<ul>\n<li>\u5b89\u88c5cjson\u5e93\uff0c\u7528\u6765\u89e3\u6790json\u683c\u5f0f\u6570\u636e\u7684<\/li>\n<\/ul>\n<pre><code class=\"\">cd \/opt &amp;&amp; wget https:\/\/www.kyne.com.au\/~mark\/software\/download\/lua-cjson-2.1.0.tar.gz\ntar zxvf lua-cjson-2.1.0.tar.gz &amp;&amp; cd lua-cjson-2.1.0\n##\u7f16\u8f91Makefile\u4fee\u6539\u4ee5\u4e0b\u51e0\u4e2a\u53c2\u6570\nvim Makefile\nPREFIX =            \/opt\/luajit\nLUA_INCLUDE_DIR =   \/opt\/luajit\/include\/luajit-2.0\nLUA_CMODULE_DIR =   \/opt\/luajit\/lib\nLUA_MODULE_DIR =    \/opt\/luajit\/share\/lua\/5.1\nLUA_BIN_DIR =       \/opt\/luajit\/bin\n##\u7f16\u8bd1\u5e76\u5b89\u88c5\nmake &amp;&amp; make install\n<\/code><\/pre>\n<ul>\n<li>\u5b89\u88c5cmsgpack\u5e93\uff0c\u4e3b\u8981\u7528\u6765\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u7684<\/li>\n<\/ul>\n<pre><code class=\"\">cd \/opt &amp;&amp; git clone https:\/\/github.com\/antirez\/lua-cmsgpack.git &amp;&amp; cd \/opt\/lua-cmsgpack\nmkdir build &amp;&amp; cd build &amp;&amp; cmake .. &amp;&amp; make\n<\/code><\/pre>\n<ul>\n<li>nginx\u7f16\u8bd1\u9700\u8981\u7684\u6a21\u5757<\/li>\n<\/ul>\n<pre><code class=\"\">cd \/opt &amp;&amp; git clone https:\/\/github.com\/simplresty\/ngx_devel_kit.git\ncd \/opt &amp;&amp; git clone https:\/\/github.com\/openresty\/lua-nginx-module.git\ncd \/opt &amp;&amp; git clone https:\/\/github.com\/openresty\/lua-resty-redis.git\n<\/code><\/pre>\n<ul>\n<li>\u89e3\u51b3\u4e00\u4e9b\u6a21\u5757\u548c\u5e93\u6587\u4ef6\u4f9d\u8d56\u95ee\u9898<\/li>\n<\/ul>\n<pre><code class=\"\">cp \/opt\/lua-cmsgpack\/build\/cmsgpack.so \/opt\/luajit\/lib\/\ncp \/opt\/lua-cmsgpack\/build\/cmsgpack.so \/opt\/luajit\/lib\/lua\/5.1\/\ncp \/opt\/luajit\/lib\/cjson.so \/opt\/luajit\/lib\/lua\/5.1\/\necho \"\/usr\/local\/lib\/\" &gt;&gt; \/etc\/ld.so.conf.d\/libc.conf\necho \"\/opt\/luajit\/lib\" &gt;&gt; \/etc\/ld.so.conf.d\/libc.conf\nldconfig\n<\/code><\/pre>\n<ul>\n<li>\u7f16\u8bd1nginx,\u590d\u5236\u7f16\u8bd1\u597d\u7684nginx\u4e8c\u8fdb\u5236\u6587\u4ef6\u66ff\u6362\u539f\u6765\u7684nginx(\u539f\u6765\u7684nginx\u4e8c\u8fdb\u5236\u6587\u4ef6\u505a\u5907\u4efd)<\/li>\n<\/ul>\n<pre><code class=\"\">tar zxvf nginx-1.14.0.tar.gz &amp;&amp; cd nginx-1.14.0\n.\/configure --add-module=\/opt\/lua-nginx-module --add-module=\/opt\/ngx_devel_kit --prefix=\/opt\/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_dav_module --with-http_flv_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_stub_status_module --with-http_sub_module --with-http_random_index_module --with-http_degradation_module --with-http_secure_link_module --with-http_gzip_static_module --with-http_perl_module --with-pcre --with-debug --with-file-aio --with-mail --with-mail_ssl_module --with-stream --with-ld-opt=-Wl,-E\nmake -j2\n<\/code><\/pre>\n<h4>\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801<\/h4>\n<ul>\n<li>\u5728nginx.conf\u4e2dhttp\u4e0b\u52a0\u5165redis\u7684\u652f\u6301: &#8220;lua_package_path &#8220;\/opt\/lua-resty-redis\/lib\/?.lua;;&#8221;;&#8221;<\/li>\n<li>lua.conf\u7684\u914d\u7f6e\u6587\u4ef6\u5982\u4e0b\uff1a<\/li>\n<\/ul>\n<pre><code class=\"\">server {\n    listen 80 default;\n    server_name _;  ## \u8fd9\u513f\u505a\u9ed8\u8ba4\u89e3\u6790\n    access_log  logs\/lua.log;\n    error_log  logs\/lua.error.log;\n    index index.html;\n\n    location \/ {\n        add_header 'Access-Control-Allow-Origin' '*';\n        set $defaultroot \/opt\/web\/default;\n        access_by_lua_file '\/opt\/nginx\/conf\/vhost\/lua.lua';\n        root $defaultroot;\n        try_files $uri $uri\/ \/index.html;\n    }\n<\/code><\/pre>\n<ul>\n<li>lua.lua\u7684\u914d\u7f6e\u6587\u4ef6\u5982\u4e0b\uff1a<\/li>\n<\/ul>\n<pre><code class=\"\">local redis = require \"resty.redis\"\nlocal cjson = require \"cjson\"\nlocal cmsgpack  = require \"cmsgpack\" \n\nlocal red = redis:new()\nred:set_timeout(1000)\nlocal ok, err = red:connect(\"127.0.0.1\", 6379)\nif not ok then\n   ngx.say(\"failed to connect: \", err)\n   return\nend\nred:select(1)\n\nlocal redisData = red:get(\"domain.key\")\nlocal getdata = cjson.decode(redisData);\n\nfor i,w in ipairs(getdata) do\n     if ngx.var.host == w.domain\n          then\n          if w.type == \"app1\"\n          then\n               ngx.var.defaultroot = \"\/opt\/web\/app1\"\n          elseif w.type == \"app2\"\n          then\n               ngx.var.defaultroot = \"\/opt\/web\/app2\"\n          else\n               ngx.var.defaultroot = \"\/opt\/web\/default\"\n          end\n     end\nend\nred:close()\n<\/code><\/pre>\n<ul>\n<li>redis\u4e2d\u53d6\u51fa\u6765\u7684json\u683c\u5f0f\u5982\u4e0b\uff1a<\/li>\n<\/ul>\n<pre><code class=\"\">[\n  {\n    \"domain\":\"domain1_name\",\n    \"id\":\"123456\",\n    \"type\":\"app1\"\n  },\n  {\n    \"domain\":\"domain2_name\",\n    \"id\":\"123457\",\n    \"type\":\"app2\"\n  },\n  {\n    \"domain\":\"domain3_name\",\n    \"id\":\"123458\",\n    \"type\":\"app3\"\n  }\n]\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528\u573a\u666f\uff1a \u5ba2\u6237\u53ea\u8981\u901a\u8fc7\u540e\u53f0\u7ed1\u5b9a\u4e86\u81ea\u5df1\u7684\u57df\u540d\uff0c\u89e3\u6790\u5230\u6211\u4eec\u670d\u52a1\u5668\u4e4b\u540e\u3002\u5c31\u80fd\u901a\u8fc7\u7ed1\u5b9a\u7684\u57df\u540d\u7c7b\u578b\u8bbf\u95ee\u5230\u76f8\u5e94\u7684\u5bf9\u5e94\u8d44\u6e90 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-375","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.aiqinqin.info\/index.php?rest_route=\/wp\/v2\/posts\/375","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.aiqinqin.info\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aiqinqin.info\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aiqinqin.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aiqinqin.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=375"}],"version-history":[{"count":5,"href":"https:\/\/www.aiqinqin.info\/index.php?rest_route=\/wp\/v2\/posts\/375\/revisions"}],"predecessor-version":[{"id":380,"href":"https:\/\/www.aiqinqin.info\/index.php?rest_route=\/wp\/v2\/posts\/375\/revisions\/380"}],"wp:attachment":[{"href":"https:\/\/www.aiqinqin.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aiqinqin.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=375"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aiqinqin.info\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}