ngx_lua模块api说明
#ngx指令
lua_code_cache on | off;
作用:打开或关闭 lua 代码缓存,影响以下指令: set_by_lua_file , content_by_lua_file, rewrite_by_lua_file, access_by_lua_file 及强制加载或者reload lua 模块等.缓存开启时修改lua代码需要重启nginx,不开启时则不用。开发阶段一般关闭缓存。
作用域:main, server, location, location if
lua_regex_cache_max_entries 1024;
作用:未知(貌似是限定缓存正则表达式处理结果的最大数量)
lua_package_path .../path... ;
作用:设置用lua代码写的扩展库路径。
例:lua_package_path '/foo/bar/?.lua;/blah/?.lua;;';
lua_package_cpath '/bar/baz/?.so;/blah/blah/?.so;;';
作用:设置c扩展的lua库路径。
set_by_lua $var '' [$arg1 $arg2];
set_by_lua_file $var [$arg1 $arg2 ...];
作用:设置一个nginx变量,变量值从lua脚本里运算由return返回,可以实现复杂的赋值逻辑;此处是阻塞的,lua代码要做到非常快.
另外可以将已有的ngx变量当作参数传进lua脚本里去,由ngx.arg[1],ngx.arg[2]等方式访问。
作用域:main, server, location, server if, location if
处理阶段:rewrite
content_by_lua '';
content_by_lua_file luafile;
作用域:location, location if
说明:内容处理器,接收请求处理并输出响应,content_by_lua直接在nginx配置文件里编写较短lua代码后者使用lua文件。
rewrite_by_lua ''
rewrite_by_lua_file lua_file;
作用域:http, server, location, location if
执行内部url重写或者外部重定向,典型的如伪静态化的url重写。其默认执行在rewrite处理阶段的最后.
注意,在使用rewrite_by_lua时,开启rewrite_log on;后也看不到相应的rewrite log。
access_by_lua 'lua code';
access_by_lua_file lua_file.lua;
作用:用于访问控制,比如我们只允许内网ip访问,可以使用如下形式。
access_by_lua '
if ngx.req.get_uri_args()[token] ~= 123 then
return ngx.exit(403)
end ';
作用域:http, server, location, location if
header_filter_by_lua 'lua code';
header_filter_by_lua_file path_file.lua;
作用:设置header 和 cookie;
lua_need_request_body on|off;
作用:是否读请求体,跟ngx.req.read_body()函数作用类似,但官方不推荐使用此方法。
lua_shared_dict shared_data 10m;
作用:设置一个共享全局变量表,在所有worker进程间共享。在lua脚本中可以如下访问它:
例:local shared_data = ngx.shared.shared_data
10m 不知是什么意思。
init_by_lua 'lua code';
init_by_lua_file lua_file.lua;
作用域:http
说明:ginx master进程加载配置时执行;通常用于初始化全局配置/预加载lua模块
init_worker_by_lua 'lua code';
init_worker_by_lua_file luafile.lua;
作用域:http
说明:每个nginx worker进程启动时调用的计时器,如果master进程不允许则只会在init_by_lua之后调用;通常用于定时拉取配置/数据,或者后端服务的健康检查。
######################
方法和常量
######################
ngx.arg[index] #ngx指令参数,当这个变量在set_by_lua或者set_by_lua_file内使用的时候是只读的,指的是在配置指令输入的参数.ngx.var.varname #读写nginx变量的值,最好在lua脚本里缓存变量值,避免在当前请求的生命周期内内存的泄漏ngx.config.ngx_lua_version #当前ngx_lua模块版本号ngx.config.nginx_version #nginx版本ngx.worker.exiting #当前worker进程是否正在关闭ngx.worker.pid #当前worker进程的pidngx.config.nginx_configure #编译时的./configure命令选项ngx.config.prefix #编译时的prefix选项core constans: #ngx_lua 核心常量 ngx.ok (0) ngx.error (-1) ngx.again (-2) ngx.done (-4) ngx.declined (-5) ngx.nilhttp method constans: #经常在ngx.location.catpure和ngx.location.capture_multi方法中被调用. 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 constans: #http请求状态常量 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.log方法中被使用. ngx.stderr ngx.emerg ngx.alert ngx.crit ngx.err ngx.warn ngx.notice ngx.info ngx.debug###################api中的方法:##################print() #与 ngx.print()方法有区别,print() 相当于ngx.log()ngx.ctx #这是一个lua的table,用于保存ngx上下文的变量,在整个请求的生命周期内都有效,详细参考官方ngx.location.capture() #发出一个子请求,详细用法参考官方文档。ngx.location.capture_multi() #发出多个子请求,详细用法参考官方文档。ngx.status #读或者写当前请求的相应状态. 必须在输出相应头之前被调用.ngx.header.header #访问或设置http header头信息,详细参考官方文档。ngx.req.set_uri() #设置当前请求的uri,详细参考官方文档ngx.set_uri_args(args) #根据args参数重新定义当前请求的uri参数.ngx.req.get_uri_args() #返回一个lua table,包含当前请求的全部的url参数ngx.req.get_post_args() #返回一个lua table,包括所有当前请求的post参数ngx.req.get_headers() #返回一个包含当前请求头信息的lua table.ngx.req.set_header() #设置当前请求头header某字段值.当前请求的子请求不会受到影响.ngx.req.read_body() #在不阻塞ngnix其他事件的情况下同步读取客户端的body信息.[详细]ngx.req.discard_body() #明确丢弃客户端请求的bodyngx.req.get_body_data() #以字符串的形式获得客户端的请求body内容ngx.req.get_body_file() #当发送文件请求的时候,获得文件的名字ngx.req.set_body_data() #设置客户端请求的bodyngx.req.set_body_file() #通过filename来指定当前请求的file data。ngx.req.clear_header() #清求某个请求头ngx.exec(uri,args) #执行内部跳转,根据uri和请求参数ngx.redirect(uri, status) #执行301或者302的重定向。ngx.send_headers() #发送指定的响应头ngx.headers_sent #判断头部是否发送给客户端ngx.headers_sent=truengx.print(str) #发送给客户端的响应页面ngx.say() #作用类似ngx.print,不过say方法输出后会换行ngx.log(log.level,...) #写入nginx日志ngx.flush() #将缓冲区内容输出到页面(刷新响应)ngx.exit(http-status) #结束请求并输出状态码ngx.eof() #明确指定关闭结束输出流ngx.escape_uri() #uri编码(本函数对逗号,不编码,而php的urlencode会编码)ngx.unescape_uri() #uri解码ngx.encode_args(table) #将tabel解析成url参数ngx.decode_args(uri) #将参数字符串编码为一个tablengx.encode_base64(str) #base64编码ngx.decode_base64(str) #base64解码ngx.crc32_short(str) #字符串的crs32_short哈希ngx.crc32_long(str) #字符串的crs32_long哈希ngx.hmac_sha1(str) #字符串的hmac_sha1哈希ngx.md5(str) #返回16进制md5ngx.md5_bin(str) #返回2进制md5ngx.today() #返回当前日期yyyy-mm-ddngx.time() #返回当前时间戳ngx.now() #返回当前时间ngx.update_time() #刷新后返回ngx.localtime() #返回 yyyy-mm-dd hh:ii:ssngx.utctime() #返回yyyy-mm-dd hh:ii:ss格式的utc时间ngx.cookie_time(sec) #返回用于cookie使用的时间ngx.http_time(sec) #返回可用于http header使用的时间 ngx.parse_http_time(str) #解析http头的时间ngx.is_subrequest #是否子请求(值为 true or false)ngx.re.match(subject,regex,options,ctx) #ngx正则表达式匹配,详细参考官网ngx.re.gmatch(subject,regex,opt) #全局正则匹配ngx.re.sub(sub,reg,opt) #匹配和替换(未知)ngx.re.gsub() #未知ngx.shared.dict #ngx.shared.dict是一个table 里面存储了所有的全局内存共享变量 ngx.shared.dict.get 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_keysndk.set_var.directive #不懂
#参考文档:
http://wiki.nginx.org/httpluamodulezh#core_constants ngx_lua官方文档
http://blog.csdn.net/imlsz/article/details/42915473 对官方api的主要内容翻译
http://jinnianshilongnian.iteye.com/blog/2186448 对ngx_lua使用文档有详细的例子
http://www.cnblogs.com/wangxusummer/p/4309007.html 对ngx_lua的模块方法的简单介绍
版权声明:本文为博主原创文章,未经博主允许不得转载。
以上就介绍了ngx_lua 模块api说明,包括了方面的内容,希望对php教程有兴趣的朋友有所帮助。