当前位置: 首页 > 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.dtcms.com/a/324047.html

相关文章:

  • 无须炮解,打开即是Pro版
  • 商业银行元宇宙布局现状及未来图景三维思考
  • 基于BP神经网络PID控制的电机转速控制器设计复现
  • 使用tcp ntrip 协议 接收数据报错 java.net.SocketException: Connection reset
  • Shell脚本-变量的定义方式有哪些
  • TF-IDF提取关键词(附实战案例)
  • ​LabVIEW键盘鼠标监控
  • Python 2025:最新技术趋势与展望
  • [激光原理与应用-223]:机械 - 机加厂加工机械需要2D还是3D图?
  • 红楼梦文本数据分析
  • Fish shell的abbr命令行参数介绍和Bat文件查看工具
  • 诺基亚就4G/5G相关专利起诉吉利对中国汽车及蜂窝模组企业的影响
  • (一)Tailwindcss
  • 【开源】分层状态机(HFSM)解析:复杂逻辑的清晰表达与FPGA实现(附完整的Verilog交通灯案例及仿真)
  • Loki+Alloy+Grafana构建轻量级的日志分析系统
  • aurora接口ufc控流设计
  • Rust面试题及详细答案120道(11-18)-- 控制流与函数
  • Docker 镜像常见标签(如 `标准`、`slim`、`alpine` 和 `noble`)详细对比
  • 利用 SD-WAN 技术优化机房运维与网络安全评估的最佳实践
  • 2025下半年AI技术热点全景透视:从多模态革命到具身智能爆发
  • SpringMVC的知识点总结
  • Microsoft Store​​ 总是打不开页面怎么办
  • LeetCode131~150题解
  • opencv:图像轮廓检测与轮廓近似(附代码)
  • 浏览器CEFSharp88+X86+win7 之js交互开启(五)
  • 人工智能系列(8)如何实现无监督学习聚类(使用竞争学习)?
  • Lua基础+Lua数据类型
  • Java学习第一百二十一部分——HTTP
  • 超越基础!一文掌握CNN/Transformer/MoE架构,实战多模态AI(第三章)
  • 《解锁 C++ 基础密码:输入输出、缺省参数,函数重载与引用的精髓》