1.安装编译环境及依赖
gcc c语言编译环境
pcre(Perl Compatible Regular Expressions)是一个perl库,包含perl兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式
pcre-devel是使用pcre开发的一个二次开发库
zlib库提供很多种压缩和解压缩方式,nginx使用zlib对http包的内容进行gzip压缩
openssl可以实现 秘钥证书管理、对称加密和非对称加密
yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
2.安装lua支持
2.1 LuaJIT
cd /tmp && wget http://luajit.org/download/LuaJIT-2.0.4.tar.gz
tar -zxvf LuaJIT-2.0.4.tar.gz
cd LuaJIT-2.0.4
make install PREFIX=/usr/local/luajit
#修改环境变量
echo "export LUAJIT_LIB=/usr/local/luajit/lib" >> /etc/profile
echo "export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0" >> /etc/profile
source /etc/profile
添加软链接,可以被nginx引用到
ln -s /usr/local/luajit/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2
2.2 ngx_devel_kit
wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
tar -xzvf v0.3.0.tar.gz
2.3 lua-nginx-module
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.8.tar.gz
tar -xzvf v0.10.8.tar.gz
3.下载安装nginx
cd /tmp/ && wget http://nginx.org/download/nginx-1.10.3.tar.gz
tar -xzvf nginx-1.10.3.tar.gz
cd nginx-1.10.3
./configure --with-http_ssl_module --add-module=/tmp/ngx_devel_kit-0.3.0 --add-module=/tmp/lua-nginx-module-0.10.8
支持http2
./configure --with-http_v2_module --with-http_ssl_module --add-module=/tmp/ngx_devel_kit-0.3.0 --add-module=/tmp/lua-nginx-module-0.10.8
make -j2
make install
#创建nginx的软链接
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
4.验证lua
cd /usr/local/nginx/conf/
vi nginx.conf
4.1 lua指令方式
#在server 中添加一个localtion
location /hello {
default_type 'text/plain';
content_by_lua 'ngx.say("hello, lua")';
}
4.2 lua文件方式
#在server 中添加一个localtion
location /lua {
default_type 'text/html';
content_by_lua_file lua/test.lua; #相对于nginx安装目录
}
#test.lua文件内容
ngx.say("hello world");
#启动nginx
nginx
访问:
http://127.0.0.1/hello
显示:hello, lua
http://127.0.0.1/lua
显示:hello world