nginx安装lua扩展模块
1.下载安装LuaJIT-2.0.4.tar.gz
wget -c http://luajit.org/download/LuaJIT-2.0.4.tar.gz
tar xzvf LuaJIT-2.0.4.tar.gz
cd LuaJIT-2.0.4
make install PREFIX=/usr/local/luajit
#注意环境变量!
export LUAJIT_LIB=/usr/local/luajit/lib
export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0
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
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
4.下载安装nginx-1.10.3.tar.gz
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 --add-module=/opt/soft/ngx_devel_kit-0.3.0 --add-module=/opt/soft/lua-nginx-module-0.10.8
#注意ngx_devel_kit和lua-nginx-module以实际解压路径为准
make -j2
make install
注:报错gcc需要安装,可以执行
yum install -y gcc g++ gcc-c++
依赖报错,可以执行
yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel
5.验证
#将nginx做成命令
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
cd /usr/local/nginx/conf/
vi nginx.conf
#lua指令方式
#在server 中添加一个localtion
location /hello {
default_type 'text/plain';
content_by_lua 'ngx.say("hello, lua")';
}
#lua文件方式
#在server 中添加一个localtion
location /lua {
default_type 'text/html';
content_by_lua_file conf/lua/test.lua; #相对于nginx安装目录
}
#test.lua文件内容
ngx.say("hello world");
#启动nginx(已经做了软连接了,可以在任何目录启动)
nginx
注:
报错nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory 可以执行
ln -s /usr/local/luajit/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2
具体source的路径已实际安装路径为准
window平台开发代码部署到Linux服务器
由于两个平台的不同导致脚本文件的编码类型不一致 部署后会出现一些诡异的问题
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
2018/09/07 19:19:30 [error] 6729#0: *22 failed to load external Lua file "/etc/nginx/lua/limit_reload.lua": /etc/nginx/lua/limit_reload.lua:1: '=' expected near 'limitRuleStore', client: 192.168.11.134, server: localhost, request: "GET /limit/reload HTTP/1.1", host: "192.168.11.134:81"
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
[root@localhost lua]# cat -A init_conf.lua M-oM-;M-?--M-eM-.M-^ZM-dM-9M-^IM-eM-^EM-(M-eM-1M-^@M-eM-^OM-^XM-iM-^GM-^OM-oM-<M-^LM-iM-;M-^XM-hM-.M-$M-dM-;M-^NlualibM-iM-^GM-^LM-iM-^]M-"M-eM-^JM- M-hM-=M-=^M$ limit_req ^I= require "resty.limit.req"; ^M$ http ^I^I= require "resty.http";^M$ limitUtils ^I= require "limit_utils"; ^M$ limit_ntfy_url ^I^I= ^I"http://"..minigo_host.."/minigo/limitnotify/add";^M$ [root@localhost lua]# cat init_conf.lua --定义全局变量,默认从lualib里面加载 limit_req = require "resty.limit.req"; http = require "resty.http"; limitUtils = require "limit_utils"; cjson = require "cjson"; [root@localhost lua]# file a.txt a.txt: ASCII text [root@localhost lua]# file init_conf.lua init_conf.lua: UTF-8 Unicode (with BOM) text [root@hz-ds-itsgj-199-136-48 lua]# file limit_main.lua limit_main.lua: ISO-8859 English text [root@hz-ds-itsgj-199-136-48 lua]# file limit_reload.lua limit_reload.lua: ISO-8859 text [root@hz-ds-itsgj-199-136-48 lua]#