YiluPHP
这家伙很懒,什么都没有留下...

经验 linux下给Nginx安装LuaJIT

浏览数 176075 最后修改时间

实测在以下tengine版本中可以安装成功:

tengine-2.2.1、tengine-2.2.3,没有测试tengine-2.2.2
在以下tengine版本中安装不了:
tengine-2.3.0、tengine-2.3.1、tengine-2.3.2、tengine-2.3.3
在以上版本的tengine中安装make时会报以下错误:
        /software/lua-nginx-module-0.10.8/src/ngx_http_lua_headers.c
/software/lua-nginx-module-0.10.8/src/ngx_http_lua_headers.c: In function ‘ngx_http_lua_ngx_req_raw_header’:
/software/lua-nginx-module-0.10.8/src/ngx_http_lua_headers.c:151:15: error: incompatible types when assigning to type ‘struct ngx_buf_t *’ from type ‘ngx_chain_t’
             b = hc->busy[i];
               ^
/software/lua-nginx-module-0.10.8/src/ngx_http_lua_headers.c:227:15: error: incompatible types when assigning to type ‘struct ngx_buf_t *’ from type ‘ngx_chain_t’
             b = hc->busy[i];
               ^
make[1]: *** [objs/addon/src/ngx_http_lua_headers.o] Error 1
make[1]: Leaving directory `/software/tengine-2.3.2'
make: *** [build] Error 2


Nginx安装lua支持

需要LuaJIT-2.0.4.tar.gz,ngx_devel_kit,lua-nginx-module

1.下载安装LuaJIT-2.0.4.tar.gz

cd /software
wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz

tar xzvf LuaJIT-2.0.5.tar.gz

cd LuaJIT-2.0.5

make install PREFIX=/usr/local/luajit


#注意环境变量!

export LUAJIT_LIB=/usr/local/luajit/lib

export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0

2.下载解压ngx_devel_kit

cd /software
wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz

tar -xzvf v0.3.0.tar.gz

3.下载解压lua-nginx-module

cd /software
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.8.tar.gz

tar -xzvf v0.10.8.tar.gz



4.重新编译安装nginx

找到现有的nginx的编译参数
/usr/local/tengine-2.2.1/sbin/nginx -V
我的现有编译参数是:
./configure --prefix=/usr/local/tengine-2.2.1 --add-module=/usr/local/tengine-2.2.1/modules/redis2-nginx-module
接下来需要加上我们需要安装的 

cd /software/tengine-2.2.1
./configure --prefix=/usr/local/tengine-2.2.1 --add-module=/usr/local/tengine-2.2.1/modules/redis2-nginx-module --add-module=/software/ngx_devel_kit-0.3.0 --add-module=/software/lua-nginx-module-0.10.8

#注意ngx_devel_kit和lua-nginx-module以实际解压路径为准

make
cp /usr/local/tengine-2.2.1/sbin/nginx /usr/local/tengine-2.2.1/sbin/nginx.bak.20190514
service nginx stop
cd /software/tengine-2.2.1/objs
cp /software/tengine-2.2.1/objs/nginx /usr/local/tengine-2.2.1/sbin/nginx
service nginx start


注:报错gcc需要安装,可以执行
yum install -y gcc g++ gcc-c++

依赖报错,可以执行

yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel

编译是报错:./configure: error: ngx_http_lua_module requires the Lua library. 可以执行
yum install lua-devel

5.验证

vim /usr/local/tengine-2.2.1/conf/vhosts/www.yiluphp.com.conf

#lua指令方式

#在server 中添加一个localtion

location /testlua{

default_type 'text/plain';

content_by_lua 'ngx.say("hello, lua")';

}

#lua文件方式

#在server 中添加一个localtion

location = /testhello{
     default_type text/plain;
     content_by_lua_file lua_file/test.lua; #目录相对于nginx的根目录,即lua_file与nginx的config同一级目录,注意目录的访问权限,需要给nginx的运行用户有执行权限,这里支持绝对路径
}

#test.lua文件内容

ngx.say("hello world");


重启nginx访问测试
service nginx reload


注:

报错nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory 可以执行

ln -s /usr/local/luajit/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2

具体source的路径已实际安装路径为准


访问:

http://www.yiluphp.com/testlua

显示:hello, lua

http://www.yiluphp.com/testhello

显示:hello world



到这里确定安装成功。

lua的注释写法
lua中的注释不是 //  、/**/ 、或#,而是标新立异的,如下:

--print("单行注释")
 
---print("单行注释")
 
--[[
print("整段注释")
print("整段注释")
print("整段注释")
]]
 
 
--[=[
print("整段注释,=号个数随意")
print("整段注释,=号个数随意")
print("整段注释,=号个数随意")
--]=]
 
 
---[[
print("取消段注释,只需加个-")
--]]

更多nginx+lua的例子

例子1:
nginx根据lua返回的值做不一样的跳转
nginx配置
server {
        listen 80;
        #... 此处忽略了其它配置
        location ~ .php$ {
                #把 return.lua 脚本中返回的值赋值给变量 $a
                set_by_lua_file $a lua_file/return.lua;
                if ($a = 1) {   #如果 $a 等于1,则输出客户端的IP和端口号
                        add_header 'Content-Type' 'text/plain; charset=utf-8';
                        return 200 '$remote_addr:$remote_port';
                }
                 #如果 $a 不等于1,则按正常的php请求执行
                #... 此处忽略了其它配置
        }
}

return.lua存放在nginx安装目录下的lua_file目录里,lua_file与nginx的conf是同一级目录,return.lua的内容如下:
--此处直接返回GET参数age的值
return ngx.var.arg_age;

综合例子2:
--设置输出的编码,避免中文出现乱码
ngx.header['Content-Type']="text/html;charset=UTF-8"

ngx.say("hello world 世界,你好!");

--获取客户端的IP
ngx.say(ngx.var.remote_addr);

--获取GET参数name
local name=ngx.var.arg_name
ngx.say(name);

--获取GET参数age
local age=ngx.var.arg_age
ngx.say(age);

--获取POST参数
ngx.req.read_body();
local args = ngx.req.get_post_args();
ngx.say(args["gender"]);
ngx.say(args["height"]);

--对IP进行md5加密,这里调用nginx的md5模块
ngx.say(ngx.md5(ngx.var.remote_addr));


--操作redis,需要先添加redis.lua的库在luajit的resty目录中,redis.lua库的内容可以本站找到
local redis = require "resty.redis";
local red = redis:new();

red:set_timeout(20);
local ok, err = red:connect("127.0.0.1", 6379);
if not ok then
        ngx.log(ngx.DEBUG, "error:" .. err);
else
        local ip_blacklist, err = red:get('test'); -- 获取redis中test的值
        if err then
                ngx.log(ngx.DEBUG, "error:" .. err);
        else
                ngx.say(ip_blacklist);
        end
end

Lua 运算符及教程

lua-nginx-module模块里ngx_lua的所有指令以及可用ngx所有方法

Nginx模块Lua-Nginx-Module学习笔记———Lua指令详解(Directives)



我来说说