当前位置: 首页 > 工具软件 > Resty > 使用案例 >

openresty lua-resty-core 使用

尹钱青
2023-12-01

openresty lua-resty-core 使用

         

官网:https://github.com/openresty/lua-resty-core

             

                 

                                          

lua-resty-core 使用

         

This library is production ready
* lua-resty-core处于生产状态

This library is automatically loaded by default in OpenResty 1.15.8.1. 
This behavior can be disabled via the lua_load_resty_core directive, 
but note that the use of this library is vividly recommended, as its 
FFI implementation is both faster, safer, and more complete than the 
Lua C API of the ngx_lua module
* OpenResty从1.15.8.1开始,默认加载lua-resty-core
* 可通过lua_load_resty_core指令禁用,推荐使用lua-resty-core模块
* 相比于ngx_lua,lua-resty-core实现的api更快、更安全、功能更全面

        

resty.core.hash 编码

ngx.md5         ==> md5编码
ngx.md5_bin     ==> 以2进制形式返回md5编码值
ngx.sha1_bin    ==> 以2进制形式返回sha1编码值

         

resty.core.base64 编解码

ngx.encode_base64  ==>  base64编码
ngx.decode_base64  ==>  base64解码

            

resty.core.uri 编解码

ngx.escape_uri      ==>  uri编码
ngx.unescape_uri    ==>  uri解码

           

resty.core.regex 正则操作

ngx.re.match    ==>  查找第一个匹配的
ngx.re.gmatch   ==>  查找全部匹配的 

ngx.re.find     ==>  查找匹配的

ngx.re.sub      ==>  替换第一个匹配的
ngx.re.gsub     ==>  替换全部匹配的

             

resty.core.exit 退出

ngx.exit  ==>  强制退出

           

resty.core.shdict 共享内存

ngx.shared.DICT.get         ==>  获取缓存值
ngx.shared.DICT.get_stale   ==>  获取缓存值,可返回过期数据

ngx.shared.DICT.set         ==>  设置缓存值,可覆盖已经存在的缓存
ngx.shared.DICT.safe_set    ==>  设置缓存值,内存空间不足时不会替换已存在的值

ngx.shared.DICT.add         ==>  缓存不存在时添加缓存
ngx.shared.DICT.safe_add    ==>  内存空间不足时,不会添加缓存

ngx.shared.DICT.incr        ==>  缓存的value增加指定的值
ngx.shared.DICT.delete      ==>  删除缓存
ngx.shared.DICT.replace     ==>  替换缓存

ngx.shared.DICT.ttl         ==>  剩余存活时间
ngx.shared.DICT.expire      ==>  设置过期时间
ngx.shared.DICT.flush_all   ==>  使所有的key过期,但是不执行删除操作

ngx.shared.DICT.capacity    ==>  内存总空间
ngx.shared.DICT.free_space  ==>  查看剩余内存空间

             

resty.core.var 变量操作

ngx.var.VARIABLE    ==>  变量操作

            

resty.core.ctx 上下文

ngx.ctx       ==>  上下文操作

           

get_ctx_table 获取上下文

语法格式:ctx = resty.core.ctx.get_ctx_table(ctx?)
* 与ngx.ctx类似,获取上下文
* 如果设置了ctx参数,当不存在上下文时,直接返回ctx,不会创建新的ctx对象

             

resty.core.request 请求

ngx.req.get_headers    ==>  获取请求头
ngx.req.set_header     ==>  设置请求头
ngx.req.clear_header   ==>  清除请求头

ngx.req.get_method     ==>  获取请求方法
ngx.req.set_method     ==>  设置请求方法

ngx.req.get_uri_args   ==>  获取uri参数
ngx.req.start_time     ==>  获取请求开始时间

               

resty.core.response 响应

ngx.header.HEADER    ==>  响应头设置

           

resty.core.misc 三方模块

ngx.status               ==>  读写当前请求的响应状态
ngx.is_subrequest        ==>  是否是子请求
ngx.headers_sent         ==>  请求头是否发送
ngx.req.is_internal      ==>  当前请求是否是内部请求

           

resty.core.time 时间操作

ngx.time             ==>  当前时间的unix时间戳
ngx.now              ==>  当前时间的unix时间戳,精确到毫秒(小数点3位)

ngx.localtime        ==>  yyyy-MM-dd HH:mm:ss(所在时区的时间)
ngx.utctime          ==>  yyyy-MM-dd HH:mm:ss(0时区)

ngx.cookie_time      ==>  cookie过期时间,返回格式:Thu, 18 Nov 2010 11:27:35 GMT
ngx.http_time        ==>  返回一个可以做header头部(Expires、Last-Modified)时间的格式,
                          如:Thu, 18 Nov 2010 11:27:35 GMT

# 输入ngx.http_time(unix)格式的时间,输出unix
ngx.parse_http_time("Thu, 18 Nov 2010 11:27:35 GMT")

# 强制更新nginx时间,会影响nginx性能,不推荐使用
ngx.update_time

             

resty.core.worker 进程操作

ngx.worker.id         ==>  返回进程编号,从0开始,最大n-1
ngx.worker.pid        ==>  返回进程id

ngx.worker.count      ==>  进程数量
ngx.worker.exiting    ==>  退出进程

           

resty.core.phase 执行阶段

ngx.get_phase     ==>  获取当前执行阶段

            

resty.core.ndk 调用其他模块指令

ndk.set_var        ==>  调用其他模块指令

This mechanism allows calling other Nginx C modules' 
directives that are implemented by Nginx Devel Kit (NDK)'s 
set_var submodule's ndk_set_var_value
* 调用其他nginx c模块指令

            

resty.core.socket 端口操作

socket.setoption        ==>  设置参数
socket.setclientcert    ==>  设置客户端证书
socket.sslhandshake     ==>  加密连接

           

resty.core.param 参数操作

ngx.arg (getter only)      ==>  获取参数

           

ngx.semaphore 信号量

This Lua module implements a semaphore API for efficient 
"light thread" synchronization, which can work across different 
requests (but not across nginx worker processes)
* 限制并发执行的线程数

           

ngx.balancer 负载均衡

This Lua module implements for defining dynamic upstream balancers in Lua
* 实现动态负载均衡

                  

ngx.ssl 加密操作

# ngx.ssl
This Lua module provides a Lua API for controlling SSL 
certificates, private keys, SSL protocol versions, and 
etc in NGINX downstream SSL handshakes.
* ssl 加密认证


# ngx.ssl.clienthello
This Lua module provides a Lua API for post-processing SSL client 
hello message for NGINX downstream SSL connections.
* ssl客户端加密信息处理


# ngx.ssl.session
This Lua module provides a Lua API for manipulating SSL session 
data and IDs for NGINX downstream SSL connections.
* ssl session处理

           

ngx.re:字符串分割、改变正则匹配参数

This Lua module provides a Lua API which implements 
convenience utilities for the ngx.re API
* ngx.re api:split(字符串分割)、opt(改变正则匹配参数)

                       

ngx.resp 响应操作

This Lua module provides Lua API which could 
be used to handle HTTP response
* 处理http 响应

                                         

ngx.pipe 管道操作

This module provides a Lua API to spawn processes and 
communicate with them in a non-blocking fashion
* 以非阻塞方式执行

                                          

ngx.process 进程操作

This Lua module is used to manage the nginx process in Lua
* 管理nginx进程

                                                

ngx.errlog 错误日志操作

This Lua module provides Lua API to capture and 
manage nginx error log messages
* 捕获、处理错误日志

             

ngx.base64 编解码

This Lua module provides Lua API to urlsafe base64 encode/decode
* base64 编解码

                                                      

                                                        

                                          

使用示例

         

default.conf

server {
    listen       80;
    server_name  localhost;

    location / {
        root   /usr/local/openresty/nginx/html;
        index  index.html index.htm;
    }

    location /test {
        content_by_lua_block {
            local resp = require 'ngx.resp';

            resp.add_header("test", "瓜田李下");
            resp.add_header("test2", "海贼王");

            ngx.say("test");
        }
    }

    location /test2 {
        content_by_lua_block {
            local process = require 'ngx.process';

            ngx.say("进程类型: ", process.type());
            ngx.say("主进程id: ", process.get_master_pid() or "-");
        }
    }

    location /test3 {
        content_by_lua_block {
            local base64 = require 'ngx.base64';

            local res, err = base64.encode_base64url("gtlx");
            if err then
                ngx.say("base64编码出错 ==> ", err);
            end

            ngx.say("base64编码 ==> ", res);

            res, err = base64.decode_base64url(res);
            if err then
                ngx.say("base64解码出错 ==> ", err);
            end

            ngx.say("base64解码 ==> ", res);

        }
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/local/openresty/nginx/html;
    }

}

          

创建openresty容器

docker run -it -d --net fixed --ip 172.18.0.2 -p 9000:80 \
-v /Users/huli/lua/openresty/core/default.conf:/etc/nginx/conf.d/default.conf \
--name open-core lihu12344/openresty

           

使用测试

# ngx.resp 添加请求头
huli@hudeMacBook-Pro core % curl -i localhost:9000/test 
HTTP/1.1 200 OK
Server: openresty/1.21.4.1
Date: Thu, 28 Jul 2022 07:45:08 GMT
Content-Type: application/octet-stream
Transfer-Encoding: chunked
Connection: keep-alive
test: 瓜田李下
test2: 海贼王

test

# ngx.process 进程操作
huli@hudeMacBook-Pro core % curl localhost:9000/test2
进程类型: worker
主进程id: 1

# ngx.base64 编解码操作
huli@hudeMacBook-Pro core % curl localhost:9000/test3
base64编码 ==> Z3RseA
base64解码 ==> gtlx

          

                 

 类似资料: