文档:https://github.com/openresty/lua-nginx-module

安装

安装LuaJIT

  1. # 下载LuaJIT安装包
  2. wget https://luajit.org/download/LuaJIT-2.0.5.tar.gz
  3. # 解压并安装到指定目录
  4. tar -zxvf LuaJIT-2.0.5.tar.gz
  5. cd LuaJIT-2.0.5.tar.gz
  6. make PREFIX=/usr/local/luajit && make install PREFIX=/usr/local/luajig
  7. # 配置环境变量
  8. echo "/usr/local/luajit/lib" > /etc/ld.so.conf.d/usr_local_luajit_lib.conf
  9. export LUAJIT_LIB=/usr/local/luajit/lib
  10. export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0
  11. # 可能需要
  12. ln -s /usr/local/luajit/lib/libluajit-5.1.so.2 /usr/lib64/libluajit-5.1.so.2

安装Nginx对应的Lua模块

  1. #下载nginxLua模块需要的相关模块包
  2. wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
  3. tar -zxvf v0.3.0.tar.gz
  4. wget https://github.com/openresty/lua-nginx-module/archive/v0.10.20.tar.gz
  5. tar -zxvf v0.10.20.tar.gz
  6. # 由于我已经安装好Nginx了,这里我不重装,仅仅只是重新编译,并替换nginx执行文件即可
  7. # 查看之前安装时相关的配置
  8. $ /usr/local/nginx/nginx -V
  9. nginx version: nginx/1.20.2
  10. built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
  11. built with OpenSSL 1.0.2k-fips 26 Jan 2017
  12. TLS SNI support enabled
  13. configure arguments: --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_gzip_static_module --with-http_stub_status_module --with-file-aio --with-http_realip_module --with-http_ssl_module
  14. # 拿到configure arguments相关配置后,在其后拼接上我们需要添加的模块
  15. $ cd /home/data/nginx-1.20.2
  16. $ ./configure --prefix=/usr/local/nginx \
  17. --sbin-path=/usr/local/nginx/nginx \
  18. --conf-path=/usr/local/nginx/nginx.conf \
  19. --pid-path=/usr/local/nginx/nginx.pid \
  20. --with-http_gzip_static_module \
  21. --with-http_stub_status_module \
  22. --with-file-aio --with-http_realip_module \
  23. --with-http_ssl_module \
  24. --with-pcre --add-module=/home/data/lua-nginx-module-0.10.20 --add-module=/home/data/ngx_devel_kit-0.3.0
  25. # 注意,这里只是执行 make -j 2,并不执行 make install
  26. $ make -j 2
  27. # 备份原来的nginx执行文件
  28. $ mv /usr/local/nginx/nginx /usr/local/nginx/nginx.bak
  29. # 将objs/nginx文件copy到/usr/local/nginx中
  30. $ cp objs/nginx /usr/local/nginx/nginx
  31. # 并确定权限是否为755
  32. # 执行测试,是否成功
  33. $ ./nginx -V
  34. ./nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory
  35. # 修复上述错误
  36. $ find / -name libluajit-5.1.so.2
  37. /usr/local/luajit/lib/libluajit-5.1.so.2
  38. # 手动建立软连接
  39. $ ln -s /usr/local/luajit/lib/libluajit-5.1.so.2 /usr/lib64/
  40. # 再次测试
  41. $ ./nginx -V
  42. nginx version: nginx/1.20.2
  43. built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
  44. built with OpenSSL 1.0.2k-fips 26 Jan 2017
  45. TLS SNI support enabled
  46. configure arguments: --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_gzip_static_module --with-http_stub_status_module --with-file-aio --with-http_realip_module --with-http_ssl_module --with-pcre --add-module=/home/data/lua-nginx-module-0.10.20 --add-module=/home/data/ngx_devel_kit-0.3.0
  47. # 重启nginx服务
  48. ./nginx -s reload
  49. # 如果出现下列报错
  50. nginx: [alert] detected a LuaJIT version which is not OpenResty's; many optimizations will be disabled and performance will be compromised (see https://github.com/openresty/luajit2 for OpenResty's LuaJIT or, even better, consider using the OpenResty releases from https://openresty.org/en/download.html)
  51. nginx: [alert] failed to load the 'resty.core' module (https://github.com/openresty/lua-resty-core); ensure you are using an OpenResty release from https://openresty.org/en/download.html (reason: module 'resty.core' not found:
  52. no field package.preload['resty.core']
  53. no file './resty/core.lua'
  54. no file '/usr/local/luajit/share/luajit-2.0.5/resty/core.lua'
  55. no file '/usr/local/share/lua/5.1/resty/core.lua'
  56. no file '/usr/local/share/lua/5.1/resty/core/init.lua'
  57. no file '/usr/local/luajit/share/lua/5.1/resty/core.lua'
  58. no file '/usr/local/luajit/share/lua/5.1/resty/core/init.lua'
  59. no file './resty/core.so'
  60. no file '/usr/local/lib/lua/5.1/resty/core.so'
  61. no file '/usr/local/luajit/lib/lua/5.1/resty/core.so'
  62. no file '/usr/local/lib/lua/5.1/loadall.so'
  63. no file './resty.so'
  64. no file '/usr/local/lib/lua/5.1/resty.so'
  65. no file '/usr/local/luajit/lib/lua/5.1/resty.so'
  66. no file '/usr/local/lib/lua/5.1/loadall.so') in /usr/local/nginx/nginx.conf:117
  67. # 需要手动下载相关依赖库
  68. wget https://github.com/openresty/lua-resty-lrucache/archive/refs/tags/v0.11.tar.gz
  69. wget https://github.com/openresty/lua-resty-core/archive/refs/tags/v0.1.22.tar.gz
  70. tar -zxvf v0.11.tar.gz
  71. tar -zxvf v0.1.22.tar.gz
  72. # 并修改nginx.conf文件,在http块中添加:
  73. lua_package_path "/home/data/lua-resty-core-0.1.22/lib/?.lua;/home/data/lua-resty-lrucache-0.11/lib/?.lua;;";
  74. # 再次重启
  75. ./nginx -s reload

Nginx使用Lua脚本

Nginx配置

  1. http {
  2. ...
  3. # 配置Lua外部库的搜索路径(;;表示默认路径)
  4. lua_package_path '/home/data/lua-resty-core-0.1.22/lib/?.lua;/home/data/lua-resty-lrucache-0.11/lib/?.lua;;';
  5. server {
  6. location /lua {
  7. # 设置MIME type,否则会被当成文件下载
  8. default_type 'text/plain';
  9. # 配置Lua相关脚本
  10. ...
  11. }
  12. }
  13. }

NginxLua脚本相关

在nginx中使用lua相关指令,一定要注意执行的先后顺序!

NginxLua指令执行顺序

image.png

Nginx比较主要的Lua指令

content_by_lua_block

介绍:此指令会在nginx处理请求的内容生成阶段执行,不要在同一个位置使用此指令和其他内容处理程序指令,例如proxy_pass
语法:content_by_lua_block {lua代码}
位置:location, location if
示例:

  1. content_by_lua_block {
  2. ngx.say("Hello World!")
  3. }

content_by_lua_file

介绍:与content_by_lua_block相同
语法:content_by_lua_file
位置:location, location if
示例:

  1. location ~ ^/app/([-_a-zA-Z0-9/]+) {
  2. set $path $1;
  3. content_by_lua_file /path/to/lua/app/root/$path.lua;
  4. }

access_by_lua_block

介绍:此指令会在请求阶段执行,可以用他来进行黑名单过滤等前置操作。
语法:access_by_lua_block {lua代码}
位置:http, server, location, location if
示例:

  1. location / {
  2. access_by_lua_block {
  3. -- 执行相关黑名单校验
  4. }
  5. content_by_lua_block {
  6. -- 只有通过access_by_lua_block 验证后,才会走到该方法中
  7. }
  8. }

access_by_lua_file

介绍:与access_by_lua_block相同
语法:access_by_lua_file
位置:http, server, location, location if