Nginx文件上傳下載實現與文件管理

1.Nginx 上傳

  • Nginx 依賴包下載

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

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

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

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

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

    # 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/app

  • 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.8ide

  • Nginx  Make

    # makepost

    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,而後對上傳的文件進行處理,達到邏輯需求

相關文章
相關標籤/搜索