openresty中如何高效引入及使用lua进行开发?

2026-03-31 22:421阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1159个文字,预计阅读时间需要5分钟。

openresty中如何高效引入及使用lua进行开发?

OpenResty 开发系列 24 -- OpenResty 中 Lua 的引入及使用(一)OpenResty + 引入 + Lua 一)OpenResty 中 Nginx 引入 Lua 方式 + 1)xxx_by_lua + --- 字符串编写方式 + 2)xxx_by_lua_block + ---- 代码块方式 + 3)xxx_by_lua_file + ---- 直接引用一个 Lua 文件

openresty开发系列24--openresty中lua的引入及使用openresty 引入 lua一)openresty中nginx引入lua方式 1)xxx_by_lua --->字符串编写方式 2) xxx_by_lua_block ---->代码块方式 3) xxx_by_lua_file ---->直接引用一个lua脚本文件我们案例中使用内容处理阶段,用content_by_lua演示-----------------编辑nginx.conf-----------------------第一种:content_by_lualocation /testlua { content_by_lua "ngx.say(‘hello world‘)";}输出了hello worldcontent_by_lua 方式,参数为字符串,编写不是太方便。----------------------------------------第二种:content_by_lua_blocklocation /testlua { content_by_lua_block { ngx.say("hello world"); } }content_by_lua_block {} 表示内部为lua块,里面可以应用lua语句----------------------------------------第三种:content_by_lua_filelocation /testlua { content_by_lua_file /usr/local/lua/test.lua;}content_by_lua_file 就是引用外部lua文件# vi test.luangx.say("hello world");二)openresty使用lua打印输出案例 location /testsay { content_by_lua_block { --写响应头 ngx.header.a = "1" ngx.header.b = "2" --输出响应 ngx.say("a", "b", "<br/>") ngx.print("c", "d", "<br/>") --200状态码退出 return ngx.exit(200) } } ngx.header:输出响应头; ngx.print:输出响应内容体; ngx.say:通ngx.print,但是会最后输出一个换行符; ngx.exit:指定状态码退出。三)介绍一下openresty使用lua常用的api1)ngx.var : 获取Nginx变量 和 内置变量nginx内置的变量$arg_name 请求中的name参数$args 请求中的参数$binary_remote_addr 远程地址的二进制表示$body_bytes_sent 已发送的消息体字节数$content_length HTTP请求信息里的"Content-Length"$content_type 请求信息里的"Content-Type"$document_root 针对当前请求的根路径设置值$document_uri 与$uri相同; 比如 /test2/test.php$host 请求信息中的"Host",如果请求中没有Host行,则等于设置的服务器名$hostname 机器名使用 gethostname系统调用的值$localhost:88/test1/$scheme 所用的协议,比如www.nginx.com/resources/wiki/modules/lua/#nginx-api-for-lua操作指令 说明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.logprint 等价于 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 将参数字符串编码为一个tablengx.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进制MD5ngx.md5_bin 返回2进制MD5ngx.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 常量说明ngx.OK (0)ngx.ERROR (-1)ngx.AGAIN (-2)ngx.DONE (-4)ngx.DECLINED (-5)ngx.nilHTTP 请求方式ngx.HTTP_GETngx.HTTP_HEADngx.HTTP_PUTngx.HTTP_POSTngx.HTTP_DELETEngx.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 返回状态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)

openresty中如何高效引入及使用lua进行开发?

本文共计1159个文字,预计阅读时间需要5分钟。

openresty中如何高效引入及使用lua进行开发?

OpenResty 开发系列 24 -- OpenResty 中 Lua 的引入及使用(一)OpenResty + 引入 + Lua 一)OpenResty 中 Nginx 引入 Lua 方式 + 1)xxx_by_lua + --- 字符串编写方式 + 2)xxx_by_lua_block + ---- 代码块方式 + 3)xxx_by_lua_file + ---- 直接引用一个 Lua 文件

openresty开发系列24--openresty中lua的引入及使用openresty 引入 lua一)openresty中nginx引入lua方式 1)xxx_by_lua --->字符串编写方式 2) xxx_by_lua_block ---->代码块方式 3) xxx_by_lua_file ---->直接引用一个lua脚本文件我们案例中使用内容处理阶段,用content_by_lua演示-----------------编辑nginx.conf-----------------------第一种:content_by_lualocation /testlua { content_by_lua "ngx.say(‘hello world‘)";}输出了hello worldcontent_by_lua 方式,参数为字符串,编写不是太方便。----------------------------------------第二种:content_by_lua_blocklocation /testlua { content_by_lua_block { ngx.say("hello world"); } }content_by_lua_block {} 表示内部为lua块,里面可以应用lua语句----------------------------------------第三种:content_by_lua_filelocation /testlua { content_by_lua_file /usr/local/lua/test.lua;}content_by_lua_file 就是引用外部lua文件# vi test.luangx.say("hello world");二)openresty使用lua打印输出案例 location /testsay { content_by_lua_block { --写响应头 ngx.header.a = "1" ngx.header.b = "2" --输出响应 ngx.say("a", "b", "<br/>") ngx.print("c", "d", "<br/>") --200状态码退出 return ngx.exit(200) } } ngx.header:输出响应头; ngx.print:输出响应内容体; ngx.say:通ngx.print,但是会最后输出一个换行符; ngx.exit:指定状态码退出。三)介绍一下openresty使用lua常用的api1)ngx.var : 获取Nginx变量 和 内置变量nginx内置的变量$arg_name 请求中的name参数$args 请求中的参数$binary_remote_addr 远程地址的二进制表示$body_bytes_sent 已发送的消息体字节数$content_length HTTP请求信息里的"Content-Length"$content_type 请求信息里的"Content-Type"$document_root 针对当前请求的根路径设置值$document_uri 与$uri相同; 比如 /test2/test.php$host 请求信息中的"Host",如果请求中没有Host行,则等于设置的服务器名$hostname 机器名使用 gethostname系统调用的值$localhost:88/test1/$scheme 所用的协议,比如www.nginx.com/resources/wiki/modules/lua/#nginx-api-for-lua操作指令 说明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.logprint 等价于 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 将参数字符串编码为一个tablengx.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进制MD5ngx.md5_bin 返回2进制MD5ngx.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 常量说明ngx.OK (0)ngx.ERROR (-1)ngx.AGAIN (-2)ngx.DONE (-4)ngx.DECLINED (-5)ngx.nilHTTP 请求方式ngx.HTTP_GETngx.HTTP_HEADngx.HTTP_PUTngx.HTTP_POSTngx.HTTP_DELETEngx.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 返回状态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)

openresty中如何高效引入及使用lua进行开发?