openresty-lua-redis案例

参考地址:https://github.com/openresty/lua-resty-redis

1。新建nginx配置文件:nginx-openresty-lua-redis.conf

指定配置文件:启动命令

Lua 复制代码
[root@localhost nginx]# ./sbin/nginx -p ./ -c conf/nginx-openresty-lua-redis.conf

文件内容:

[root@localhost nginx]# cat conf/nginx-openresty-lua-redis.conf

worker_processes  1;

error_log logs/error.log;

events {

worker_connections  1024;

}

http {

include       mime.types;

default_type  application/octet-stream;

sendfile        on;

keepalive_timeout  65;

server {

listen       8082;

server_name  localhost;

location / {

default_type text/html;

content_by_lua_file /usr/local/openresty/nginx/lua/lua-openresty-redis.lua; }

}

}

文件目录

[root@localhost nginx]# pwd /usr/local/openresty/nginx

[root@localhost nginx]#

2。修改lua文件:

Lua 复制代码
[root@localhost lua]# cat lua-openresty-redis.lua -- 引用resty的redis

local redis = require "resty.redis";

local red = redis:new(); -- 连接redis

local ok,err = red:connect("127.0.0.1",6379); if not ok then

ngx.say("faild to connect",err);

return end

ok,err = red:set("dKey","dValue"); if not ok then

ngx.say("failed to set dKey",err);

return end

ngx.say("dKey set dValue success") return

[root@localhost lua]#

读取redis的key对应的值

[root@localhost lua]# cat lua-openresty-redis.lua -- 引用resty的redis

local redis = require "resty.redis";

local red = redis:new(); -- 连接redis

local ok,err = red:connect("127.0.0.1",6379); if not ok then

ngx.say("faild to connect",err);

return end

ok,err = red:set("dKey","dValue"); if not ok then

ngx.say("failed to set dKey",err);

return end

ok,err = red:get("dKey") if not ok then

ngx.say("dKey is null") else

ngx.say("dKey's value is :"..ok) end

return

[root@localhost lua]#

分析OpenResty响应信息:

目的:为了修改以后的响应信息。

server {

listen 8081; location / {

default_type text/html;

content_by_lua_block {

ngx.say("hi block");

}

}

}

获取请求参数信息:

Lua 复制代码
修改nginx-param.conf

[root@localhost nginx]# ./sbin/nginx -p ./ -c conf/nginx-param.conf

[root@localhost nginx]# cat conf/nginx-param.conf worker_processes  1;

error_log  logs/error.log;

events {

worker_connections  1024;

}

http {

include       mime.types;

default_type  application/octet-stream;

server {

listen 8081; location / {

default_type text/html;

content_by_lua_file /usr/local/openresty/nginx/lua/lua-http-param.lua; }

}

}

[root@localhost nginx]#

lua-http-param.lua

[root@localhost lua]# cat lua-http-param.lua -- 获取get请求的参数

local arg = ngx.req.get_uri_args(); for k,v in pairs(arg)

do

ngx.say("key:",k,"   value:",v); end

[root@localhost lua]#

结合redis实践一下:

[root@localhost lua]# cat lua-http-param.lua -- 获取get请求的参数

local redis = require "resty.redis";

local red = redis:new();

red:connect("127.0.0.1",6379);

-- 省去链接错误的判断,前面课程中有

local arg = ngx.req.get_uri_args(); for k,v in pairs(arg)

do

ngx.say("key:",k,"   value:",v);

red:set(k,v); end

[root@localhost lua]#

获取请求头参数

Lua 复制代码
获取http请求中header参数。

lua脚本:

[root@localhost lua]# cat lua-header-param.lua -- 获取header参数

local headers = ngx.req.get_headers(); for k,v in pairs(headers)

do

ngx.say("[header] key:",k," value:",v); end

[root@localhost lua]#

nginx配置修改

[root@localhost conf]# cat nginx-param.conf worker_processes  1;

error_log  logs/error.log;

events {

worker_connections  1024;

}

http {

include       mime.types;

default_type  application/octet-stream;

server {

listen 8081; location / {

default_type text/html;

content_by_lua_file /usr/local/openresty/nginx/lua/lua-header-param.lua;

}

}

}

[root@localhost conf]#

获取post body 键值对 参数

bash 复制代码
nginx配置文件

[root@localhost conf]# cat nginx-param.conf worker_processes  1;

error_log  logs/error.log;

events {

worker_connections  1024;

}

http {

include       mime.types;

default_type  application/octet-stream;

server {

listen 8081; location / {

default_type text/html;

content_by_lua_file /usr/local/openresty/nginx/lua/lua-post-kv-param.lua;

}

}

}

[root@localhost conf]#

lua代码

[root@localhost lua]# cat lua-post-kv-param.lua

-- 获取post body kv参数 -- 重要:读取body

ngx.req.read_body();

local postArgs = ngx.req.get_post_args(); for k,v in pairs(postArgs)

do

ngx.say("[post] key:",k," value:",v); end

[root@localhost lua]#

获取body体信息

bash 复制代码
lua脚本:

[root@localhost lua]# cat lua-post-body-param.lua

-- 获取body体参数

-- 所有获取body的操作,这个很重要 ngx.req.read_body();

local body = ngx.req.get_body_data();

ngx.say(body);

[root@localhost lua]#

nginx配置文件

[root@localhost conf]# cat nginx-param.conf worker_processes  1;

error_log  logs/error.log;

events {

worker_connections  1024;

}

http {

include       mime.types;

default_type  application/octet-stream;

server {

listen 8081; location / {

default_type text/html;

content_by_lua_file /usr/local/openresty/nginx/lua/lua-post-body-param.lua;

}

}

}

[root@localhost conf]#
相关推荐
DemonAvenger11 分钟前
Redis与MySQL双剑合璧:缓存更新策略与数据一致性保障
数据库·redis·性能优化
想用offer打牌44 分钟前
面试官问Redis主从延迟导致脏数据读怎么解决?
redis·后端·面试
墨白曦煜2 小时前
深入剖析 Redis 客户端:Sentinel 模式下的“寻址”与“感知”艺术
数据库·redis·sentinel
遇见火星2 小时前
Redis高可用-哨兵模式(Sentinel)
redis·sentinel
鸽鸽程序猿3 小时前
【Redis】Java客户端使用Redis
java·redis·github
北城以北88883 小时前
SpringBoot--Redis基础知识
java·spring boot·redis·后端·intellij-idea
橘子134 小时前
Linux线程——一些概念(七)
java·redis·缓存
IMPYLH4 小时前
Lua 的 Math(数学) 模块
开发语言·笔记·lua
后端小张5 小时前
【Java 进阶】深入理解Redis:从基础应用到进阶实践全解析
java·开发语言·数据库·spring boot·redis·spring·缓存
LSL666_5 小时前
1 验证码
java·服务器·前端·redis·验证码