侧边栏壁纸
博主头像
lai博主等级

  • 累计撰写 52 篇文章
  • 累计创建 19 个标签
  • 累计收到 2 条评论

目 录CONTENT

文章目录

Nginx 模块增强:lua-nginx-module 的安装

lai
lai
2024-08-22 / 0 评论 / 0 点赞 / 207 阅读 / 953 字
温馨提示:
本文最后更新于 2024-08-22,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

前言

nginx 通过安装lua-nginx-module 可以执行lua脚本,从而可以实现更多功能,lua-nginx-module OpenResty的核心组件。如果您正在使用此模块,那么实际上是在使用 OpenResty 。Nginx、LuaJIT 和 OpenSSL 官方版本存在各种限制和长期存在的错误,可能会导致lua-nginx-module的某些功能被禁用、无法正常工作或运行速度变慢

版本信息

nginx: 1.26.1
lua-nginx-module:v0.10.27
ngx_devel_kit:v0.3.3
lua-resty-core:v0.1.29
lua-resty-lrucache: v1.4
luajit2-2.1: v2.1-20230410.1
openssl: 3.0.5

安装步骤

这里主要是基于原有nginx新增lua-nginx-module 模块, /app/server/nginx/src 为nginx源码,/app/server/nginx/build为当前nginx编译后路径

上传文件到tmp目录

cd /tmp
tar -zxvf ngx_devel_kit-0.3.3.tar.gz -C /app/server/modules
tar -zxvf nginx-lua-prometheus-0.20240525.tar.gz -C /app/server/modules
tar -zxvf lua-nginx-module-0.10.27.tar.gz  -C /app/server/modules
tar -zxvf luajit2-2.1-20230410.1.tar.gz
tar -zxvf /tmp/lua-resty-lrucache-0.14.tar.gz
tar -zxvf /tmp/lua-resty-core-0.1.29.tar.gz

设置lua-nginx-module模块编译时所需要环境变量

tee -a /etc/profile <<'EOF'
 export LUAJIT_LIB=/app/server/luajit/lib
 export LUAJIT_INC=/app/server/luajit/include/luajit-2.1
EOF

source /etc/profile

查看原编译信息

 /app/server/nginx/build/sbin/nginx -V
# 得到已编译模块信息
--prefix=/app/server/nginx/build --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_stub_status_module --with-openssl=/usr/local/openssl3.0.5 --with-openssl-opt=enable-tlsext 

在原来编译模块中加入新模块

sudo ./configure --prefix=/app/server/nginx/build/1261 --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_stub_status_module --with-openssl=/usr/local/openssl3.0.5 --with-openssl-opt=enable-tlsext --with-stream --with-http_realip_module --with-pcre --add-module=/app/server/modules/lua-nginx-module-0.10.27 --add-module=/app/server/modules/ngx_devel_kit-0.3.3 --with-ld-opt="-Wl,-rpath,/app/server/luajit/lib"

期间可能会报错

checking for LuaJIT 2.x ... not found
    ./configure: error: unsupported LuaJIT version; ngx_http_lua_module requires LuaJIT 2.x.

是由于环境变量无法识别导致,可以直接修改lua-nginx-module下的config文件

#手动设置环境变量
LUAJIT_LIB=/app/server/luajit/lib
LUAJIT_INC=/app/server/luajit/include/luajit-2.1

ngx_lua_opt_I=
ngx_lua_opt_L=
luajit_ld_opt=

ngx_feature_name=
ngx_feature_run=no
ngx_feature_incs=
ngx_feature_test=
...

编译nginx

#j2可以多核心同时编译,提高效率,核心数根据实际cpu设置
make -j2

备份并更新

 cp /app/server/nginx/build/sbin/nginx /app/server/nginx/build/sbin/nginx.bak 
 # 这里使用了相对路径
 cp ./objs/nginx /app/server/nginx/build/sbin/

安装 lua-resty-corelua-resty-lrucache

lug-nginx-module 依赖 lua-resty-core 和 lua-resty-lrucache;lug-nginx-module 不再允许设置 lua_load_resty_coreoff,所以必须安装lua-resty-core 和 lua-resty-lrucache

cd /tmp/lua-resty-core-0.1.29
make install PREFIX=/app/server/nginx/build
cd /tmp/lua-resty-lrucache-0.14
make install PREFIX=/app/server/nginx/build

配置nginx

至此nginx已经完成lua-nginx-module模块的安装,由于设置了lua-nginx-module模块,nginx启动时也必须配置lua_package_path

lua_package_path "/app/server/nginx/build/1261/lib/lua/?.lua;;";

验证

 server {
  listen       80;

  charset utf-8;

  # 方式1.content_by_lua_block lua 片段
  location /hello-lua {
    default_type 'text/plain';
    content_by_lua_block {
      ngx.say("Hello World! Lua & Nginx .")
    }
  }
    
  # 方式2.content_by_lua_file lua 脚本文件路径
  location /hello-lua-file {
    default_type 'text/html';
    content_by_lua_file ./lua/hello.lua;
  }

  # 方式3.access_by_lua 在请求访问阶段处理用于访问控制。
  location /hello-lua-access {
    default_type 'text/html';
    access_by_lua '
      local message = "403 - Hello World! Lua & Nginx  access_by_lua"
      ngx.say(message)
    ';
  }

  # 方式4.content_by_lua 在内容处理阶段接受请求并输出响应。
  location /hello-lua-content {
    default_type 'text/html';
    content_by_lua "ngx.print('Hello World!')";
  }
}
0

评论区