zoukankan      html  css  js  c++  java
  • Nginx文件上传下载实现与文件管理

    1.Nginx 上传

    • Nginx 依赖包下载

        # wget http://www.nginx.org/download/nginx-1.2.2.tar.gzinx

        # wget http://www.grid.net.ru/nginx/download/nginx_upload_module-2.2.0.tar.gz

        # wget http://nchc.dl.sourceforge.net/project/pcre/pcre/8.37/pcre-8.37.tar.gz

        # wget http://zlib.net/zlib-1.2.8.tar.gz

        # tar -zxvf nginx-1.2.2.tar.gz -C software

        # tar zxvf nginx_upload_module-2.2.0.tar.gz -C software/

        # tar zxvf pcre-8.37.tar.gz -C software/

        # tar zxvf zlib-1.2.8.tar.gz -C software/

    • Nginx  Configure

        # ./configure --prefix=/usr/local/nginx --add-module=../nginx_upload_module-2.2.0 --with-http_secure_link_module --with-pcre=/home/ubuntu/software/pcre-8.37 --with-zlib=/home/ubuntu/software/zlib-1.2.8

    • Nginx  Make

        # make

        make过程中,注意查看过程是否出错

        

      • 错误1:

         

        解决:

          Openssl 安装

          检查是否已安装openssl:

          sudo apt-get install openssl

          如果没有安装执行以下操作:
          sudo apt-get install libssl-dev
          sudo apt-get install libssl0.9.8

      • 错误2:

        

        解决:

        Just replace "int result;" by "__attribute__((__unused__)) int result;".

    • Nginx  Make Install

        # make install

    • Nginx  配置 upload

        修改/usr/local/nginx/conf/nginx.conf

        # 上传大小限制(包括所有内容)

        client_max_body_size 100m;

     

        # 上传path配置

        location /upload {

            # 转到后台处理URL

          upload_pass   /uploadHandle;

          # 文件上传以后转交给后端的php代码处理

          # 这个位置是否是文章上传成功到指定目录后 戳发后台处理的地址

          upload_pass /uploadBackendDispose;

     

            # 临时保存路径

            # 可以使用散列

            upload_store /tmp/nginx_upload;

         

            # 上传文件的权限,rw表示读写 r只读

            upload_store_access user:rw;

     

            # 这里写入http报头,pass到后台页面后能获取这里set的报头字段

            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;

     

            # Upload模块自动生成的一些信息,如文件大小与文件md5值

            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$";

          upload_pass_form_field "^.*$";

     

            # 每秒字节速度控制,0表示不受控制,默认0

            upload_limit_rate 0;

     

            # 如果pass页面是以下状态码,就删除此次上传的临时文件

            upload_cleanup 400 404 499 500-505;

        }

        # 将请求转到后端的地址处理

        location /uploadBackendDispose {
               proxy_pass http://localhost:10000/nginx_file_upload;

        }

    • Nginx  启动

        直接运行sudo /usr/local/nginx/sbin/nginx

    • Nginx  上传测试POST

        Html代码

        <html>

        <head>

        <title>Test upload</title>

        </head>

        <body>

        <h2>Select files to upload</h2>

        <form enctype="multipart/form-data" action="http://52.77.234.86/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="submit111" value="Upload">

        <input type="hidden" name="user1" value="value1">

        <input type="hidden" name="user2" value="value2">

        <input type="hidden" name="user3" value="value3">

        </form>

        </body>

        </html>

        可在服务器的/tmp/nginx_upload目录在看到上传的文件

        这部分参数为上传到后台的处理参数:

         

    • Nginx  上传文件附带参数特别说明

      1. Nginx.conf中设置upload_pass_form_field

        # 允许所有的字段

         upload_pass_form_field "^.*$";

      2. Nginx.conf中设置upload_pass_form_field

        在上传表单中增加上传字段

        <input type="submit" name="submit111" value="Upload">

        <input type="hidden" name="user1" value="value1">

        <input type="hidden" name="user2" value="value2">

        <input type="hidden" name="user3" value="value3">

       3. Nginx回调后台的url可接受到以下参数:

        uri:/nginx_file_upload

        headers:{'Content-Length': '1306', 'Accept-Language': 'zh-CN', 'Accept-Encoding': 'gzip, deflate', 'Connection': 'close', 'Accept': 'text/html, application/xhtml+xml, */*', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko', 'Dnt': '1', 'Host': 'localhost:10000', 'Cache-Control': 'no-cache', 'Content-Type': 'multipart/form-data; boundary=---------------------------7df1e01d20270'}

        body_arguments:{"file3":[""],"file2":[""],"file6":[""],"file5":[""],"file4":[""],"file1_content_type":["image/jpeg"],"file1_md5":["fafa5efeaf3cbe3b23b2748d13e629a1"],"submit111":["Upload"],"file1_name":["Tulips.jpg"],"test":["value"],"file1_path":["/tmp/nginx_upload/0000000001"],"file1_size":["620888"]}

        body:-----------------------------7df1e01d20270

        Content-Disposition: form-data; name="file1_name"

        Tulips.jpg

        -----------------------------7df1e01d20270

        Content-Disposition: form-data; name="file1_content_type"

        image/jpeg

        -----------------------------7df1e01d20270

        Content-Disposition: form-data; name="file1_path"

        /tmp/nginx_upload/0000000001

        -----------------------------7df1e01d20270

        Content-Disposition: form-data; name="file1_md5"

        fafa5efeaf3cbe3b23b2748d13e629a1

        -----------------------------7df1e01d20270

        Content-Disposition: form-data; name="file1_size"

        620888

        -----------------------------7df1e01d20270

        Content-Disposition: form-data; name="file2"

        -----------------------------7df1e01d20270

        Content-Disposition: form-data; name="file3"

        -----------------------------7df1e01d20270

        Content-Disposition: form-data; name="file4"

        -----------------------------7df1e01d20270

        Content-Disposition: form-data; name="file5"

        -----------------------------7df1e01d20270

        Content-Disposition: form-data; name="file6"

        -----------------------------7df1e01d20270

        Content-Disposition: form-data; name="submit111"

        Upload

        -----------------------------7df1e01d20270

        Content-Disposition: form-data; name="test"

        value

        -----------------------------7df1e01d20270--

        [WARNING] [P31366] T140497047279408] 2015-12-24 15:06:39] nginx_file_upload, args:() kwargs:{u'file3': '', u'file2': '', u'file6': '', u'file5': '', u'file4': '',u'file1_content_type': 'image/jpeg', u'file1_md5': 'fafa5efeaf3cbe3b23b2748d13e629a1', u'submit111': 'Upload',u'file1_name': 'Tulips.jpg', u'test1': 'value1', u'test2': 'value2', u'file1_path': '/tmp/nginx_upload/0000000001', u'file1_size': '620888'}

    2.Nginx 下载

    • Nginx  配置 download

        修改/usr/local/nginx/conf/nginx.conf

        server {

               listen  20150;

          root /tmp/nginx_upload;

        }

        

        /tmp/nginx_upload为刚才nginx上传的文件目录,这样就可以实现文件的上传和下载

        如果涉及到文件后缀名的问题,可以使用python等脚本语言,接受nginx的上传url,然后对上传的文件进行处理,达到逻辑需求

  • 相关阅读:
    [原][osg]osg程序窗口化、显示网格、状态信息
    [转][qt]QSignalMapper 的用法,识别QPushButton的传入参数
    [转]office2013密钥和office2013永久激活码
    [原][工具][C++]调用adobe reader打开pdf,通过命令行启动pdf
    [转]C/C++实现回调机制的几种方式(回调、槽、代理)
    [转][c++]关于构造函数不能有返回类型的错误
    [转]全国性的地理空间数据库管理平台
    [转]JsonCpp遍历json
    阿里开源分布式事务解决方案 Fescar
    MySQL 中基于 XA 实现的分布式事务
  • 原文地址:https://www.cnblogs.com/zhanchenjin/p/5076466.html
Copyright © 2011-2022 走看看