wget http:
//luajit
.org
/download/LuaJIT-2
.0.5.
tar
.gz
tar
-zxvf LuaJIT-2.0.5.
tar
.gz
cd
LuaJIT-2.0.5
make
&&
make
install
PREFIX=
/usr/local/LuaJIT
# lua
export
LUAJIT_LIB=
/usr/local/LuaJIT/lib
export
LUAJIT_INC=
/usr/local/LuaJIT/include/luajit-2
.0
下载 ngx_devel_kit 模块
wget https:
//github
.com
/simpl/ngx_devel_kit/archive/v0
.3.0.
tar
.gz
NDK (nginx development kit) 模块是一个拓展 nginx 服务器核心功能的模块,第三方模块开发可以基于它来快速实现。 NDK 提供函数和宏处理一些基本任务, 减轻第三方模块开发的代码量
下载 lua-nginx-module 模块
wget https:
//github
.com
/openresty/lua-nginx-module/archive/v0
.10.9rc7.
tar
.gz
lua-nginx-module 模块使 nginx 中能直接运行 lua
查看原始编译
/opt/nginx/sbin/nginx -V
如:
configure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_sub_module --with-http_v2_module
进入 nginx 原始目录:
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_sub_module --with-http_v2_module --add-module=/root/lua-nginx-module-0.10.9rc7/ --add-module=/root/ngx_devel_kit-0.3.0
只 make,不执行 make install。
编译报错应该就是 lua 环境变量不对。
nginx -V 命令报错
./nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory
解决:
echo "/usr/local/LuaJIT/lib" >> /etc/ld.so.conf
ldconfig
成功之后可以 nginx -V 查看,无报错即可。
把原来的 nginx 备份为 nginx_old
cp objs/nginx 到原来的 nginx 并覆盖。
在编译目录执行 (在nginx是启动状态)
make
upgrade
Nginx 添加 lua 模块
测试:
server{
...
location
/lua
{
default_type
‘text/html‘
;
content_by_lua ‘
ngx.say(
"hello, lua!"
)
‘;
}
...
}
浏览器打开:
http://blog.13sai.com/lua
可以看到 hello, lua!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
方法 | 类型 | 说明 |
---|---|---|
ngx.var | 请求 | 如果要赋值如 ngx.var.b = 2,此变量必须提前声明;另外对于 nginx location 中使用 正则捕获的捕获组可以使用 ngx.var [捕获组数字]获取; |
ngx.req.get_headers | 请求 | 获取请求头,默认只获取前100,如果想要获取所以可以调用ngx.req.get_header s(0);获取带中划线的请求头时请使用如 headers.user_agent 这种方式;如果一个请求头有多个值,则返回的 是 table; |
ngx.req.get_uri_args | 请求 | 获取 url 请求参数,其用法和 get_headers 类似; |
ngx.req.get_post_args | 请求 | 获取 post 请求内容体,其用法和 get_headers 类似,但是必须提前调用 ngx.req.r ead_body() 来读取 body 体(也可以选择在 nginx 配置文件使用lua_need_request_body on;开启读取 bod y 体,但是官方不推荐); |
ngx.req.raw_header | 请求 | 未解析的请求头字符串; |
ngx.req.get_body_data | 请求 | 为解析的请求 body 体内容字符串。 |
ngx.req.get_method | 请求 | 获取请求的大写字母形式的请求方式 |
ngx.header | 响应 | 通过ngx.header.header_name的形式获取或设置响应头信息。 |
ngx.exit | 响应 | 以某个状态码返回响应内容 |
ngx.redirect | 响应 | 重定向当前请求到新的 url |
ngx.log | 其他 | 输出到log日志 |
ngx.re.match | 其他 | 正则匹配 |
ngx.md5 | 其他 | md5编码 |
ngx.encode_base64 | 其他 | base64解码 |
ngx.decode_base64 | 其他 | base64编码 |
每个模块都有*_lua
(指令)、*_lua_block
(代码块)、*_lua_file
(脚本文件)
指令 | 所在阶段 | 使用范围 | 说明 |
---|---|---|---|
init_by_lua | 加载配置文件 | http | 可以用于初始化全局配置 |
set_by_lua | rewrite | server location location if | 复杂逻辑的变量赋值,注意是阻塞的 |
rewrite_by_lua | rewrite | http server location location if | 实现复杂逻辑的转发或重定向 |
content_by_lua | content | location location if | 处理请求并输出响应 |
header_filter_by_lua | 响应头信息过滤 | http server location location if | 设置响应头信息 |
body_filter_by_lua | 输出过滤 | http server location location if | 对输出进行过滤或修改 |
原文:https://www.cnblogs.com/winss/p/13605087.html