nginx中使用lua脚本的方法

Lua是一种跟JavaScript很像的语言,Ngix_Lua同样使用异步单线程,语法甚至比JS更加简单,之前的评测指出,Ngix_lua的性能几乎是Node.JS的一倍。

Nginx 特点

1.流行的高性能HTTP服务器
2.事件驱动(异步)架构
3.少量且可测内存占用
4.声明性配置语言
5.基于C的可扩展模块

通过lua-nginx-module即可在nginx上启动lua脚本。

一个例子:

代码如下:

location / {

    content_by_lua '

        local res = ngx.location.capture("/sub")

        if res.status >= 500 then 

            ngx.exit(res.status) 

        end

        ngx.status = res.status

        ngx.say(res.body)

    ';

}

location /sub {

    echo "Hello, Sub-Request!";

}

lua-nginx-module在Github上的项目地址 https://github.com/openresty/lua-nginx-module