当前位置: 首页 > nginx > 正文

nginx+lua module直接调用redis实现url跳转

需求:想要实现的功能是lua通过id取redis中对应的url然后进行url跳转。

nginx编译用到的模块:
git clone https://github.com/simpl/ngx_devel_kit.git
git clone https://github.com/chaoslawful/lua-nginx-module
git clone https://github.com/agentzh/redis2-nginx-module.git
git clone https://github.com/agentzh/set-misc-nginx-module.git
git clone https://github.com/agentzh/echo-nginx-module.git
git clone https://github.com/catap/ngx_http_upstream_keepalive.git

安装lua-redis-parser
git clone https://github.com/agentzh/lua-redis-parser.git
export LUA_INCLUDE_DIR=/usr/include/
make
make install

nginx 需要编译参数
./confgiure \
--add-module=../ngx_devel_kit \
--add-module=../echo-nginx-module \
--add-module=../lua-nginx-module \
--add-module=../redis2-nginx-module \
--add-module=../set-misc-nginx-module \
--add-module=../ngx_http_upstream_keepalive \
--add-module=../ngx_http_oupeng_redir_module

链接parse.so,应该还有其他方式,也可以直接引入parser.lua
mkdir /usr/lib64/lua/5.1/redis/
ln -s /usr/local/lib/lua/5.1/redis/parser.so /usr/lib64/lua/5.1/redis/parser.so

nginx配置文件nginx.conf:

lua_code_cache off; 如果为off则lua文件修改时需要重新reload nginx才能生效

upstream redis_pool {
server 127.0.0.1:6379;
keepalive 1024 single;
}

server {
listen 80;
server_name 2hei.net;
#charset utf-8;
#access_log logs/2hei.net.access.log main;

location /check_redis{
#internal;
default_type ‘text/plain’;
set_unescape_uri $key $arg_key;
redis2_query get $key;
redis2_pass redis_pool;
}

location /lua_click.php {
default_type ‘text/plain’;
content_by_lua_file /usr/local/nginx/conf/lua/redir.lua;
}

cat redir.lua
local json = require("json")
local parser = require("redis.parser")
local res = ngx.location.capture("/check_redis",{
args = { key = ngx.var.arg_key }
})

if res.status == 200 then
reply = parser.parse_reply(res.body)
value = json.encode(reply)
strs = json.decode(value)
return ngx.redirect(strs,ngx.HTTP_MOVED_TEMPORARILY)
end

本文固定链接: https://www.2hei.net/2013/05/24/nginxlua-module%e7%9b%b4%e6%8e%a5%e8%b0%83%e7%94%a8redis%e5%ae%9e%e7%8e%b0url%e8%b7%b3%e8%bd%ac/ | 2hei.net

该日志由 u2 于2013年05月24日发表在 nginx 分类下,
原创文章转载请注明: nginx+lua module直接调用redis实现url跳转 | 2hei.net
关键字: , ,

报歉!评论已关闭.