赞
踩
接下来主要讲一下nginx扩展lua安装配置
可能要用到以下的库文件
1、安装nginx1.18 下载http://nginx.org/en/download.html 解压tar -xvf nginx-1.18.tar.gz
2、安装LuaJIT-2.0.4(轻量级lua)http://luajit.org/download.html 修改makefile 里面的安装路径export PREFIX= /usr/local/luajit 然后安装 make &make install
3、安装lua-nginx-module-0.10.9rc7 下载https://github.com/openresty/lua-nginx-module 解压
4、 安装ngx_devel_kit-0.3.0(NDK提供函数和宏处理一些基本任务,减轻第三方模块开发的代码量)下载https://github.com/simpl/ngx_devel_kit/
5、安装编译 nginx1.18,导入
export LUAJIT_LIB=/usr/local/luajit/lib
export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0
./configure --prefix=/www/server/nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-pcre --user=nginx --group=nginx --with-http_stub_status_module --with-threads --add-module=/root/lua-nginx-module-0.10.9rc7 --add-module=/root/ngx_devel_kit-0.3.0 --with-stream
make -j2
**这里要注意如果之前已经存在安装了的nginx,则不要执行make install,否则会把之前的覆盖,之前没有安装过就执行make install
启动/usr/local/nginx/sbin/nginx 重启命令 usr/local/nginx/sbin/nginx -s reload
如果报错找不到luajit库 ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2
测试nginx直接打开浏览器就可以了http:10.x.x.x:8080 就可以看到欢迎界面了
6 、配置conf/nginx.conf 运行lua脚本
增加lua库的查找路径lua_package_path,lua_package_cpath
7、增加mysql.lua 下载 https://github.com/openresty/lua-resty-mysql 拷贝到lua_package_path 目录下就可 了
8、增加csjon http://www.kyne.com.au/~mark/software/download/lua-cjson-2.1.0.tar.gz 修改Makefile 里面的PREFIX=/usr/local/luajit 就是luajit 的安装路径,make后将生成的cjson.so 拷贝到lua_package_cpath 目录下
9、安装lpack 可以用现成的lpack.lua http://blog.csdn.net/tom_221x/article/details/41119715 拷贝到lua_package_path 或者用 https://github.com/LuaDist/lpack 编译生成 lpack.so 拷贝到 lua_package_cpath 64位需要增加编译命令-fPIC
10、upload.lua 下载https://github.com/openresty/lua-resty-upload
11、md5下载 https://github.com/openresty/lua-resty-string
12、上传页代码 index.html
<!DOCTYPE html>
<html>
<head>
<title>File upload example</title>
</head>
<body>
<form action="uptest" method="post" enctype="multipart/form-data">
<input type="file" name="testFileName"/>
<input type="submit" name="upload" value="Upload" />
</form>
</body>
</html>
13、上传实现/www/server/nginx/lua/uptest.lua
package.path = '/www/server/nginx/lua/?.lua;'
package.cpath = '/usr/local/luajit/lib/lua/5.1/?.so;'
local upload = require "upload"
local chunk_size = 4096
local form = upload:new(chunk_size)
local file
local msg
local filelen=0
form:set_timeout(0) -- 1 sec
local filename
function get_filename(res)
local filename = ngx.re.match(res,'(.+)filename="(.+)"(.*)')
if filename then
return filename[2]
end
end
local osfilepath = "/data/hotfix/uploads/"
local i=0
while true do
local typ, res, err = form:read()
if not typ then
ngx.say("failed to read: ", err)
return
end
if typ == "header" then
if res[1] ~= "Content-Type" then
filename = get_filename(res[2])
if filename then
i=i+1
filepath = osfilepath .. filename
file, msg = io.open(filepath,"w+")
if not file then
ngx.say("failed to open file "..filepath.." msg:"..msg)
return
end
else
end
end
elseif typ == "body" then
if file then
filelen= filelen + tonumber(string.len(res))
file:write(res)
else
end
elseif typ == "part_end" then
if file then
file:close()
file = nil
--ngx.say("file upload success")
--ngx.say(ngx.var.http_host)
local url = "http://"..ngx.var.http_host.."/uploads"
-- ngx.say(url)
ngx.redirect(url)
end
elseif typ == "eof" then
break
else
end
end
if i==0 then
ngx.say("please upload at least one file!")
return
end
14、在nginx目录上创建一下目录lua把要用的代码全部放到这个目录里面
这里的lua文件来源于上面库的安装以及luajit里面的文件,还有要实现的功能文件
15、网站配置文件加入如下配置:
server
{
...........................省略.....
charset utf-8,gbk; # 避免中文乱码
#下载配置
location /uploads {
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
if ($request_filename ~* ^.*?\.(doc|txt|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx)$){
add_header Content-Disposition: 'attachment;';
}
}
#lua执行测试
location /lua {
default_type 'text/plain';
content_by_lua 'ngx.say("hello, 你好呀")';
}
#文件上传配置
location /uptest {
default_type 'text/plain';
error_log /www/server/nginx/logs/uptest.log;
lua_code_cache off;
content_by_lua_file 'lua/uptest.lua';
}
...........................省略.....
}
上传post接口就是http://ip/uptest
下载接口就是 http://ip/uploads
16、如何无法下载txt文件,则需要修改
加一行: application/octet-stream txt;
并去掉: text/plain txt;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。