zoukankan      html  css  js  c++  java
  • openresty函数总结

    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
    })
    

      

  • 相关阅读:
    面向对象程序设计2020第二次作业
    工作日志。SQL篇
    正则表达式 转
    jquery ajax初级
    Javascript 面向对象编程
    C#开发的高性能EXCEL导入、导出工具DataPie(支持MSSQL、ORACLE、ACCESS,附源码下载地址)
    asp.net后台操作javascript:confirm返回值(转)
    SQL学习之索引(转)
    linq学习(一)
    SQL基础数据库执行及优化2012.06.02听课记录+资料收集
  • 原文地址:https://www.cnblogs.com/zk-blog/p/13668656.html
Copyright © 2011-2022 走看看