1. nginx文件版本及系統環境
nginx | 1.17.3 |
nginx-upload-module | 2.3.0 |
系統 | centos7 |
2. 文件下載
nginx和nginx-upload-module
1)原生的nginx沒有upload-module,需要重新編譯nginx,添加nginx-upload-module
2) nginx下載 http://nginx.org/en/download.html,下載最新版
3)nginx-upload-module下載 https://github.com/fdintino/nginx-upload-module/releases,下載最新版
3. 編譯
1)將nginx和nginx-upload-module放在解壓到/home的目錄下
2)在nginx目錄下,運行nginx的配置腳本
./configure --prefix=/home/software/nginx --sbin-path=/home/software/nginx/sbin/nginx --conf-path=/home/software/nginx/conf/nginx.conf --with-http_ssl_module --with-http_stub_status_module --add-module=./nginx-upload-module-2.3.0
3)在nginx目錄下,安裝 make & make install, 完成nginx帶上傳模塊的安裝。本次安裝時采用了絕對路徑,如果需要相對路徑自行修改--sbin-path和--conf-path即可。
4. nginx upload配置
server { client_max_body_size 100m; listen 8077; # Upload form should be submitted to this location location /file/ { # Pass altered request body to this location upload_pass @fileserver_backend; upload_pass_args on; # Store files to this directory # The directory is hashed, subdirectories 0 1 2 3 4 5 6 7 8 9 should exist upload_store /home/software/nginx/tmp-file 1; # Allow uploaded files to be read only by user upload_store_access user:rw group:rw all:r; # 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 "^.*"; upload_limit_rate 0; upload_cleanup 400-599; } # Pass altered request body to a backend location @fileserver_backend { proxy_pass http://127.0.0.1:2230; } }
nginx upload上傳的文件會以臨時文件的方式存在路勁(/home/software/nginx/tmp-file)中,文件名是文件上傳的順序編號,不帶文件后綴。其中關於文件的信息會作為參數傳到 fileserver_backend 后續處理程序中,后續程序可以使用多種方式實現,本文提供一種java接口的方式。
5. nginx后續處理程序接口
@RequestMapping(value = "/file/", method = RequestMethod.POST) public ResponseEntity fileRenameAndSave(HttpServletRequest request){ /** * 略 */ return ResponseEntity.status(HttpStatus.OK).body(""); }
至此,nginx linux版上傳模塊的功能介紹完。nginx windows版上傳模塊編譯包可在 https://download.csdn.net/download/Lpig900911/12287434 下載。