openresty ngx_lua常用指令


openresty ngx_lua常用指令

            

                    

                                   

请求中断

        

ngx.exit:请求中断

语法格式:ngx.exit(status)
* status >= 200:立刻中断请求,返回状态码
* status == 0:中断当前执行阶段,继续执行后续阶段

环境:rewrite_by_lua*, access_by_lua*, content_by_lua*, 
     header_filter_by_lua*, ngx.timer.*, balancer_by_lua*, 
     ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, 
     ssl_session_store_by_lua*, ssl_client_hello_by_lua*

            

请求状态码:status可以用数字、也可以是字面量(ngx.HTTP_OK

   value = ngx.HTTP_CONTINUE (100) (first added in the v0.9.20 release)
   value = ngx.HTTP_SWITCHING_PROTOCOLS (101) (first added in the v0.9.20 release)
   value = ngx.HTTP_OK (200)
   value = ngx.HTTP_CREATED (201)
   value = ngx.HTTP_ACCEPTED (202) (first added in the v0.9.20 release)
   value = ngx.HTTP_NO_CONTENT (204) (first added in the v0.9.20 release)
   value = ngx.HTTP_PARTIAL_CONTENT (206) (first added in the v0.9.20 release)
   value = ngx.HTTP_SPECIAL_RESPONSE (300)
   value = ngx.HTTP_MOVED_PERMANENTLY (301)
   value = ngx.HTTP_MOVED_TEMPORARILY (302)
   value = ngx.HTTP_SEE_OTHER (303)
   value = ngx.HTTP_NOT_MODIFIED (304)
   value = ngx.HTTP_TEMPORARY_REDIRECT (307) (first added in the v0.9.20 release)
   value = ngx.HTTP_PERMANENT_REDIRECT (308)
   value = ngx.HTTP_BAD_REQUEST (400)
   value = ngx.HTTP_UNAUTHORIZED (401)
   value = ngx.HTTP_PAYMENT_REQUIRED (402) (first added in the v0.9.20 release)
   value = ngx.HTTP_FORBIDDEN (403)
   value = ngx.HTTP_NOT_FOUND (404)
   value = ngx.HTTP_NOT_ALLOWED (405)
   value = ngx.HTTP_NOT_ACCEPTABLE (406) (first added in the v0.9.20 release)
   value = ngx.HTTP_REQUEST_TIMEOUT (408) (first added in the v0.9.20 release)
   value = ngx.HTTP_CONFLICT (409) (first added in the v0.9.20 release)
   value = ngx.HTTP_GONE (410)
   value = ngx.HTTP_UPGRADE_REQUIRED (426) (first added in the v0.9.20 release)
   value = ngx.HTTP_TOO_MANY_REQUESTS (429) (first added in the v0.9.20 release)
   value = ngx.HTTP_CLOSE (444) (first added in the v0.9.20 release)
   value = ngx.HTTP_ILLEGAL (451) (first added in the v0.9.20 release)
   value = ngx.HTTP_INTERNAL_SERVER_ERROR (500)
   value = ngx.HTTP_NOT_IMPLEMENTED (501)
   value = ngx.HTTP_METHOD_NOT_IMPLEMENTED (501) (kept for compatibility)
   value = ngx.HTTP_BAD_GATEWAY (502) (first added in the v0.9.20 release)
   value = ngx.HTTP_SERVICE_UNAVAILABLE (503)
   value = ngx.HTTP_GATEWAY_TIMEOUT (504) (first added in the v0.3.1rc38 release)
   value = ngx.HTTP_VERSION_NOT_SUPPORTED (505) (first added in the v0.9.20 release)
   value = ngx.HTTP_INSUFFICIENT_STORAGE (507) (first added in the v0.9.20 release)

           

core constants

ngx.exit accepts ngx.OK, ngx.ERROR, and ngx.DECLINED as input
* ngx.exit只接受ngx.OK, ngx.ERROR, ngx.DECLINED作为输入参数

   ngx.OK (0)
   ngx.ERROR (-1)
   ngx.AGAIN (-2)
   ngx.DONE (-4)
   ngx.DECLINED (-5)

          

示例

# 输出自定义内容
 ngx.status = ngx.HTTP_GONE
 ngx.say("This is our own content")
 -- to cause quit the whole request rather than the current phase handler
 ngx.exit(ngx.HTTP_OK)


 $ curl -i http://localhost/test
 HTTP/1.1 410 Gone
 Server: nginx/1.0.6
 Date: Thu, 15 Sep 2011 00:51:48 GMT
 Content-Type: text/plain
 Transfer-Encoding: chunked
 Connection: keep-alive

 This is our own content

         

              

                                   

断开连接

        

ngx.eof:告知客户端断开连接

语法格式:ok, err = ngx.eof()
* 告知客户端断开连接,服务端继续执行后续操作
* 如果后续有子请求,子请求会中断
* 设置指令:proxy_ignore_client_abort on;  ==> 子请求继续执行

环境:rewrite_by_lua*, access_by_lua*, content_by_lua*

                         

示例


 location = /async {
     keepalive_timeout 0;
     content_by_lua_block {
         ngx.say("got the task!")
         ngx.eof();     --向客户端发出连接中断请求

         ngx.sleep(3);  --服务端继续执行后续操作
         ngx.log(ngx.INFO, "执行ngx.eof");
     }
 }

        

            

                                   

请求休眠

        

ngx.sleep:请求休眠

语法格式:ngx.sleep(seconds)
* second可以是0.001s

环境:rewrite_by_lua*, access_by_lua*, content_by_lua*, ngx.timer.*, 
     ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, 
     ssl_client_hello_by_lua*

            

示例

location /test {

    content_by_lua_block {
        ngx.sleep(5);
        ngx.say("gtlx");
    }
}

          

                

                                   

获取时间

        

日期时间

ngx.today()      ==>  yyyy-MM-dd格式的时间
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(unix)   ==> cookie过期时间,返回格式:Thu, 18 Nov 2010 11:27:35 GMT
ngx.http_time(unix)     ==> 返回一个可以做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")

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

              

                         

                                   

编解码

        

编解码

# uri编解码
ngx.escape_uri(uri)     ==>  uri编码成字符串
ngx.unescape_uri(str)   ==>  字符串还原为uri

# 参数编解码
ngx.encode_args(table)             ==>  将table类型的数据编码为字符串(uri路径参数)
ngx.decode_args(str, max_args?)    ==>  将uri路径参数解码为table类型数据

# md5加密
ngx.md5(str)       ==> 对str进行md5加密,返回16进制字符串
ngx.md5_bin(str)   ==> 对str进行md5加密,返回2进制字符串

         

             

                                   

防止sql注入

        

ngx.quote_sql_str:按照mysql的sql格式,将字符串转换为sql语句

语法格式:quoted_value = ngx.quote_sql_str(raw_value)

环境:set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, 
     header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, 
     balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, 
     ssl_session_store_by_lua*, ssl_client_hello_by_lua*

Returns a quoted SQL string literal according to the MySQL quoting rules
* 根据mysql sql语法格式,返回sql语句

         

             

                                   

判断子请求

        

ngx.is_subrequest:判断是否是子请求

语法格式:alue = ngx.is_subrequest
* 当前请求是子请求,返回true
* 当前请求是http请求,返回false

环境:set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, 
     header_filter_by_lua*, body_filter_by_lua*, log_by_lua*

          

               

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在使用 `rewrite_by_lua_file` 指令时,可以通过 `ngx.req.set_uri_args` 函数将参数作为 URI 参数传递给 Lua 脚本。例如: ``` location /test { rewrite_by_lua_file /path/to/lua/script.lua; } ``` 在 Lua 脚本中,可以通过 `ngx.req.get_uri_args()` 获取 URI 参数。例如: ```lua local args = ngx.req.get_uri_args() local foo = args["foo"] local bar = args["bar"] ``` 如果需要在 `rewrite_by_lua_file` 指令中传递其他参数,可以使用 Lua 全局变量或者 `ngx.ctx` 共享数据。例如: ```lua -- 使用全局变量传递参数 my_var = "hello" -- 在 Lua 脚本中使用 my_var 变量 -- 使用 ngx.ctx 共享数据传递参数 ngx.ctx.my_var = "hello" -- 在 Lua 脚本中使用 ngx.ctx.my_var 变量 ``` 需要注意的是,`rewrite_by_lua_file` 指令在处理请求时会阻塞 Nginx worker 进程,因此应该尽量避免使用复杂的 Lua 代码或者进行耗时操作。 ### 回答2: 在openresty中,通过rewrite_by_lua_file命令可以在nginx的rewrite阶段引入Lua脚本文件来执行自定义的重写逻辑。而rewrite_by_lua_file还支持传递参数给Lua脚本。 要在Lua脚本中接收传递的参数,可以通过ngx.arg变量来获取。ngx.arg变量是一个table数据结构,其中包括两个字段,一个是request_args,另一个是uri_args。 request_args用于获取通过rewrite指令传递的参数,uri_args用于获取URI中的参数。 我们可以使用下列示例代码来说明: ``` -- nginx配置文件中的rewrite指令 location /rewrite { rewrite_by_lua_file /path/to/lua_script.lua?param1=value1&param2=value2; } ``` ``` -- lua_script.lua -- 获取rewrite指令中通过传参传递的参数 local request_args = ngx.arg.request_args ngx.say("通过rewrite传递的参数:", request_args) -- 输出:param1=value1&param2=value2 -- 获取URI中的参数 local uri_args = ngx.arg.uri_args ngx.say("URI中的参数:", uri_args) -- 输出:空 ``` 在这个示例中,通过rewrite指令传递了两个参数param1和param2,它们的值分别为value1和value2。在Lua脚本中,我们可以使用ngx.arg.request_args来获取通过rewrite指令传递的参数。 至于获取URI中的参数,则需要使用ngx.arg.uri_args方法来获取。但是在此示例中,我们没有在URI中传递参数,因此获取到的uri_args为空。 需要注意的是,在使用rewrite_by_lua_file命令时,如果要传递多个参数,可以通过使用&符号来分隔不同的参数。同时也需要注意对传递的参数进行URL编码,以避免出现特殊字符导致的错误。 总之,rewrite_by_lua_file命令可以传递参数给Lua脚本,并且可以使用ngx.arg来获取这些参数。 ### 回答3: 在OpenResty框架中,rewrite_by_lua_file是一个指令,用于通过Lua脚本对请求进行重写。它允许我们在Nginx的请求重写阶段使用Lua脚本来动态地改变请求的URI、请求头、请求方法等。 但是,rewrite_by_lua_file指令本身并不直接支持传递参数给Lua脚本。所以,我们需要通过其他方法来实现传参。 一种常见的方法是使用ngx.var变量。我们可以在Nginx配置文件中定义变量,然后在Lua脚本中读取这些变量以获取参数的值。例如: 在Nginx配置文件中: ``` location /example { set $my_param "hello"; rewrite_by_lua_file /path/to/lua_script.lua; } ``` 在Lua脚本中: ```lua local param = ngx.var.my_param ``` 通过这种方式,我们可以将参数值传递给Lua脚本进行处理。 另外,我们还可以使用ngx.req.set_uri_args方法来传递URL参数给Lua脚本。例如: 在Nginx配置文件中: ``` location /example { rewrite_by_lua_file /path/to/lua_script.lua; } ``` 在Lua脚本中: ```lua local args = ngx.req.get_uri_args() local param1 = args.param1 local param2 = args.param2 ``` 这样,我们可以在URL中传递参数,然后在Lua脚本中通过ngx.req.get_uri_args方法获取这些参数的值。 综上所述,虽然rewrite_by_lua_file指令本身不直接支持传参,但我们可以通过定义变量、使用ngx.req.get_uri_args等方法来实现传参给Lua脚本的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值