当前位置:   article > 正文

nginx文件上传模块 nginx_upload_module

nginx upload

1、编译安装nginx

wget https://github.com/fdintino/nginx-upload-module/archive/refs/heads/master.zip

PS:原先使用的nginx-upload-module-2.2编译的时候报错:ngx_http_upload_module.c:14:17: fatal error: md5.h: No such file or directory

后来找到一个可用的fork版本 https://github.com/Austinb/nginx-upload-module
我下载得zip包 nginx-upload-module-master.zip
  1. # yum -y install openssl libssl-dev gcc gcc-c++ make
  2. # cd /data
  3. # unzip nginx-upload-module-master.zip
  4. # cd nginx-1.19.9/
  5. # ./configure --prefix=/data/nginx --add-module=../nginx-upload-module-master --with-http_secure_link_module
  6. # make && make install

2、编辑nginx配置文件

vim conf/nginx.conf

  1. #user nobody;
  2. worker_processes 1;
  3. #error_log logs/error.log;
  4. #error_log logs/error.log notice;
  5. #error_log logs/error.log info;
  6. #pid logs/nginx.pid;
  7. events {
  8. worker_connections 1024;
  9. }
  10. http {
  11. include mime.types;
  12. default_type application/octet-stream;
  13. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  14. # '$status $body_bytes_sent "$http_referer" '
  15. # '"$http_user_agent" "$http_x_forwarded_for"';
  16. #access_log logs/access.log main;
  17. sendfile on;
  18. #tcp_nopush on;
  19. #keepalive_timeout 0;
  20. keepalive_timeout 65;
  21. #gzip on;
  22. server {
  23. listen 80;
  24. client_max_body_size 100m;
  25. location / {
  26. root html;
  27. index index.html index.htm;
  28. }
  29. error_page 405 =200 @405;
  30. # Upload form should be submitted to this location
  31. location /upload {
  32. if ($request_method = 'GET'){
  33. root html;
  34. }
  35. if ($request_method = 'POST'){
  36. # Pass altered request body to this location
  37. upload_pass @test;
  38. # Store files to this directory
  39. # The directory is hashed, subdirectories 0 1 2 3 4 5 6 7 8 9 should exist
  40.  #文件存储的路径,要先手动创建0 1 2 3 4 5 6 7 8 9一共10个文件夹
  41. upload_store /data/nginx/html/upload 1;
  42. # Allow uploaded files to be read only by user
  43. upload_store_access user:rw;
  44. # Set specified fields in request body
  45. upload_set_form_field "${upload_field_name}_name" $upload_file_name;
  46. upload_set_form_field "${upload_field_name}_content_type" $upload_content_type;
  47. upload_set_form_field "${upload_field_name}_path" $upload_tmp_path;
  48. # Inform backend about hash and size of a file
  49. upload_aggregate_form_field "${upload_field_name}_md5" $upload_file_md5;
  50. upload_aggregate_form_field "${upload_field_name}_size" $upload_file_size;
  51. upload_pass_form_field "^submit$|^description$";
  52. upload_cleanup 400 404 499 500-505;
  53. }
  54. }
  55. # Pass altered request body to a backend
  56. location @test {
  57. #content_by_lua ngx.say("upload success!");
  58. proxy_pass http://localhost:8888;
  59. return 200;
  60. }
  61. }
  62. }

使用 nginx -t 检测一下配置文件是否正确

我这里遇到一个问题  如果不使用 if 判断 开启uoload_pass @test 的话直接访问页面会报405

创建存储目录和访问的index首页

# mkdir -p html/upload/{0..9}

vim html/upload/index.html

  1. <html>
  2. <head>
  3. <title>Test upload</title>
  4. </head>
  5. <body>
  6. <h2>Select files to upload</h2>
  7. <form enctype="multipart/form-data" action="/upload" method="post">
  8. <input type="file" name="file1"><br>
  9. <input type="file" name="file2"><br>
  10. <input type="file" name="file3"><br>
  11. <input type="file" name="file4"><br>
  12. <input type="file" name="file5"><br>
  13. <input type="file" name="file6"><br>
  14. <input type="submit" name="submit" value="Upload">
  15. <input type="hidden" name="test" value="value">
  16. </form>
  17. </body>
  18. </html>

3、启动nginx

./sbin/nginx

4、浏览器访问 http://192.168.53.100/upload/,结果如下

5、上传文件测试

可以看到选中文件点upload 的时候报出了503,需要修改一下存储目录权限

  1. # chmod 777 {0..9}
  2. [root@localhost upload]# ll
  3. total 4
  4. drwxrwxrwx. 2 root root 6 Mar 14 00:07 0
  5. drwxrwxrwx. 2 root root 6 Mar 14 00:07 1
  6. drwxrwxrwx. 2 root root 6 Mar 14 00:07 2
  7. drwxrwxrwx. 2 root root 6 Mar 14 00:07 3
  8. drwxrwxrwx. 2 root root 6 Mar 15 00:56 4
  9. drwxrwxrwx. 2 root root 6 Mar 15 00:56 5
  10. drwxrwxrwx. 2 root root 6 Mar 14 00:07 6
  11. drwxrwxrwx. 2 root root 6 Mar 14 00:07 7
  12. drwxrwxrwx. 2 root root 6 Mar 14 00:07 8
  13. drwxrwxrwx. 2 root root 6 Mar 14 00:07 9
  14. -rw-r--r--. 1 root root 501 Mar 14 00:14 index.html

重新上传试试  上传成功

我们去服务器查看文件存储位置

6、添加账号登录验证

使用 htpasswd 生成用户密码

  1. # htpasswd -c conf/htpasswd test1
  2. New password:
  3. Re-type new password:
  4. Adding password for user test1

编辑nginx.conf配置文件,在upload下添加

  1. auth_basic 'upload balance file';
  2. auth_basic_user_file htpasswd;

重启nginx,浏览器访问

可以看到现在登录页面需要账号密码验证,使用自己创建的账号密码登录即可

7、服务器使用curl 进行文件上传调试

# curl -v -u 'test1:test1' -F file1=@1.txt -H "Content-Type:multipart/form-data" -H "Content-Disposition:attachment;filename=1.txt" -X POST http://localhost/upload

8、编写文件上传脚本

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/232389
推荐阅读
相关标签
  

闽ICP备14008679号