1、获取http请求nethod
ngx.req.get_method()
2、获取请求体body
ngx.req.get_body_data() ngx.req.read_body()
3、获取请求参数args
ngx.req.get_uri_args()
4、获取时间日期相关函数
ngx.today():本地时间,格式是yyyy-mm-dd,不含时分秒 ngx.localtime():本地时间,格式是yyyy-mm-dd hh:mm:ss ngx.utctime():UTC 时间,格式是yyyy-mm-dd hh:mm:ss ngx.time():当前的时间戳,即epoch以来的秒数 ngx.now():类似ngx.time,但返回的是浮点数,精确到毫秒 ngx.http_time():把时间戳转换为http时间格式 ngx.cookie_time():把时间戳转换为cookie时间格式 ngx.parse_http_time():解析http 时间格式,转换为时间戳
5、批量子请求
res1, res2, res3 = ngx.location.capture_multi{ { "/foo", { args = "a=3&b=4" } }, { "/bar" }, { "/baz", { method = ngx.HTTP_POST, body = "hello" } }, } --注意:这里省略了(),相当于({{}}) if res1.status == ngx.HTTP_OK then ... end
6、share_all_vars使用
location ~ /comment/([0-9]+) { internal; set $goodsId $1; set $dog "$dog world"; echo "$uri dog: $dog"; } location ~ /goods/detail/([0-9]+) { set $goodsId $1; default_type plain/text; set $dog 'hello'; content_by_lua_block{ local res = ngx.location.capture("/comment/"..ngx.var.goodsId.."?rank=5",{ method = ngx.HTTP_GET, share_all_vars = true, }) ngx.print(res.body) ngx.say(ngx.var.uri, ": ", ngx.var.dog) } }
7、ctx使用
location ~ /comment/([0-9]+) { internal; set $goodsId $1; content_by_lua_block{ ngx.ctx.foo = "bar" } } location ~ /goods/detail/([0-9]+) { set $goodsId $1; default_type plain/text; content_by_lua_block{ local c = {} local res = ngx.location.capture("/comment/"..ngx.var.goodsId.."?rank=5",{ method = ngx.HTTP_GET, ctx = c, }) ngx.say(c.foo) ngx.say(ngx.ctx.foo) } }
8、always_forward_body使用
local res = ngx.location.capture("/comment/"..ngx.var.goodsId.."?rank=5",{ method = ngx.HTTP_GET, body = 'hello, world', always_forward_body = false, --也可以设置为true })