ngx_lua 模块详细讲解(基于openresty)
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_lua 模块详细讲解(基于openresty)的更多相关文章
- python中的logger模块详细讲解
logger 提供了应用程序可以直接使用的接口handler将(logger创建的)日志记录发送到合适的目的输出filter提供了细度设备来决定输出哪条日志记录formatter决定日志记录的最终输出 ...
- OpenResty之ngx_lua模块的加密接口
原文: ngx_Lua模块中的加密api接口 ngx.crc32_short digest = ngx.crc32_short(str) 该方法主要是计算给定字符串 str 的循环校验码(Cyclic ...
- auth权限认证详细讲解
auth权限认证详细讲解 一.总结 一句话总结:四表两组关系,一个多对多(权限和用户组之间)(多对多需要3个表),一个一对多(用户和用户组之间) 1.实际上使用Auth是需要4张表的(1.会员表 2. ...
- 基于 OpenResty 的动态服务路由方案
2019 年 5 月 11 日,OpenResty 社区联合又拍云,举办 OpenResty × Open Talk 全国巡回沙龙武汉站,又拍云首席布道师在活动上做了< 基于 OpenResty ...
- Nginx基础详细讲解
Nginx基础详细讲解 链接:https://pan.baidu.com/s/1xB20bnuanh0Avs4kwRpSXQ 提取码:migq 复制这段内容后打开百度网盘手机App,操作更方便哦 1. ...
- 基于OpenResty与Consul实现服务网格ServiceMesh
一.逻辑架构 1.基于OpenResty开发智能代理: 利用其动态可编程特性,动态化配置nginx服务路由: 2.需要向OpenResty添加weibo开源的upsync服务发现模块: 3.基于con ...
- 万字长文,以代码的思想去详细讲解yolov3算法的实现原理和训练过程,Visdrone数据集实战训练
以代码的思想去详细讲解yolov3算法的实现原理和训练过程,并教使用visdrone2019数据集和自己制作数据集两种方式去训练自己的pytorch搭建的yolov3模型,吐血整理万字长文,纯属干货 ...
- 30 道 Vue 面试题,内含详细讲解(涵盖入门到精通,自测 Vue 掌握程度)
前言 本文以前端面试官的角度出发,对 Vue 框架中一些重要的特性.框架的原理以问题的形式进行整理汇总,意在帮助作者及读者自测下 Vue 掌握的程度.本文章节结构以从易到难进行组织,建议读者按章节顺序 ...
- Android webservice的用法详细讲解
Android webservice的用法详细讲解 看到有很多朋友对WebService还不是很了解,在此就详细的讲讲WebService,争取说得明白吧.此文章采用的项目是我毕业设计的webserv ...
随机推荐
- 什么是IOC为什么要使用IOC
概念: 作用: 结论:借助于“第三方”实现具有依赖关系的对象之间的解耦 在使用IOC之前的情况 如果有一个齿轮出了问题,就可能会影响到整个齿轮组的正常运 使用IOC之后 对象A获得依赖对象B的过程,由 ...
- Twain
3.7.37 ICAP_SUPPORTEDSIZESType:TW_UINT16Supported Messages:MSG_GET - TW_ENUMERATIONMSG_GETCURRENT - ...
- Linux Kafka集群管理工具kafka-manager的安装使用
一.kafka-manager简介 kafka-manager是目前最受欢迎的kafka集群管理工具,最早由雅虎开源,用户可以在Web界面执行一些简单的集群管理操作.具体支持以下内容: 管理多个集群 ...
- SparkContext.union 与 RDD.union
RDD.union,和SparkContext.union都可以将多个RDD聚合成一个UnionRDD. 但不同的是,RDD.union在每次操作时,会创建一个新的数据集合,生成新的RDD,新的RDD ...
- Tarjan 强连通分量 及 双联通分量(求割点,割边)
Tarjan 强连通分量 及 双联通分量(求割点,割边) 众所周知,Tarjan的三大算法分别为 (1) 有向图的强联通分量 (2) 无向图的双联通分量(求割点,桥) ...
- db2 v9.7 新特性cur_commit 能够实现未提交读新特性cur_commit 能够实现未提交读
db2 get db cfg|find "CUR_COMMIT" 当前已落实 (CUR_COMMIT) = ON ...
- linux 下安装 redis
https://redis.io/ 1.下载 $ cd /usr/local/ $ wget http://download.redis.io/releases/redis-4.0.7.tar.gz ...
- Steeltoe之Service Discovery篇
在前文一窥Spring Cloud Eureka中,已经构建了基于Eureka的服务端与客户端,可用于实现服务注册与发现功能.而借助Steeltoe的类库,可以在.NET生态系统中使用Spring C ...
- ORM 简介 单表操作
cls超 Django基础五之django模型层(一)单表操作 本节目录 一 ORM简介 二 单表操作 三xxx 一 ORM简介 MVC或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型 ...
- Angular4 Ng 模块
原文地址 http://www.jianshu.com/p/317f0e4b7284 根模块 (root module) 每个应用都至少有一个根模块用来引导并运行应用.根模块通常命名为 AppModu ...