编译安装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 的源码路径
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