openresty下载地址 https://openresty.org/download
nginx-upload-moudle下载地址: https://github.com/Austinb/nginx-upload-module
把上面两个安装包放在/data下,并解压
安装源码编译所需环境:
yum install -y gcc zlib zlib-devel pcre pcre-devel openssl openssl-devel perl
进入openresty目录下执行:
./configure --prefix=/data/openresty --add-module=/data/nginx_upload_modulexxxxxx
这里和nginx的源码编译一样,也可以编译其他模块,nginx能用的,这里也能用
make && make install
写入配置:
vi nginx.conf:
- user root;
- worker_processes 20;
-
- error_log logs/error.log notice;
-
- events {
- worker_connections 1024;
- }
-
- http {
- include mime.types;
- default_type application/octet-stream;
- server {
- listen 8082;
- server_name localhost;
- # 最大允许上传的文件大小
- client_max_body_size 200m;
-
- location / {
- root html;
- index index.html index.htm;
- }
- set $store_dir "/sdf/slb/openresty/nginx/html/download/"; # 文件存储路径,注意不要漏掉最后的斜杠 /
- # 文件上传接口:http://xxx:8082/upfile
- location /upfile {
- content_by_lua_file conf/lua/upload.lua; # 实现文件上传的逻辑
- }
- # 文件下载入口: http://xxx:8082/download
- location /download {
- autoindex on;
- autoindex_localtime on;
- root html;
- index index.html;
- }
- # redirect server error pages to the static page /50x.html
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- }
- }
写入upload.lua文件:(位置根据nginx.conf,文件要创建在nginx/conf/lua/upload.lua下)
- -- upload.lua
- --==========================================
- -- 文件上传
- --==========================================
- local upload = require "resty.upload"
- local cjson = require "cjson"
- local chunk_size = 4096
- local form, err = upload:new(chunk_size)
- if not form then
- ngx.log(ngx.ERR, "failed to new upload: ", err)
- ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
- end
- form:set_timeout(1000)
- -- 字符串 split 分割
- string.split = function(s, p)
- local rt= {}
- string.gsub(s, '[^'..p..']+', function(w) table.insert(rt, w) end )
- return rt
- end
- -- 支持字符串前后 trim
- string.trim = function(s)
- return (s:gsub("^%s*(.-)%s*$", "%1"))
- end
- -- 文件保存的根路径
- local saveRootPath = ngx.var.store_dir
- -- 保存的文件对象
- local fileToSave
- --文件是否成功保存
- local ret_save = false
- 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
- -- 开始读取 http header
- -- 解析出本次上传的文件名
- local key = res[1]
- local value = res[2]
- if key == "Content-Disposition" then
- -- 解析出本次上传的文件名
- -- form-data; name="testFileName"; filename="testfile.txt"
- local kvlist = string.split(value, ';')
- for _, kv in ipairs(kvlist) do
- local seg = string.trim(kv)
- if seg:find("filename") then
- local kvfile = string.split(seg, "=")
- local filename = string.sub(kvfile[2], 2, -2)
- if filename then
- fileToSave = io.open(saveRootPath .. filename, "w+")
- if not fileToSave then
- ngx.say("failed to open file ", filename)
- return
- end
- break
- end
- end
- end
- end
- elseif typ == "body" then
- -- 开始读取 http body
- if fileToSave then
- fileToSave:write(res)
- end
- elseif typ == "part_end" then
- -- 文件写结束,关闭文件
- if fileToSave then
- fileToSave:close()
- fileToSave = nil
- end
-
- ret_save = true
- elseif typ == "eof" then
- -- 文件读取结束
- break
- else
- ngx.log(ngx.INFO, "do other things")
- end
- end
- if ret_save then
- ngx.say("save file ok")
- end
找到nginx启动命令,启动nginx:
nginx/sbin/nginx
访问8082端口: 成功如下:
上传文件命令:curl -F 'file=@{要上传文件的绝对路径}' http://192.168.80.131:8082/upfile
例子:curl -F 'file=@/data/test.txt' http://192.168.80.131:8082/upfile
可在nginx.conf 设置上传文件的大小:
查看上传文件:http://192.168.80.131:8082/download/
下载文件:
curl -O http://192.168.80.131:8082/download/test.txt 或者:
wget http://192.168.80.131:8082/download/test.txt