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 参数的意思是: 
这是链接器选项,目的是把 /usr/local/lib 和 /opt/openrersty/luajit/lib 这两个路径添加进 
nginx 可执行文件的 RPATH 头中,这样在启动的时候,系统就可以找到正确的动态链接库文件。

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
###如果redis 里面的 myk == 1 就返回一个第一个节点,其他返回第二个节点。
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 127.0.0.1:8001;
}
upstream client_test {
server 127.0.0.1:8002;
}
lua_package_path "/opt/lua-resty-redis/lib/resty/redis.lua";
server {
listen 8888;
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
local ok, err = red:connect("192.168.5.101", 6379)
if not ok then
ngx.say("failed to connect: ", err)
return
end
local res, flags, err = red:get("myk")
if err then
ngx.say("error",err)
return
end
if res == "1" then
ngx.exec("@client")
return
end
ngx.exec("@client_test")
';
}
location /a{
content_by_lua '
ngx.exec("@client");
';
}

location @client{

proxy_pass http://client;

}

location @client_test{

proxy_pass http://client_test;

}

location /hello {
proxy_pass http://client;
}

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; } } }

nginx+lua+redis的更多相关文章

  1. Nginx+Lua+Redis 对请求进行限制

    Nginx+Lua+Redis 对请求进行限制 一.概述 需求:所有访问/myapi/**的请求必须是POST请求,而且根据请求参数过滤不符合规则的非法请求(黑名单), 这些请求一律不转发到后端服务器 ...

  2. Nginx+Lua+Redis整合实现高性能API接口 - 网站服务器 - LinuxTone | 运维专家网论坛 - 最棒的Linux运维与开源架构技术交流社区! - Powered by Discuz!

    Nginx+Lua+Redis整合实现高性能API接口 - 网站服务器 - LinuxTone | 运维专家网论坛 - 最棒的Linux运维与开源架构技术交流社区! - Powered by Disc ...

  3. nginx lua redis 访问频率限制(转)

    1. 需求分析 Nginx来处理访问控制的方法有多种,实现的效果也有多种,访问IP段,访问内容限制,访问频率限制等. 用Nginx+Lua+Redis来做访问限制主要是考虑到高并发环境下快速访问控制的 ...

  4. nginx+lua+redis构建高并发应用(转)

    nginx+lua+redis构建高并发应用 ngx_lua将lua嵌入到nginx,让nginx执行lua脚本,高并发,非阻塞的处理各种请求. url请求nginx服务器,然后lua查询redis, ...

  5. 基于nginx+lua+redis高性能api应用实践

    基于nginx+lua+redis高性能api应用实践 前言 比较传统的服务端程序(PHP.FAST CGI等),大多都是通过每产生一个请求,都会有一个进程与之相对应,请求处理完毕后相关进程自动释放. ...

  6. nginx限制请求之三:Nginx+Lua+Redis 对请求进行限制

    相关文章: <高可用服务设计之二:Rate limiting 限流与降级> <nginx限制请求之一:(ngx_http_limit_conn_module)模块> <n ...

  7. nginx+lua+redis 处理APK包替换

    nginx + lua +redis 安装与使用入门: http://huoding.com/2012/08/31/156 nginx httpEchoModule : http://wiki.ngi ...

  8. nginx+lua+redis实现灰度发布_test

    nginx+lua+redis实现灰度发布: 灰度发布是指在黑白之间能够平滑过渡的一种方式 AB test就是一种灰度发布方式,让一部分用户继续用A,一部分用户开始用B,如果用户对B没有什么反对意见, ...

  9. nginx+lua+redis初体验

    1.下载nginx.lua.redis nginx下载地址 wget  http://nginx.org/download/nginx-1.8.0.tar.gz lua下载地址 wget http:/ ...

随机推荐

  1. bzoj1833: [ZJOI2010]count 数字计数 && codevs1359 数字计数

    bzoj1833 codevs1359 这道题也是道数位dp 因为0有前导0这一说卡了很久 最后发现用所有位数减1~9的位数就okay.....orzczl大爷 其他就跟51nod那道统计1出现次数一 ...

  2. 【转】vs2015一键卸载干净

    插件是国外的一位同行写的,偶然在网上发现感觉挺好用,分享一下. 第二步.下载工具并解压 网盘下载地址:https://pan.baidu.com/s/1eSHRYxW 也可以在Github上下载最新版 ...

  3. equestAnimationFrame

    export const requestAnimationFrame = (() => { /* istanbul ignore next */ if (!inBrowser) { return ...

  4. CVE-2016-6662 mysql RCE测试

    参考:http://bobao.360.cn/learning/detail/3027.html ,我尝试第一种方法 1.先修改mysql_hookandroot_lib.c里面的反弹地址和端口: # ...

  5. Python与金融分析基础

    一 .ipython的简单使用 IPython:交互式的Python命令行 丰富的快捷键 TAB键自动完成 ?:内省.命名空间搜索 !:执行系统命令 魔术命令:以%开始的命令 %run:执行文件代码 ...

  6. 【C++】复制构造函数

    参考资料:黄邦勇帅(里面对于临时变量的说法我不是很理解,感觉里面的解释有问题) 用到复制构造函数的情况: 1.函数值传递 2.返回对象 3.用一个对象初始化另一个对象 重点注意下面两种情况: ① 只调 ...

  7. Selenium2+python自动化48-登录方法(参数化)【转载】

    前言 登录这个场景在写用例的时候经常会有,我们可以把登录封装成一个方法,然后把账号和密码参数化,这样以后用的登录的时候,只需调用这个方法就行了 一.登录方法 1.把输入账号.输入密码.点击登录按钮三个 ...

  8. 超详细saltstack安装部署及应用

    1.环境准备 准备两台虚拟机 主机名 ip role linux-node1 10.0.0.7 master linux-node2 10.0.0.8 minion 在节点1上安装 master 和 ...

  9. (三)mysql数据库基本操作

    (1)SQL语句:结构化查询语句 DDL语句 数据定义语言:数据库丶表丶视图丶索引丶存储过程丶函数丶create drop alter DML语句 数据库操作语言:插入数据insert,删除数据del ...

  10. PHP开发经常遇到的几个错误

    错误1:foreach循环后留下悬挂指针 在foreach循环中,如果我们需要更改迭代的元素或是为了提高效率,运用引用是一个好办法: $arr = array(1,2,3,4); foreach($a ...