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

openresty-lua-redis案例

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

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

指定配置文件:启动命令

[root@localhost nginx]# ./sbin/nginx -p ./ -c conf/nginx-openresty-lua-redis.conf文件内容:[root@localhost nginx]# cat conf/nginx-openresty-lua-redis.confworker_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文件:

[root@localhost lua]# cat lua-openresty-redis.lua -- 引用resty的redislocal redis = require "resty.redis";local red = redis:new(); -- 连接redislocal ok,err = red:connect("127.0.0.1",6379); if not ok thenngx.say("faild to connect",err);return endok,err = red:set("dKey","dValue"); if not ok thenngx.say("failed to set dKey",err);return endngx.say("dKey set dValue success") return[root@localhost lua]#读取redis的key对应的值[root@localhost lua]# cat lua-openresty-redis.lua -- 引用resty的redislocal redis = require "resty.redis";local red = redis:new(); -- 连接redislocal ok,err = red:connect("127.0.0.1",6379); if not ok thenngx.say("faild to connect",err);return endok,err = red:set("dKey","dValue"); if not ok thenngx.say("failed to set dKey",err);return endok,err = red:get("dKey") if not ok thenngx.say("dKey is null") elsengx.say("dKey's value is :"..ok) endreturn[root@localhost lua]#分析OpenResty响应信息:目的:为了修改以后的响应信息。server {listen 8081; location / {default_type text/html;content_by_lua_block {ngx.say("hi block");}}}

获取请求参数信息:

修改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)dongx.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)dongx.say("key:",k,"   value:",v);red:set(k,v); end[root@localhost 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)dongx.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 键值对 参数

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参数 -- 重要:读取bodyngx.req.read_body();local postArgs = ngx.req.get_post_args(); for k,v in pairs(postArgs)dongx.say("[post] key:",k," value:",v); end[root@localhost lua]#

获取body体信息

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]#

http://www.lryc.cn/news/616294.html

相关文章:

  • 高标准农田建设—用ZDM画钢筋图并标注
  • BLDC直流无刷电机工作原理
  • 【无标题】消息队列(Message Queue)是一种**进程间通信(IPC)机制
  • VBA即用型代码手册:段落Paragraphs
  • GPT-5与中国AI发展(DeepSeek R1视角)
  • 补卡day16
  • Django @login_required实现登陆认证
  • A#语言详解
  • Redis - 使用 Redis HyperLogLog 进行高效基数统计
  • 安全生产基础知识(一)
  • django基于Python的设计师作品平台的数据可视化系统设计与实现
  • 音视频学习(五十二):ADTS
  • 集成电路学习:什么是Parameter Server参数服务器
  • 比特币现货和比特币合约的区别与联系
  • 联邦学习之------VT合谋
  • 刑法视野下的虚拟财产属性争议:法律风险与市场潜力解析
  • 机器学习——TF-IDF 衡量词语在文档中重要程度
  • MySQL 序列使用详细说明
  • SpringIoc 实践和应用--XML配置
  • PHP版本控制系统:高效文档管理
  • MVC结构变种——第三章核心视图及控制器的整体逻辑
  • 计算机网络---IP(互联网协议)
  • 学习分库分表的前置知识:高可用系统架构理论与实践
  • Android视图回调机制:从post到ViewTreeObserver,从源码分析到最佳实践
  • java组件漏洞
  • 【在线五子棋对战】十二、http请求处理
  • 从 GPT‑2 到 gpt‑oss:解析架构的迭代
  • C++移动语义、完美转发及编译器优化零拷贝
  • win11(RTX5060)下进行nanodetplus训练
  • 2025年全国青少年信息素养大赛Scratch编程践挑战赛-小低组-初赛-模拟题