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*