ngx映射到lua模块函数变量一览
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169ngx.arg[1] 脚本参数ngx.var['arg_a'] 取queryString的参数a...
OpenResty快速入门
OpenResty监听请求OpenResty的很多功能都依赖于其目录下的Lua库,需要在nginx.conf中指定依赖库的目录,并导入依赖: 1)添加对OpenResty的Lua模块的加载 修改/usr/local/openresty/nginx/conf/nginx.conf文件,在其中的http下面,添加下面代码: 1234#lua 模块lua_package_path "/usr/local/openresty/lualib/?.lua;;";#c模块 lua_package_cpath "/usr/local/openresty/lualib/?.so;;"; 2)监听/api/item路径 修改/usr/local/openresty/nginx/conf/nginx.conf文件,在nginx.conf的server下面,添加对/api/item这个路径的监听: 123456location /api/item { # 默认的响应类型 ...
Orange网关容器化改造
orange网关传统集群部署模式1、在orange.conf的 plugins中加入node,表示开启node插件(容器集群节点管理插件) 12345678910111213141516171819202122232425262728 "plugins": [ "stat", "headers", "monitor", "redirect", "rewrite", "rate_limiting", "property_rate_limiting", "basic_auth", "key_auth", "jwt_auth", "hmac_auth", ...
openresty(lua-API-cosocket,请求转发)
摘自:http://www.daileinote.com/computer/openresty/12 Openresty 提供了强大的 cosocket 功能,它源自 nginx 的 upstream 机制,但又超越了 upstream 机制,结合了 nginx 的事件机制和 Lua 协程特性,以同步非阻塞模式实现 socket 编程,可以高效的与后端服务器通信,而且还支持连接池功能。 ngx.socket.tcp123syntax: tcpsock = ngx.socket.tcp()context: rewrite_by_lua*, access_by_lua*, content_by_lua*, ngx.timer.*, \ ssl_certificate_by_lua*, ssl_session_fetch_by_lua* 创建一个 tcp 的 cosocket 对象,使用完后最好手动关闭或者通过 setkeepalive 存入连接池。如果没有关闭或者存入连接池,那么如下2中情况发生也会关闭。 1.当前请求的 Lua 句柄结束。2.被 Lua GC...
openresty(lua-API-共享内存区域)
摘自:http://www.daileinote.com/computer/openresty/11 通过 lua_shared_dict 指令可以声明一个共享内存区域,可以在多个 worker 进程间共享,单位支持 k、m,然后配合 ngx.shared.DICT api函数来操作。nginx -s reload 后共享内存的数据还在。 这个共享内存功能非常有用,极大的便利了worker 进程间的通信和协作,而且还提供了类似 redis 的数据结构,可以当做一个简易的数据库,而且没有通信开销。 共享内存在 Openresty 里用途很广,基于它可以实现流量统计、缓存、进程锁等高级功能。 12345678910111213141516171819202122232425http { lua_shared_dict dogs 10m; server { location /set { content_by_lua_block { local dogs =...
openresty(lua-API-基础,常量)
摘自:http://www.daileinote.com/computer/openresty/06 下面介绍的 Lua API 可以在 *_by_lua_block 和 *_by_lua_file 指定的 Lua 代码里调用,这些 API 很好的桥接了 nginx 和 Lua。 ngx.arg12syntax: val = ngx.arg[index]context: body_filter_by_lua* 在响应体过滤时候 ngx.arg[1] 代表响应体内容,ngx.arg[2] 代表该块的标志位。参照 配置指令 body_filter_by_lua_block。 ngx.var.VARIABLE这个指令可以获取 nginx 的变量,注意,在一次请求里 nginx 变量读取的时候会分配一次内存,在请求结束的时候会释放,所以如果需要反复使用该变量的值,可以保存为本地 lua 变量。 未定义的变量返回 nil,定义了未初始化的返回空字符串。 把变量设置为 nil,会清除该变量。 1ngx.var.args = nil 例子 123456789101112location...
openresty(lua-API-子请求,(内部)重定向)
摘自:http://www.daileinote.com/computer/openresty/07 内部子请求子请求在内部执行,非常高效。子请求会缓冲所有的响应体,如果响应体很大,建议用 cosockets 来操作。 子请求默认情况下会继承所有父请求的请求头。 ngx.location.capture1syntax: res = ngx.location.capture(uri, options?) 基本用法 1res = ngx.location.capture(uri) res是一个表格,包含4个成员 res.status 子请求响应的状态码res.header 表格的形式存放子请求的响应头,如果有同名多个响应头,那么值也为表格,比如假设响应头有3个 Set-Cookie。 123Set-Cookie: a=3Set-Cookie: foo=barSet-Cookie: baz=blah 那么 res.header["Set-Cookie"] 里的值为 1{"a=3", "foo=bar",...
openresty(lua-API-定时器-ngx.timer)
摘自:http://www.daileinote.com/computer/openresty/13 定时器一旦创建,就会脱离当前请求,也就是说就算当前请求结束了,定时器还是在的,它的生命周期跟创建它的 worker 进程一样。 ngx.timer.at1syntax: hdl, err = ngx.timer.at(delay, callback, user_arg1, user_arg2, ...) 创建定时器,到期时会回调 callback 函数。 delay 参数指定到期时间,单位为秒,精度0.001,代表1ms,也可以指定0,会立马过期并执行回调函数。 callback 回调函数第一个参数为 premature,接下来才是 user_arg1,user_arg2...,premature是个布尔值,代表是否是正常回调。 premature 为真的情况比如,nginx 准备关闭或者reload。 当定时器到期,回调函数以协程的方式运行,完全脱离创建该定时器的环境,对象生命周期跟创建它的worker进程一样,跟 cosockets...
openresty(lua-API-常用组件)
摘自:http://www.daileinote.com/computer/openresty/09 下面列出 lua-nginx-module 模块内置的一些组件。 ngx.escape_uri1syntax: newstr = ngx.escape_uri(str) 转义uri。 ngx.unescape_uri1syntax: newstr = ngx.unescape_uri(str) 取消转义。 12345678910location = /test { content_by_lua_block { ngx.say(ngx.escape_uri("http://www.freecls.com?name=freecls&age=28")) ngx.say(ngx.unescape_uri("http%3A%2F%2Fwww.freecls.com%3Fname%3Dfreecls%26age%3D28")) }...
openresty(lua-API-正则表达式)
摘自:http://www.daileinote.com/computer/openresty/10 ngx_lua 在表 ngx.re 里提供了5个正则表达式相关函数,它们的底层实现为 nginx 的 PCRE 库,并且支持 PCRE-JIT,速度非常快,完全可以代替 Lua 标准库的字符串匹配函数。 ngx.re.match1syntax: captures, err = ngx.re.match(subject, regex, options?, ctx?, res_table?) 返回第一个匹配,没匹配或报错返回 nil。 捕获的结果存在 captures 里,captures[0] 代表全部,captures[1] 代表第一个括号的捕获值,没捕获到的返回 nil。 1234567891011local m, err = ngx.re.match("hello, 1234", "[0-9]+")if m then -- m[0] == "1234"else if err then --出错 ...