ngx_lua模块学习笔记

ngx_lua的配置指令和ngx API

  • 配置指令:在ngx中使用,和set, pass_proxy等指令的使用方法一样,有适用上下文。
  • ngx API:lua脚本中访问ngx变量,调用ngx提供的函数。

配置指令

 - lua_code_cache

 1. 语法: lua_code_cache on | off; (默认为on)
 2. 作用:是否开启lua代码缓存。
 3. 注意:关闭(off)时,不用重启ngx服务,方便测试。生产环境,千万不要关闭该开关。
 - lua_transform_underscores_in_response_headers

 1. 语法: lua_transform_underscores_in_response_headers on|off; (默认为on)
 2. 作用:(下划线_是否等同短横线-)
      注:http头部名称大小写不敏感,该开关仅对_和-线进行是否区分处理。
     (1)当为默认设置on时:
         ngx.header.content_type = 'text/plain'    等同于: ngx.header["Content-Type"] = 'text/plain'
     (2)当设置为off时,(1)中的header就不等同。

ngx API

 - ngx.redirect

 1. 语法:ngx.redirect(uri, status = 302)
 2. 作用:临时(302)或永久重定向到uri, 响应头Location的值为该uri
 3. 示例:
        3.1  return ngx.redirect("/foo")   临时重定向到“/foo”,等效的rewrite指令配置:
                    rewrite ^ /foo? redirect;
        3.2  return ngx.redirect("http://www.baidu.com/")  临时重定向到外部uri
        3.3  return ngx.redirect("/foo", 301)  永久重定向到"/foo",等效的rewrite指令配置:
                    rewrite ^ /foo? permanent;
        3.4  return ngx.redirect("/foo?a=3&b=4")
 - ngx.escape_uri

 1. 语法:newstr = ngx.escape_uri(str)
 2. 作用:以urlEncode编码
 3. 示例:

 - ngx.unescape_uri

 1. 语法:newstr = ngx.unescape_uri(str)
 2. 作用: 以urlDecode解码
 3. 示例:
         3.1 newstr = ngx.unescape_uri("b%20r56+7")   得到:b r56 7
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值