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