NGINX+Lua 笔记 (陆续补充,待整理)

编译安装nginx(包含Lua模块)
1. $ yum install pcre-devel.x86_64 #如果没有安装的话请安装。rewrite模块需要pcre
2. 下载以下模块 https://github.com/simpl/ngx_devel_kit/tags   https://github.com/openresty/lua-nginx-module/tags
3. 下载luajit程序 http://luajit.org/download.html 并安装
$ export LUAJIT_LIB=/usr/local/lib #根据实际安装路径而定
$ export LUAJIT_INC=/usr/local/include/luajit-2.0 #根据实际安装路径而定
4. 下载nginx程序文件,解压安装。下载地址: http://nginx.org/en/download.html
$ ./configure --prefix=/opt/nginx \    #nginx的安装路径 
--add-module=/path/to/ngx_devel_kit \   #ngx_devel_kit 的源码路径 
--add-module=/path/to/lua-nginx-module  #nginx_lua_module 的源码路径

$ make && make install


nginx.conf添加:

location /lua { 
      default_type 'text/plain'; 
      content_by_lua 'ngx.say("hello, lua")'; 
}

浏览器打开 http://localhost/lua 查看是否安装成功。

或者将lua代码放在独立的文件

lua_code_cache off; #方便debug
location /lua { 
      content_by_lua_file /path/to/your_perfect_lua_code.lua;
}
示例 /path/to/your_perfect_lua_code.lua:

ngx.header.content_type = "text/plain";

function receive (prod)
    local status, value = coroutine.resume(prod)
    return value
end

function send (x)
    coroutine.yield(x)
end

function producer ()
    return coroutine.create(function ()
        local i = 0
        ngx.say("producer start")
        while true do
            local x = "hello, world"
            i = i + 1
            if i > 10 then
                send(nil)
                break 
            else send(x)
            end
        end
        ngx.say("producer end")
    end)
end

function filter (prod)
    return coroutine.create(function ()
        local line = 1
        ngx.say("filter start")
        while true do
            local x = receive(prod) -- get new value
            ngx.say("filter receiver: ", x)
            if x then
                x = string.format("%5d %s", line, x)
                send(x) -- send it to consumer
            else
                send(nil)
                coroutine.resume(prod)
                break
            end
            
            line = line + 1
        end
        ngx.say("filter end")
    end)
end

function consumer (prod)
    ngx.say("consumer start")
    while true do
        local x = receive(prod) -- get new value
        ngx.say("consumer received: ", x)
        if x then
        ngx.say(x)
        else
        coroutine.resume(prod)
        break
        end
    end
    ngx.say("consumer end")
end

p = producer()
f = filter(p)
consumer(f)

这段代码使用Lua的协程演示了如何使用Lua来实现生产者-消费者模式。关于协程具体请参考:http://www.lua.org/pil/9.1.html

访问http://localhost/lua,显示如下

consumer start
filter start
producer start
filter receiver: hello, world
consumer received:     1 hello, world
    1 hello, world
filter receiver: hello, world
consumer received:     2 hello, world
    2 hello, world
filter receiver: hello, world
consumer received:     3 hello, world
    3 hello, world
filter receiver: hello, world
consumer received:     4 hello, world
    4 hello, world
filter receiver: hello, world
consumer received:     5 hello, world
    5 hello, world
filter receiver: hello, world
consumer received:     6 hello, world
    6 hello, world
filter receiver: hello, world
consumer received:     7 hello, world
    7 hello, world
filter receiver: hello, world
consumer received:     8 hello, world
    8 hello, world
filter receiver: hello, world
consumer received:     9 hello, world
    9 hello, world
filter receiver: hello, world
consumer received:    10 hello, world
   10 hello, world
filter receiver: nil
consumer received: nil
producer end
filter end
consumer end




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值