跳至主要內容

Nginx-Plus的动态模块Lua

PaperDragon...大约 3 分钟

Nginx-Plus的动态模块Lua

将Lua联合例程集成到NGINX事件处理模型中,使用由NGINX,Inc。编写和支持的Lua动态模块。

http://openresty.org

这个开源 Web 平台主要由章亦春(agentzhopen in new window)维护。在 2011 年之前曾由雅虎中国和淘宝网open in new window赞助,在后来的 2012 ~ 2016 年间主要由美国的 CloudFlare 公司open in new window 提供支持。目前,OpenResty® 主要由 OpenResty 软件基金会和 OpenResty Inc. 公司提供支持。

2009年,agentzh & chaoslawful一起基于Nginx用C语言开发OpenResty 2011年,agentzh离职专心维护OpenResty 2012-2016年,Cloudflare赞助支持agentzh专心开发OpenResty,快速发展 2016年,锤子科技赞助OpenResty软件基金会(发布会的门票收入100万元)以支持OpenResty开发

https://blog.csdn.net/shasharoman/article/details/120069206

安装过程

安装NDK模块

因为Lua依赖于NDK,安装NDK模块

https://docs.nginx.com/nginx/admin-guide/dynamic-modules/ndk/

installation Instructions

  1. Install the NDK module.

    For Amazon Linux, CentOS, Oracle Linux, and RHEL:

    $ yum install nginx-plus-module-ndk
    

    For Debian and Ubuntu:

    $ apt-get install nginx-plus-module-ndk
    

    For SLES:

    $ zypper install nginx-plus-module-ndk
    

    For Alpine:

    $ apk add nginx-plus-module-ndk
    
  2. Put the load_moduleopen in new window directive in the top‑level (“main”) context of NGINX Plus configuration file, nginx.conf:

    load_module modules/ndk_http_module.so;
    
  3. Perform additional configuration as required by the moduleopen in new window.

  4. Reload NGINX Plus to enable the module:

    $ nginx -t && nginx -s reload
    

nginx安装Lua模块

https://docs.nginx.com/nginx/admin-guide/dynamic-modules/lua/

  1. Prior to installing the Lua module, verify that the NDKopen in new window module is already installed.

  2. Install the Lua module.

    For Amazon Linux, CentOS, Oracle Linux, and RHEL:

    $ yum install nginx-plus-module-lua
    

    For Debian and Ubuntu:

    $ apt-get install nginx-plus-module-lua
    

    For SLES:

    $ zypper install nginx-plus-module-lua
    

    For Alpine:

    $ apk add nginx-plus-module-lua
    
  3. Put the load_moduleopen in new window directives for NDK and Lua modules in the top‑level (“main”) context of NGINX Plus configuration file, nginx.conf:

    load_module modules/ndk_http_module.so;
    load_module modules/ngx_http_lua_module.so;
    

    Note: The directives must be in this order.

  4. Perform additional configuration as required by the moduleopen in new window.

  5. Reload NGINX Plus to enable the module:

    $ nginx -t && nginx -s reload
    
    

怎么去使用

set_by_lua_block

syntax: set_by_lua_block $res { lua-script }

context: server, server if, location, location if

phase: rewrite

执行一对大括号({})内指定的代码,并将字符串输出返回$res。一对大括号({})内的代码可以进行API调用,并可以从ngx.arg表中检索输入参数(索引从1开始,按顺序递增)。

当Nginx事件循环在代码执行期间被阻塞时,该指令被设计为执行短的、快速运行的代码块。因此,应避免使用耗时的代码序列。
该指令通过将自定义命令注入标准ngx_http_rewrite_module的命令列表来实现。由于ngx_http_rewrite_module在其命令中不支持非阻塞I/O,因此要求生成当前Lua“轻线程”的Lua API无法在此指令中工作。

在set_by_lua_block的上下文中,当前至少禁用了以下API函数:
输出API函数(例如,ngx.say和ngx.send_headers)
控制API函数(例如,ngx.exit)
子请求API函数(例如,ngx.location.capture和ngx.location/capture_multi)
Cosocket API函数(例如,ngx.socket.tcp和ngx.req.socket)。
休眠API函数ngx.sleep。
此外,请注意,该指令一次只能将值写入单个Nginx变量。但是,可以使用ngx.var.VARIABLE接口解决问题。

例子


 location /foo {
     set $diff ''; # we have to predefine the $diff variable here

     set_by_lua_block $sum {
         local a = 32
         local b = 56

         ngx.var.diff = a - b  -- write to $diff directly
         return a + b          -- return the $sum value normally
     }

     echo "sum = $sum, diff = $diff";
 }

This directive can be freely mixed with all directives of the ngx_http_rewrite_moduleopen in new window, set-misc-nginx-moduleopen in new window, and array-var-nginx-moduleopen in new window modules. All of these directives will run in the same order as they appear in the config file.

 set $foo 32;
 set_by_lua_block $bar { return tonumber(ngx.var.foo) + 1 }
 set $baz "bar: $bar";  # $baz == "bar: 33"

No special escaping is required in the Lua code block.

This directive requires the ngx_devel_kitopen in new window module.

This directive was first introduced in the v0.9.17 release.

你认为这篇文章怎么样?
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v3.1.3