• helloworld
    2019-07-08
    老师,文中的这句“OpenResty 的 HTTP 状态码中,有一个特别的常量:ngx.OK。当 ngx.exit(ngx.OK) 时,请求会退出当前处理阶段,进入下一个阶段,而不是直接返回给客户端。”
    ngx.OK应该不能算是HTTP状态码吧,它对应的值是0;
    我下面的理解对不对:
    ngx.exit(ngx.OK)、ngx.exit(ngx.ERROR)和ngx.exit(ngx.DECLINED)时,请求会退出当前处理阶段,进入下一个阶段;
    而当ngx.exit(ngx.HTTP_*)以ngx.HTTP_*各种HTTP状态码作为参数时,会直接响应给客户端。
    展开

    作者回复: ngx.ok 确实不是http状态码,它是 OpenResty 中的一个常量,值是0.
    ngx.exit 的官方文档正好可以解答你的问题:
    When status >= 200 (i.e., ngx.HTTP_OK and above), it will interrupt the execution of the current request and return status code to nginx.

    When status == 0 (i.e., ngx.OK), it will only quit the current phase handler (or the content handler if the content_by_lua* directive is used) and continue to run later phases (if any) for the current request.

    不过,里面并没有提到对于ngx.exit(ngx.ERROR)和ngx.exit(ngx.DECLINED)是如何处理的,我们可以自己来做个测试:
    location /lua {
            rewrite_by_lua "ngx.exit(ngx.ERROR)";
            echo hello;
        }
    访问这个 location,可以看到 http 响应码为空,响应体也是空。并没有引入下一个执行阶段。

    
     2
  • 回家
    2019-07-03
    利用ngx.exit返回非200时,请求会退出当前请求处理阶段,然后直接返回给客户端。在测试的时候发现一个特点是无论ngx.exit返回什么应答码,log_by_lua*阶段都会执行。本来以为直接返回给客户端,不会执行log阶段的,因为log阶段也是处理流程中的一个阶段。其实log阶段是在返回给客户端之后才会执行的一个阶段是吧。仔细想来也应该是这样:)

    作者回复: 👍

    
     2
  • 小强
    2019-07-30
    老师,openresty 可以做api鉴权吗?现在项目中网关用的spring cloud gateway和jwt,只做鉴权和代理,性能较差,适合往openresty迁移吗?

    作者回复: 当然可以,现在流行的 API 网关不少都是基于 OpenResty 的。JWT 的可以参考这里:https://github.com/iresty/apisix/blob/master/doc/plugins/jwt-auth-cn.md

    
     1
  • Jeff.Smile
    2019-07-05
    看到这里,才觉得接近了实战。^_^
    
    
  • 2xshu
    2019-07-05
    老师请教个 ngx.location.capture问题,看lua api官方文档如下:
    This API function (as well as ngx.location.capture_multi) always buffers the whole response body of the subrequest in memory.
    想问下,这个缓存的response body,是parent request结束就会释放掉吗?有什么办法可以验证我的猜想吗?

    作者回复: 是的,在 OpenResty 中 Lua 层面的内存都是自动管理的。
    是否可以使用反证法,如果在结束的时候没有释放,那么内存就是一直增长。用 wrk 或者 ab 发送几百个请求,然后看看内存的占用。

    
    
  • psoracle
    2019-07-04
    一直没搞明白为什么luajit提供的shell命令行中不能使用local 本地变量,可以定义声明,但使用时还是尝试从global index中查找,所以找不到。
    ![](http://wimgss.oss-cn-hangzhou.aliyuncs.com/2019-07-04-024647.png)

    作者回复: 官方 LuaJIT 并没有这个问题,OpenResty 带的 LuaJIT 确实会如此。我也不太清楚这么做的原因,可能是个 bug,你可以到https://github.com/openresty/luajit2提交 issue 询问下。

     2
    
  • 小飞哥 ‍超級會員
    2019-07-03
    看到这里,已经不知道应该如何操作了。
    
    
  • psoracle
    2019-07-03
    如果在resty中熟悉处理http请求的api,如ngx.req.http_version,我是使用resty启动一个mini http server,代码中打印当前访问的http 信息,使用curl -XMETHOD来调用
    ![](http://wimgss.oss-cn-hangzhou.aliyuncs.com/2019-07-03-034013.png)

    作者回复: content by lua 这个是 nginx 的指令,不能在 resty 里面调用

     2
    
  • manatee
    2019-07-03
    对于不同contenttype的请求体,有什么需要特别注意的吗

    作者回复: 并没有特别要注意的地方

     1
    
我们在线,来聊聊吧