ngx_lua模块
ngx_lua模块的原理:
1、每个worker(工作进程)创建一个Lua VM,worker内所有协程共享VM;
2、将Nginx I/O原语封装后注入 Lua VM,允许Lua代码直接访问;
3、每个外部请求都由一个Lua协程处理,协程之间数据隔离;
4、Lua代码调用I/O操作等异步接口时,会挂起当前协程(并保护上下文数据),而不阻塞worker;
5、I/O等异步操作完成时还原相关协程上下文数据,并继续运行;
ngx_lua 模块提供的指令和API等:
指令名称 | 说明 |
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对象receive方法的超时时间 |
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 | |
lua_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 |
table | 说明 |
ngx.arg | 指令参数,如跟在content_by_lua_file后面的参数 |
ngx.var | 变量,ngx.var.VARIABLE引用某个变量 |
ngx.ctx | 请求的lua上下文 |
ngx.header | 响应头,ngx.header.HEADER引用某个头 |
ngx.status | 响应码 |
API | 说明 |
ngx.log | 输出到error.log |
等价于 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 | |
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 | |
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 | 将参数字符串编码为一个table |
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语句转义 |
ngx.today | 返回当前日期 |
ngx.time | 返回UNIX时间戳 |
ngx.now | 返回当前时间 |
ngx.update_time | 刷新时间后再返回 |
ngx.localtime | |
ngx.utctime | |
ngx.cookie_time | 返回的时间可用于cookie值 |
ngx.http_time | 返回的时间可用于HTTP头 |
ngx.parse_http_time | 解析HTTP头的时间 |
ngx.re.match | |
ngx.re.find | |
ngx.re.gmatch | |
ngx.re.sub | |
ngx.re.gsub | |
ngx.shared.DICT | |
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_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 |
常量 | 说明 |
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 |
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进程的PID
ngx.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.nil
http 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_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_GONE (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() #在不阻塞nginx其他事件的情况下同步读取客户端的body信息[详细]
ngx.req.discard_body() #明确丢弃客户端请求的body.
ngx.req.get_body_data() #以字符串的形式获得客户端的请求body内容
ngx.req.get_body_file() #当发送文件请求的时候,获得文件的名字
ngx.req.set_body_data() #设置客户端请求的BODY
ngx.req.clear_header() #请求某个请求头
ngx.exec(uri, args) #执行内部跳转,根据uri和请求参数
ngx.redirect(uri, args) #执行301或者302的重定向。
ngx.send_headers() #发送指定的响应头
ngx.headers_sent #判断头部是否发送给客户端ngx.headers_sent=true
ngx.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的uriencode会编码)
ngx.unescape_uri() #将uri解码
ngx.encode_args(table) #将table解析成url参数
ngx.decode_args(uri) #将参数字符串编码为一个table
ngx.encode_base64(str) #BASE64编码
ngx.decode_base64(str) #BASE64解码
ngx.crc32_short(str) #字符串的crs32_short哈希
ngx.crc32_long(str) #字符串的crs32_long哈希
ngx.md5(str) #返回16进制MD5
ngx.md5_bin(str) #返回2进制MD5
ngx.today() #返回当前日期yyyy-mm-dd
ngx.time() #返回当前时间戳
ngx.now() #返回当前时间
ngx.update_time() #刷新后返回
ngx.localtime() #返回yyyy-mm-dd hh:ii:ss
ngx.utctime() #返回yyyy-mm-dd hh:ii:ss格式的utc时间
ngx.cookie_time(src) #返回可用于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_keys
ndk.set_var.DIRECTIVE #不懂
ngx_lua模块的更多相关文章
- ngx_lua 模块
ngx_lua模块的原理: 1.每个worker(工作进程)创建一个Lua VM,worker内所有协程共享VM:2.将Nginx I/O原语封装后注入 Lua VM,允许Lua代码直接访问:3.每个 ...
- ngx_lua 模块详细讲解(基于openresty)
ngx_lua模块的原理: 1.每个worker(工作进程)创建一个Lua VM,worker内所有协程共享VM:2.将Nginx I/O原语封装后注入 Lua VM,允许Lua代码直接访问:3.每个 ...
- LNMP平滑升级nginx并安装ngx_lua模块教程
#ngx_lua module项目地址 https://github.com/chaoslawful/lua-nginx-module 在LNMP安装包后,重编译nginx,并添加ngx_lua模块 ...
- OpenResty之ngx_lua模块的加密接口
原文: ngx_Lua模块中的加密api接口 ngx.crc32_short digest = ngx.crc32_short(str) 该方法主要是计算给定字符串 str 的循环校验码(Cyclic ...
- ngx_lua模块学习示例之waf
转自:http://www.tuicool.com/articles/FbQ3ymB WAF的主要功能为: ip黑白名单 url黑白名单 useragent黑白名单 referer黑白名单 常见web ...
- 基于ngx_lua模块的waf开发实践
0x00 常见WAF简单分析 WAF主要分为硬件WAF和软件防火墙,硬件WAF如绿盟的NSFOCUS Web Application Firewall,软件防火墙比较有名的是ModSecurity,再 ...
- Nginx-Lua模块的执行顺序
一.nginx执行步骤 nginx在处理每一个用户请求时,都是按照若干个不同的阶段依次处理的,与配置文件上的顺序没有关系,详细内容可以阅读<深入理解nginx:模块开发与架构解析>这本书, ...
- Nginx-ngx_lua模块原理和内置函数
ngx_lua模块的原理: 1.每个worker(工作进程)创建一个Lua VM,worker内所有协程共享VM:2.将Nginx I/O原语封装后注入 Lua VM,允许Lua代码直接访问:3.每个 ...
- 使用ngx_lua构建高并发应用(1)
转自:http://blog.csdn.net/chosen0ne/article/details/7304192 一. 概述 Nginx是一个高性能,支持高并发的,轻量级的web服务器.目前,Apa ...
随机推荐
- src/众测篇:oracle注入过滤-- , + - * /,case when ,select,from,decode等函数如何证明是注入?
(1)nullif: NULLIF:如果exp1和exp2相等则返回空(NULL),否则返回第一个值 真: 假: (2)nvl/nvl2 测试失败无法实现:)记录 (3)如果是oracle报错注入 ...
- 【JVM】JVM中的垃圾回收算法
1.标记 -清除算法 "标记-清除"(Mark-Sweep)算法,如它的名字一样,算法分为"标记"和"清除"两个阶段:首先标记出所有需要回收 ...
- 【转】如何用MTR诊断网络问题
MTR 是一个强大的网络诊断工具,管理员能够用它诊断和隔离网络错误,并向上游提供商提供有关网络状态的有用报告.MTR 通过更大的采样来跟踪路由,就像 traceroute + ping 命令的组合.本 ...
- UVA11464偶数矩阵
题意: 给你一个n*n的01矩阵,你的你的任务是吧尽量少的0变成1,使得每个元素的上下左右之和均为偶数(如果有的话),比如 0 0 0 0 1 0 1 0 0 ---&g ...
- 【python】Leetcode每日一题-螺旋矩阵2
[python]Leetcode每日一题-螺旋矩阵2 [题目描述] 给你一个正整数 n ,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix . ...
- 在AWS Glue中使用Apache Hudi
1. Glue与Hudi简介 AWS Glue AWS Glue是Amazon Web Services(AWS)云平台推出的一款无服务器(Serverless)的大数据分析服务.对于不了解该产品的读 ...
- Outlook关闭时最小化
一:背景环境: 当使用Outlook的时候,不小心点关闭,会不能及时发现接收的新邮件. 二:解决方法: 利用KeepOutlookRunning.dll插件,可以实现,点击关闭时,outlook没有实 ...
- springboot国际化与@valid国际化支持
springboot国际化 springboot对国际化的支持还是很好的,要实现国际化还简单.主要流程是通过配置springboot的LocaleResolver解析器,当请求打到springboot ...
- OPC使用思路
- 3D高清电商购物小图标图片_在线商城三维icon图标素材大全
3D高清电商购物小图标图片_在线商城三维icon图标素材大全