服务 Ngx_lua
-
date_range 19/06/2019 00:00
点击量:次infosort label
ngx_lua
API
init_by_lua用于http模块,常用于全局变量的申请-
init_worker_by_lua在每个nginx worker 进程启动时调用指定的lua代码 set_by_lua:设置一个变量,计算变量供后续使用-
access_by_lua:修改请求参数 content_by_lua:由ngx返回内容,而不走proxied后端header_filter_by_lua:用来修改后端的response的header-
body_filter_by_lua:一般在一次请求中被调用多次,因为这是实现基于 HTTP 1.1 chunked 编码的所谓“流式输出”的。 -
log_by_lua:在请求结束的时候运行,可以做统计工作 -
ngx.cookie_time:ngx.cookie_time(ngx.time() + 60 * 30) – 设置Cookie过期时间为30分钟 -
ngx.ctx:当前请求的上下文 -
ngx.decode_args:decode 为 table -
gx.encode_args:将table编码为表单提交格式,a1=arg1&a2=arg2 -
ngx.escape_url:url编码 -
ngx.exec:内部重定向 -
ngx.exit:当传入的status >= 200(200即为ngx.HTTP_OK),ngx.exit() 会中断当前请求,并将传入的状态码(status)返回给nginx。当传入的status == 0(0即为ngx.OK)则 ngx.exit() 会中断当前执行的phrase(ngx-lua模块处理请求的阶段,如content_by_lua*),进而继续执行下面的phrase。
-
ngx.flush:设置为true的话,则ngx.print或者ngx.say的内容等写入send buffer之后才返回
-
ngx.get_phase:返回当前的处理阶段 -
ngx.http_time:时间函数 -
ngx.is_subrequest:如果subrequest则返回true -
ngx.localtime:从NGINX’s cache中返回yyyy-mm-dd hh:mm:ss格式的时间 -
ngx.location.capture:用于子请求 -
ngx.log:第一个参数是log基本(one of ngx.STDERR, ngx.EMERG, ngx.ALERT,ngx.CRIT, ngx.ERR, ngx.WARN, ngx.NOTICE, ngx.INFO, and ngx.DEBUG) 后续可以接多个参数来打印log -
ngx.now从NGINX’s cache返回epoch time以来的毫秒数 ngx.now() 是有误差的,因为使用了nginx 自身的时间缓存。对于精度要求较高的计时,应使用下面的调用序列: -
ngx.parse_http_time -
ngx.print打印到response body -
ngx.say打印到response body并换行 -
ngx.statushttp status状态码 -
ngx.time从nginx cached返回epoch time以来的秒数(no syscall involved unlike Lua’s date library). -
ngx.today从NGINX’s cache返回当前日期,格式 yyyy-mm-dd -
ngx.unescape_uri: -
ngx.update_time更新NGINX‘s time cache ngx.utctime:从NGINX’s cache返回UTC time,格式yyyy-mm-dd hh:mm:ss
