0. 說明
這種方式其實複雜,麻煩!建議經過這個方式搭建Openresty文件上傳和下載服務器:http://www.cnblogs.com/lujiango/p/9056680.htmlhtml
1. 包下載
openresty-1.13.6.1下載地址 https://openresty.org/download/openresty-1.13.6.1.tar.gzjava
nginx-upload-module-2.2因爲原做者已經很長時間不更新了,原本從原做者github下載的時候,編譯openresty的時候報錯:ngx_http_upload_module.c:14:17: fatal error: md5.h: No such file or directorynginx
2. 編譯
(1)分別解壓openresty-1.13.6.1.tar.gz和nginx-upload-module-2.2.zipgit
(2)執行/sdf/slb/openresty-1.13.6.1# ./configure --prefix=/sdf/slb/openresty --add-module=/sdf/slb/nginx_upload_module-2.2.0github
3. 配置
3.1 nginx.conf
須要配置upload_pass做爲文件上傳以後的返回處理。ruby
worker_processes 20; error_log logs/error.log notice; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; server { listen 8081; client_max_body_size 100m; # Upload form should be submitted to this location location /upload {文件上傳的action # Pass altered request body to this location upload_pass /success;文件上傳以後的返回 # Store files to this directory # The directory is hashed, subdirectories 0 1 2 3 4 5 6 7 8 9 should exist upload_store /sdf/slb/store 1;文件存儲的路徑,要先手動建立0 1 2 3 4 5 6 7 8 9一共10個文件夾 # Allow uploaded files to be read only by user upload_store_access user:rw; # Set specified fields in request body upload_set_form_field "${upload_field_name}_name" $upload_file_name; upload_set_form_field "${upload_field_name}_content_type" $upload_content_type; upload_set_form_field "${upload_field_name}_path" $upload_tmp_path; # Inform backend about hash and size of a file upload_aggregate_form_field "${upload_field_name}_md5" $upload_file_md5; upload_aggregate_form_field "${upload_field_name}_size" $upload_file_size; upload_pass_form_field "^submit$|^description$"; } # Pass altered request body to a backend location /success { content_by_lua 'ngx.say("upload success!")'; } location /site { root html; index upload.html;支持文件上傳的靜態頁面 } } }
3.2 配置靜態頁面訪問,做爲文件上傳的入口
搭建openresty靜態頁面訪問,請參考http://www.cnblogs.com/lujiango/p/9001006.html#_lab2_4_0服務器
upload.html的文件內容以下:請注意其中的action="/upload",與nginx.conf裏面的location /upload{}是對應的。app
<html> <head> <title>Test upload</title> </head> <body> <h2>Select files to upload</h2> <form enctype="multipart/form-data" action="/upload" method="post"> <input type="file" name="file1"><br> <input type="file" name="file2"><br> <input type="file" name="file3"><br> <input type="file" name="file4"><br> <input type="file" name="file5"><br> <input type="file" name="file6"><br> <input type="submit" name="submit" value="Upload"> <input type="hidden" name="test" value="value"> </form> </body> </html>
頁面以下:post
3.3 文件上傳
訪問http:192.168.25.84:8081/site或http:192.168.25.84:8081/site/upload.htmlthis
能夠進入文件上傳的入口頁面,選擇文件上傳便可,以下是文件上傳成功。
此時,在nginx.conf配置的文件存儲位置有以下文件:
其中的0000000010和0000000020就是上傳的文件。
注:在上傳文件的時候報錯:/sdf/slb/store/6/0000000006" for "89629.log" (13: Permission denied),這個是nginx權限的問題,能夠參考https://www.ruby-forum.com/topic/1379755
x. 參考資料
https://www.ruby-forum.com/topic/1379755
http://www.grid.net.ru/nginx/upload.en.html
https://github.com/Austinb/nginx-upload-module/blob/2.2/nginx.conf