openresty(lua-API-基础,常量)
摘自:http://www.daileinote.com/computer/openresty/06 下面介绍的 Lua API 可以在 *_by_lua_block 和 *_by_lua_file 指定的 Lua 代码里调用,这些 API 很好的桥接了 nginx 和 Lua。 ngx.arg12syntax: val = ngx.arg[index]context: body_filter_by_lua* 在响应体过滤时候 ngx.arg[1] 代表响应体内容,ngx.arg[2] 代表该块的标志位。参照 配置指令 body_filter_by_lua_block。 ngx.var.VARIABLE这个指令可以获取 nginx 的变量,注意,在一次请求里 nginx 变量读取的时候会分配一次内存,在请求结束的时候会释放,所以如果需要反复使用该变量的值,可以保存为本地 lua 变量。 未定义的变量返回 nil,定义了未初始化的返回空字符串。 把变量设置为 nil,会清除该变量。 1ngx.var.args = nil 例子 123456789101112location...
openresty(lua-API-子请求,(内部)重定向)
摘自:http://www.daileinote.com/computer/openresty/07 内部子请求子请求在内部执行,非常高效。子请求会缓冲所有的响应体,如果响应体很大,建议用 cosockets 来操作。 子请求默认情况下会继承所有父请求的请求头。 ngx.location.capture1syntax: res = ngx.location.capture(uri, options?) 基本用法 1res = ngx.location.capture(uri) res是一个表格,包含4个成员 res.status 子请求响应的状态码res.header 表格的形式存放子请求的响应头,如果有同名多个响应头,那么值也为表格,比如假设响应头有3个 Set-Cookie。 123Set-Cookie: a=3Set-Cookie: foo=barSet-Cookie: baz=blah 那么 res.header["Set-Cookie"] 里的值为 1{"a=3", "foo=bar",...
openresty(lua-API-常用组件)
摘自:http://www.daileinote.com/computer/openresty/09 下面列出 lua-nginx-module 模块内置的一些组件。 ngx.escape_uri1syntax: newstr = ngx.escape_uri(str) 转义uri。 ngx.unescape_uri1syntax: newstr = ngx.unescape_uri(str) 取消转义。 12345678910location = /test { content_by_lua_block { ngx.say(ngx.escape_uri("http://www.freecls.com?name=freecls&age=28")) ngx.say(ngx.unescape_uri("http%3A%2F%2Fwww.freecls.com%3Fname%3Dfreecls%26age%3D28")) }...
openresty(lua-API-请求相关)
摘自:http://www.daileinote.com/computer/openresty/08 ngx.req.is_internal1syntax: is_internal = ngx.req.is_internal() 判断是否为内部请求。 ngx.get_phase1syntax: str = ngx.get_phase() 获取当前阶段名 1234567891011121314init #init_by_lua*.init_worker #init_worker_by_lua*.ssl_cert #ssl_certificate_by_lua*.ssl_session_fetch #ssl_session_fetch_by_lua*.ssl_session_store #ssl_session_store_by_lua*.set #set_by_lua*.rewrite #rewrite_by_lua*.balancer ...
openresty - luajit - continue、bit、ffi
摘自:http://www.daileinote.com/computer/openresty/03 我们知道,openresty 使用的开发语言为 Lua,内置了 LuaJIT 解释器,速度比 Lua 官方解释器要快很多。 LuaJIT 基于 Lua 5.1,但在不破坏兼容性的前提下适当引入了一些 5.2 和 5.3 的语言特性,还提供了很多特别的优化和库,例如 table.new、bit、ffi。LuaJIT 是开源的,官网地址为 http://luajit.org continueLua 语言不支持 continue 语法,有时候很不方便,好在 LuaJIT 加入了 Lua 5.2 的 goto 语句,变相的实现了 continue。goto 语句需要配合标签 ::label::使用,例如 123456789101112--tmp.luafor i=0,6,1 do if i<5 then goto continue else print(i) end ::continue::end[root@192 lua]#...
openresty-配置指令
摘自:http://www.daileinote.com/computer/openresty/05 下面是 lua-nginx-module 模块的一些配置指令 lua_package_path123syntax: lua_package_path <lua-style-path-str>default: context: http 设置 lua 模块的搜索路径,可以包含 $prefix 或 ${prefix} 变量,当我们有多个 openresty 程序通过 -p 命令指定工作目录启动时非常有用。 lua_package_cpath123syntax: lua_package_cpath <lua-style-path-str>default: context: http 指定c动态库搜索路径,其他的同上。 12lua_package_cpath '/usr/lib64/lua/5.1/?.so;;/tmp/lua/*.so;;';lua_package_path...
lua语法入门
Lua语法入门Nginx编程需要用到Lua语言,因此我们必须先入门Lua的基本语法。 初识LuaLua 是一种轻量小巧的脚本语言,用标准C语言编写并以源代码形式开放, 其设计目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能。官网:https://www.lua.org/ Lua经常嵌入到C语言开发的程序中,例如游戏开发、游戏插件等。 Nginx本身也是C语言开发,因此也允许基于Lua做拓展。 HelloWorldCentOS7默认已经安装了Lua语言环境,所以可以直接运行Lua代码。 1)在Linux虚拟机的任意目录下,新建一个hello.lua文件 2)添加下面的内容 1print("Hello World!") ...
