在Nginx上使用Lua脚本能做很多有价值的事情,比如生成请求的唯一标识(排查问题)、统一的权限校验、限流防洪(保证服务正常可用)等。
Lua是什么?
Lua是一种轻量级的多范式编程语言,其被设计为一种可扩展的语义脚本语言作为首要目标。(英语发音:/ˈluːə/,是葡萄牙语中“Lua”(月亮)的意思)[摘自] Lua (programming language) - Wikipedia、Lua - 维基百科
Lua是一个功能强大、快速、轻量级、可嵌入的脚本语言。[摘自] Lua: about 官网
本文主要讲述“Lua编译器的选择与安装过程”。
首先,谈谈“Lua编译器的选择”问题。根据“HttpLuaModule - Embed the power of Lua into Nginx”文档的Description部分描述,运行Lua脚本主要可通过标准的Lua解释器或LuaJIT实时编译器。在HttpLuaModule的Installation部分可以看到,“Install LuaJIT 2.0 or 2.1 (recommended) or Lua 5.1 (Lua 5.2 is not supported yet).”。文档里为什么会推荐使用LuaJIT编译器呢?经过网上搜索,发现答案在“Lua vs LuaJIT Performance Comparison on x86/x64 - LuaJIT”,LuaJIT编译器在各方面的性能都要好于Lua解释器(见下图)。
既然选择LuaJIT作为Lua的编译器,下面就需要搭建运行环境。进入LuaJIT的下载页面,下载最新的稳定版本(LuaJIT-2.0.4),也可以从git仓库克隆最新的代码(使用‘git checkout v2.1’来获取LuaJIT 2.1开发分支)。
在LuaJIT的安装页面可以看到,LuaJIT仅发布了源码包,所以必须通过源码包来进行构建、安装并配置运行环境。(LuaJIT_install_shell.7z 是LuaJIT自动化安装脚本)
在Mac上安装“LuaJIT 2.1.0-alpha”时,由于LuaJIT 2.1.0还是alpha版本,还需执行"ln -sf luajit-2.1.0-alpha /usr/local/luajit/bin/luajit"。
Note: the development releases deliberately do NOT install a symlink for luajit You can do this now by running this command (with sudo): ln -sf luajit-2.1.0-alpha /usr/local/luajit/bin/luajit
更多关于Lua的资料,请查看我整理的“Lua - Github”。
从安装日志中,可以大体地了解有哪些东西创建了。
# 构建日志
==== Building LuaJIT 2.0.4 ====
make -C src
make[1]: 正在进入目录 `/usr/local/LuaJIT-2.0.4/src'
...
BUILDVM jit/vmdef.lua
DYNLINK libluajit.so
LINK luajit
OK Successfully built LuaJIT
make[1]:正在离开目录 `/usr/local/LuaJIT-2.0.4/src'
==== Successfully built LuaJIT 2.0.4 ====
# 安装日志
==== Installing LuaJIT 2.0.4 to /usr/local/luajit ====
mkdir -p /usr/local/luajit/bin /usr/local/luajit/lib /usr/local/luajit/include/luajit-2.0 /usr/local/luajit/share/man/man1 /usr/local/luajit/lib/pkgconfig /usr/local/luajit/share/luajit-2.0.4/jit /usr/local/luajit/share/lua/5.1 /usr/local/luajit/lib/lua/5.1
cd src && install -m 0755 luajit /usr/local/luajit/bin/luajit-2.0.4
cd src && test -f libluajit.a && install -m 0644 libluajit.a /usr/local/luajit/lib/libluajit-5.1.a || :
rm -f /usr/local/luajit/bin/luajit /usr/local/luajit/lib/libluajit-5.1.so.2.0.4 /usr/local/luajit/lib/libluajit-5.1.so /usr/local/luajit/lib/libluajit-5.1.so
cd src && test -f libluajit.so && \
install -m 0755 libluajit.so /usr/local/luajit/lib/libluajit-5.1.so.2.0.4 && \
ldconfig -n /usr/local/luajit/lib && \
ln -sf libluajit-5.1.so.2.0.4 /usr/local/luajit/lib/libluajit-5.1.so && \
ln -sf libluajit-5.1.so.2.0.4 /usr/local/luajit/lib/libluajit-5.1.so || :
cd etc && install -m 0644 luajit.1 /usr/local/luajit/share/man/man1
cd etc && sed -e "s|^prefix=.*|prefix=/usr/local/luajit|" -e "s|^multilib=.*|multilib=lib|" luajit.pc > luajit.pc.tmp && \
install -m 0644 luajit.pc.tmp /usr/local/luajit/lib/pkgconfig/luajit.pc && \
rm -f luajit.pc.tmp
cd src && install -m 0644 lua.h lualib.h lauxlib.h luaconf.h lua.hpp luajit.h /usr/local/luajit/include/luajit-2.0
cd src/jit && install -m 0644 bc.lua v.lua dump.lua dis_x86.lua dis_x64.lua dis_arm.lua dis_ppc.lua dis_mips.lua dis_mipsel.lua bcsave.lua vmdef.lua /usr/local/luajit/share/luajit-2.0.4/jit/usr/local/luajit/src/jit
ln -sf luajit-2.0.4 /usr/local/luajit/bin/luajit
==== Successfully installed LuaJIT 2.0.4 to /usr/local/luajit ====
请发表评论