在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
git clone --branch master https://github.com/openresty/lua-resty-redis.git yum install openssl openssl-devel
tar xf LuaJIT-2.0.5.tar.gz
cd LuaJIT-2.0.5 && make && make install
tar xf tengine-2.2.0.tar.gz && cd tengine-2.2.0
./configure --prefix=/usr/local/tengine --with-http_lua_module --with-luajit-lib=/usr/local/lib/ --with-luajit-inc=/usr/local/include/luajit-2.0/ --with-lua-inc=/usr/local/include/luajit-2.0/ --with-lua-lib=/usr/local/lib/ --with-ld-opt=-Wl,-rpath,/usr/local/lib ua我们是手工安装的, 所以才会有后面的参数设置 --with-http_lua_module --with-luajit-lib=/usr/local/lib/ --with-luajit-inc=/usr/local/include/luajit-2.0/ --with-lua-inc=/usr/local/include/luajit-2.0/ --with-lua-lib=/usr/local/lib/ --with-ld-opt 其中的 --with-ld-opt=-Wl,-rpath,/usr/local/lib:/opt/openresty/luajit/lib 参数的意思是: https://groups.google.com/forum/#!msg/openresty/mVi0p-Qx7Qg/zNyrOZi-slgJ
cd /opt cp /opt/source/lua-resty-redis/lib/resty/redis.lua . git clone --branch master https://github.com/openresty/lua-resty-redis.git
测试部分 vim nginx.conf location @client{ proxy_pass http://client; } location @client_test{ proxy_pass http://client_test; } location /hello { location = /50x.html { root html; } } }
nginx+lua+redis 测试部分 worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; proxy_next_upstream error timeout; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $http_x_forwarded_for; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 100m; client_body_buffer_size 256k; proxy_connect_timeout 180; proxy_send_timeout 180; proxy_read_timeout 180; proxy_buffer_size 8k; proxy_buffers 8 64k; proxy_busy_buffers_size 128k; proxy_temp_file_write_size 128k; upstream client { server 192.168.5.101; } upstream client_test { server 192.168.5.101; } lua_package_path "/usr/local/verynginx/lualib/redis/redis.lua"; server { listen 80; location / { default_type 'text/plain'; content_by_lua ' local redis = require "resty.redis" local red = redis:new() local headers=ngx.req.get_headers() local ip=headers["X-REAL-IP"] or headers["X_FORWARDED_FOR"] or ngx.var.remote_addr or "0.0.0.0" red:set_timeout(1000) -- 1 sec ngx.say(ip) local ok, err = red:connect("192.168.5.101", 6379) if not ok then ngx.say("failed to connect: ", err) return end ok, err = red:set("dog", "an animal") if not ok then ngx.say("failed to set dog: ", err) return end ngx.say("set result: ", ok) local res, err = red:get("dog") if not res then ngx.say("failed to get dog: ", err) return end if res == ngx.null then ngx.say("dog not found.") return end ngx.say("dog: ", res) red:init_pipeline() red:set("cat", "Marry") red:set("horse", "Bob") red:get("cat") red:get("horse") local results, err = red:commit_pipeline() if not results then ngx.say("failed to commit the pipelined requests: ", err) return end for i, res in ipairs(results) do if type(res) == "table" then if not res[1] then ngx.say("failed to run command ", i, ": ", res[2]) else -- process the table value end else -- process the scalar value end end -- put it into the connection pool of size 100, -- with 10 seconds max idle time local ok, err = red:set_keepalive(10000, 100) if not ok then ngx.say("failed to set keepalive: ", err) return end -- or just close the connection right away: -- local ok, err = red:close() -- if not ok then -- ngx.say("failed to close: ", err) -- return -- end '; } location @client{ proxy_pass http://client; } location @client_test{ proxy_pass http://client_test; } location /hello { default_type 'text/plain'; content_by_lua 'ngx.say("hello, lua")'; } location = /50x.html { root html; } } }
|
请发表评论