目录

Nginx - Lua模块文档表

笔记

这里是 ngx_lua 模块的文档表总结。

2021-11-30 @Young Kbt

OpenRestry 中文文档 (opens new window)

# 常量

常量 说明
Core constants ngx.OK       (0)
ngx.ERROR     (-1)
ngx.AGAIN     (-2)
ngx.DONE     (-4)
ngx.DECLINED   (-5)
ngx.nil
HTTP method constants ngx.HTTP_GET
ngx.HTTP_HEAD
ngx.HTTP_PUT
ngx.HTTP_POST
ngx.HTTP_DELETE
ngx.HTTP_OPTIONS
ngx.HTTP_MKCOL
ngx.HTTP_COPY
ngx.HTTP_MOVE
ngx.HTTP_PROPFIND
ngx.HTTP_PROPPATCH
ngx.HTTP_LOCK
ngx.HTTP_UNLOCK
ngx.HTTP_PATCH
ngx.HTTP_TRACE
HTTP status constants ngx.HTTP_OK              (200)
ngx.HTTP_CREATED            (201)
ngx.HTTP_SPECIAL_RESPONSE       (300)
ngx.HTTP_MOVED_PERMANENTLY     (301)
ngx.HTTP_MOVED_TEMPORARILY     (302)
ngx.HTTP_SEE_OTHER          (303)
ngx.HTTP_NOT_MODIFIED        (304)
ngx.HTTP_BAD_REQUEST         (400)
ngx.HTTP_UNAUTHORIZED        (401)
ngx.HTTP_FORBIDDEN          (403)
ngx.HTTP_NOT_FOUND          (404)
ngx.HTTP_NOT_ALLOWED         (405)
ngx.HTTP_GONE             (410)
ngx.HTTP_INTERNAL_SERVER_ERROR    (500)
ngx.HTTP_METHOD_NOT_IMPLEMENTED  (501)
ngx.HTTP_SERVICE_UNAVAILABLE      (503)
ngx.HTTP_GATEWAY_TIMEOUT       (504)
Nginx log level constants ngx.STDERR
ngx.EMERG
ngx.ALERT
ngx.CRIT
ngx.ERR
ngx.WARN
ngx.NOTICE
ngx.INFO
ngx.DEBUG

# API

API 说明
ngx.arg 指令参数,如跟在 content_by_lua_file 后面的参数
ngx.var 变量,ngx.var.VARIABLE 引用某个变量
ngx.ctx 请求的 lua 上下文
ngx.header 响应头,ngx.header.HEADER 引用某个头
ngx.status 响应码
ngx.log 输出到 error.log
print 等价于 ngx.log(ngx.NOTICE, ...)
ngx.send_headers 发送响应头
ngx.headers_sent 响应头是否已发送
ngx.resp.get_headers 获取响应头
ngx.timer.at 注册定时器事件
ngx.is_subrequest 当前请求是否是子请求
ngx.location.capture 发布一个子请求
ngx.location.capture_multi 发布多个子请求
ngx.exec 执行一个内部 localtion 模块跳转
ngx.redirect
ngx.print 输出响应
ngx.say 输出响应,自动添加『 \n 』
ngx.flush 刷新响应
ngx.exit 结束请求
ngx.eof
ngx.sleep 无阻塞的休眠(使用定时器实现)
ngx.get_phase
ngx.on_abort 注册 client 断开请求时的回调函数
ndk.set_var.DIRECTIVE
ngx.req.start_time 请求的开始时间
ngx.req.http_version 请求的 HTTP 版本号
ngx.req.raw_header 请求头(包括请求行)
ngx.req.get_method 请求方法
ngx.req.set_method 请求方法重载
ngx.req.set_uri 请求URL重写
ngx.req.set_uri_args
ngx.req.get_uri_args 获取请求参数
ngx.req.get_post_args 获取请求表单
ngx.req.get_headers 获取请求头
ngx.req.set_header 设置数据到请求头
ngx.req.clear_header 清除请求头数据
ngx.req.read_body 读取请求体
ngx.req.discard_body 扔掉请求体
ngx.req.get_body_data 获取请求体数据
ngx.req.get_body_file 后面跟 lua 文件
ngx.req.set_body_data 设置数据到请求体
ngx.req.set_body_file
ngx.req.init_body
ngx.req.append_body
ngx.req.finish_body
ngx.req.socket
ngx.escape_uri 字符串的 URL 编码
ngx.unescape_uri 字符串 URL 解码
ngx.encode_args 将 table 编码为一个参数字符串
ngx.decode_args 将参数字符串编码为一个t able
ngx.encode_base64 字符串的 base64 编码
ngx.decode_base64 字符串的 base64 解码
ngx.crc32_short 字符串的 crs32_short 哈希
ngx.crc32_long 字符串的 crs32_long 哈希
ngx.hmac_sha1 字符串的 hmac_sha1 哈希
ngx.md5 返回 16 进制 MD5
ngx.md5_bin 返回 2 进制 MD5
ngx.sha1_bin 返回 2 进制 sha1 哈希值
ngx.quote_sql_str SQL 语句转义,防止 SQL 注入
ngx.today 返回当前日期
ngx.time 返回 UNIX 时间戳
ngx.now 返回当前时间
ngx.update_time 刷新时间后再返回
ngx.localtime 返回本地系统时间
ngx.utctime 返回 nginx 时间缓存
ngx.cookie_time 返回的时间可用于 cookie 值
ngx.http_time 返回的时间可用于 HTTP 头
ngx.parse_http_time 解析 HTTP 头的时间
ngx.re.match 使用 Perl 兼容正则表达式匹配字符串
ngx.re.find 返回匹配结果子字符串的开始索引 (from) 和结束索引 (to)
ngx.re.gmatch 返回一个 Lua 迭代器
ngx.re.sub
ngx.re.gsub 执行全局替换
ngx.shared.DICT 获取基于共享内存名为 DICT 的 Lua 字典对象,它是一个共享内存区块
ngx.shared.DICT.get 从字典中获取名为键 (key) 值
ngx.shared.DICT.get_stale
ngx.shared.DICT.set
ngx.shared.DICT.safe_set
ngx.shared.DICT.add
ngx.shared.DICT.safe_add
ngx.shared.DICT.replace
ngx.shared.DICT.delete
ngx.shared.DICT.incr
ngx.shared.DICT.flush_all
ngx.shared.DICT.flush_expired
ngx.shared.DICT.get_keys
ngx.socket.udp
udpsock:setpeername
udpsock:send
udpsock:receive
udpsock:close
udpsock:settimeout
ngx.socket.tcp
tcpsock:connect
tcpsock:sslhandshake
tcpsock:send
tcpsock:receive
tcpsock:receiveuntil
tcpsock:close
tcpsock:settimeout
tcpsock:setoption
tcpsock:setkeepalive
tcpsock:getreusedtimes
ngx.socket.connect
ngx.thread.spawn
ngx.thread.wait
ngx.thread.kill
coroutine.create
coroutine.resume
coroutine.yield
coroutine.wrap
coroutine.running
coroutine.status
ngx.config.debug 编译时是否有 --with-debug 选项
ngx.config.prefix 编译时的 --prefix 选项
ngx.config.nginx_version 返回 Nginx 版本号
ngx.config.nginx_configure 返回编译时 ./configure 的命令行选项
ngx.config.ngx_lua_version 返回 ngx_lua 模块版本号
ngx.worker.exiting 当前 worker 进程是否正在关闭(如reload、shutdown期间)
ngx.worker.pid 返回当前 worker 进程的 pid

# 指令

指令名称 说明
lua_use_default_type 是否使用 default_type 指令定义的 Content-Type 默认值
lua_code_cache *_by_lua_file 文件是否 cache
lua_regex_cache_max_entries
lua_regex_match_limit
lua_package_path 用 Lua 写的 lua 外部库路径(.lua文件)
lua_package_cpath 用 C 写的 lua 外部库路径(.so文件)
init_by_lua master 进程启动时挂载的 lua 代码
init_by_lua_file
init_worker_by_lua worker 进程启动时挂载的 lua 代码,常用来执行一些定时器任务
init_worker_by_lua_file
set_by_lua 设置变量
set_by_lua_file
content_by_lua handler 模块
content_by_lua_file
rewrite_by_lua
rewrite_by_lua_file
access_by_lua
access_by_lua_file
header_filter_by_lua header filter 模块
header_filter_by_lua_file
body_filter_by_lua body filter 模块,ngx.arg[1]代表输入的 chunk,ngx.arg[2] 代表当前 chunk 是否为 last
body_filter_by_lua_file
log_by_lua
log_by_lua_file
lua_need_request_body 是否读请求体,跟 ngx.req.read_body() 函数作用类似
lua_shared_dict 创建全局共享的 table(多个 worker 进程共享)
lua_socket_connect_timeout TCP/unix 域 socket 对象 connect 方法的超时时间
lua_socket_send_timeout TCP/unix 域 socket 对象 send 方法的超时时间
lua_socket_send_lowat 设置 cosocket send buffer 的 low water 值
lua_socket_read_timeout TCP/unix 域 socket 对象r eceive 方法的超时时间
lua_socket_buffer_size cosocket 读 buffer 大小
lua_socket_pool_size cosocket 连接池大小
lua_socket_keepalive_timeout cosocket 长连接超时时间
lua_socket_log_errors 是否打开 cosocket 错误日志
lua_ssl_ciphers
lua_ssl_crl
lua_ssl_protocols
ua_ssl_trusted_certificate
lua_ssl_verify_depth
lua_http10_buffering
rewrite_by_lua_no_postpone
lua_transform_underscores_in_response_headers
lua_check_client_abort 是否监视 client 提前关闭请求的事件,如果打开监视,会调用 ngx.on_abort() 注册的回调
lua_max_pending_timers
lua_max_running_timers
更新时间: 2024/01/17, 05:48:13
最近更新
01
JVM调优
12-10
02
jenkins
12-10
03
Arthas
12-10
更多文章>