nginx_upload_module

nginx_upload_module

我直接下载master代码,网上说下载2.2版本需要打补丁;但我直接用master版本直接就可以和nginx 1.18.0 编译

nginx

  • 下载nginx源码 ngxin 1.18.0

安装说明: https://www.cnblogs.com/lywJ/p/10710361.html

  • 下载依赖库

    1
    yum install gcc-c++ zlib zlib-devel pcre  pcre-devel zlib  zlib-devel openssl openssl-devel -y
  • 编译nginx_upload_module

    1
    2
    3
    ./configure --prefix=/usr/local/nginx-upload --add-module=/home/nginx_upload_module-master --with-http_ssl_module

    - make & make install

设置nginx.conf

  • git的源码中包含nginx.conf ,复制
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    location /upload {
    # Pass altered request body to this location
    upload_pass /rename.php;

    # Store files to this directory
    # The directory is hashed, subdirectories 0 1 2 3 4 5 6 7 8 9 should exist
    upload_store /tmp;

    # Allow uploaded files to be read only by user
    upload_store_access user: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 "^submit$|^description$";
    }

到 /usr/local/nginx-upload/conf/nginx.conf 中

  • 启动ngxin : /usr/local/nginx-upload/sbin/ 下执行./nginx
  • web访问nginx,主页正常

nginx_upload_module 原理

upload_module 负责http文件的传输,并保存在”upload_store” 临时目录中,保存成功后,会发送消息到”upload_pass” 的url中,携带参数
“file_name”: 上传的文件名
“file_content_type”:文件类型
“file_path”:临时保存路径+文件名
“file_md5”:文件md5
“file_size”:文件大小

支持多文件同时上传,并以数组方式显示。

upload_pass

上传成功后,通过upload_pass 发送http post请求到执行url,并将信息放置在http form表单中,使用php、python等web框架都可以解析出来

测试

  • 使用git主线代码中的”upload.html”,建议把内容复制到nginx的index.html中
  • 在nginx的 /html 下新建 rename.php
1
2
3
4
<?php
header("Content-Type:text/html;charset=utf-8");
print_r($_POST)
?>
  • 打开主页,选择上传,返回正确信息

问题

  • 支持中文文件: 在nginx.conf 的
    http {
    charset utf-8,gbk;
    }

  • 支持大文件: nginx.conf 的clietn_max_body_size 200m;

  • 支持上传进度:默认支持

  • 其他问题先查看ngixn的error.log

参考资料