Nginx upload module-nginx upload module-

Source: Internet
Author: User
Tags form post sha1

I. Nginx Upload module principle

Official Document: Http://www.grid.net.ru/nginx/upload.en.html

Nginx upload module accepts user uploaded files through nginx service, and automatically resolves all files stored in the request body to the Upload_store specified directory. These file information is detached from the original request body and reassembled according to the configuration in the nginx.conf to be processed by the segment specified by Upload_pass, allowing the processing of any uploaded file. The value of the file field in each upload file is replaced by a series of upload_set_form_field instruction values. The contents of each uploaded file can be read from the $upload_tmp_path variable, or the file can be transferred to the destination directory. The uploaded file removal can be controlled via the Upload_cleanup command. If the requested method is not post, the module returns a 405 error (405 not allowed), which can be handled by the Error_page directive.

The specific process is as follows:

1. User access to the page that can choose to upload files

2. The user submits the form

3. The browser sends the file and information about the file as part of the request to the server

4. The server saves the file to the temporary storage directory Upload_store

5. Upload_pass The specified PHP page that handles the form submission copies the file from the Upload_store to the persistent storage location

P.S.

Install the compilation method

1. Download

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

2. Compile (Execute the following command in the Nginx compiled directory, where--add-module= you download the extracted upload plugin directory)

12 ./configure--user=www --group=www --prefix=/usr/local/nginx--with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --add-module=/data/downfile/nginx_upload_module-2.2.0

Two. Nginx Upload module configuration parameters

UPLOAD_PASS Specifies the PHP address for subsequent processing. The fields in the file will be separated and replaced with the necessary information processing to upload the file.

Upload_resumable whether to initiate a recoverable upload.

Upload_store Specifies the upload file storage address (directory). Directories can be hashed, in which case all subdirectories must exist before Nginx is started.

Upload_state_store specifies that the upload file status information directory can be restored by saving the uploaded files. Directories can be hashed, in which case all subdirectories must exist before Nginx is started.

Upload_store_access access to uploaded files, user:r refers to user-readable

Upload_pass_form_field a parameter that goes from the form to the back end, and can be represented by a regular expression. :

$upload _field_name-The name of the field in the original file

Upload_pass_form_field "^submit$|^description$";

This means that the Submit,description two fields are also passed through Upload_pass to the back-end PHP processing. If you want to pass all the form fields to the back end can be used Upload_pass_form_field "^.*$";

The Upload_set_form_field name and value may contain the following special variables:

$upload the name value of the _field_name form

$upload _content_type Types of uploaded files

$upload The original file name uploaded by the _file_name client

$upload _tmp_path file is saved on the server after uploading

Upload_aggregate_form_field can be used more than a few variables, the file is received after the generated and passed to the backend

The MD5 checksum value of the $upload _file_md5 file

The MD5 checksum value $upload _FILE_MD5_UC capital letters

The SHA1 checksum value of the $upload _file_sha1 file

The SHA1 checksum value $upload _FILE_SHA1_UC capital letters

File CRC32 value represented by $upload _FILE_CRC32 16 binary

$upload _file_size File Size

$upload _file_number The file number in the request body

These field values are calculated after the file has been successfully uploaded.

Upload_cleanup If an error such as 400 404 499 500-505 appears, delete the uploaded file

Upload_buffer_size Upload buffer size

Upload_max_part_header_len Specifies the maximum length byte of the head section.

UPLOAD_MAX_FILE_SIZE Specifies the maximum size and soft limit for uploading files. Client_max_body_size hard limit.

Upload_limit_rate upload speed limit, if set to 0 means no limit.

Upload_max_output_body_len exceeds this size and will report 403 errors (Request entity too large).

UPLOAD_TAME_ARRAYS Specifies whether square brackets for file field names are deleted

Upload_pass_args whether the parameter is forwarded.

Three. Nginx Configuration
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 #wget http: //www.nginx.org/download/nginx-1.2.2.tar.gz#wget http: //www.grid.net.ru/nginx/download/nginx_upload_module-2.2.0.tar.gz#tar zxvf nginx_upload_module - 2.2.0.tar.gz - c.. / software /#tar zxvf nginx_upload_module - 2.2.0.tar.gz - C.. / software /#. / configure–prefix = /usr/local / nginx–add - module = .. / nginx_upload_module - 2.2.0–with - http_secure_link_module#make#make install  #vi nginx.confuser www - data;worker_processes 20;error_log logs / error.log notice;working_directory / usr / local/ nginx;  events {worker_connections 1024;}  http {include mime.types;default_type application / octet - stream;root / www / web / upload;server {listen 80;server_name 192.168.41.129;error_page 405 = [email protected]; //处理405错误 location / {index index.html index.htm index.php;}[email protected] {root / www / web / upload;}  location~\.php$ {try_files $uri / 404.html;fastcgi_pass 127.0.0.1 : 9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include /etc/nginx/fastcgi_params;}  client_max_body_size 100m; #上传页面提交到这个location  location / upload {#文件上传以后转交给后端的php代码处理[email protected]test;#上传文件的临时存储位置,目录是散列的,应该存在子目录0 1 2 3 4 5 6 7 8 9upload_store /www/web/upload/tmp1;upload_store_access user: r;#设置请求体的字段  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;# 指示后端关于上传文件的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_args on;#将请求转到后端的地址处理[email protected]test{rewrite ^ (. * ) $ / test.php last;}}}

Four. Upload interface

# cat/www/web/upload/upload.html

123456789101112131415161718 <html>  <head>  <title>Test upload</title>  </head>  <body>  <h2>Select files to upload</h2>  <formenctype=”multipart/form-data” action=”/upload” method=”post”>  <inputtype=”file” name=”file1″><br>  <inputtype=”file” name=”file2″><br>  <inputtype=”file” name=”file3″><br>  <inputtype=”file” name=”file4″><br>  <input type=”file” name=”file5″><br>  <inputtype=”file” name=”file6″><br>  <inputtype=”submit” name=”submit” value=”Upload”>  <inputtype=”hidden” name=”test” value=”value”>  </form>  </body>  </html>

Five. Upload_pass processing content

# cat test.php//Here is just a simple print out, easy to understand the upload principle first. Please understand the output content of the Nginx upload module configuration parameters.

<?php

Print_r ($_post);

?>

For processing of uploaded files, please refer to: http://cn.php.net/manual/en/features.file-upload.php

Six. Testing

Http://192.168.41.129/upload.html

The output is as follows:

Array

(

[File1_name] = Learning Perl, sixth edition.pdf

[File1_content_type] = Application/pdf

[File1_path] =/www/web/upload/tmp/4/0000000014

[FILE1_MD5] = 87032cc58109f5c6bb866d2684f9b48c

[File1_size] = 8927511

[File2_name] = Programming Perl, 4th edition.pdf

[File2_content_type] = Application/pdf

[File2_path] =/www/web/upload/tmp/5/0000000015

[FILE2_MD5] = 82a52df177a8912c06af276581cfd5e4

[File2_size] = 21146356

[Submit] = Upload

)

Note: The following parameters need to be modified php.ini

File_uploads on whether to allow uploading via HTTP

Upload_max_filesize 8m maximum size allowed for uploading files

Post_max_size 8m the maximum value that PHP can receive via the form post

Also set the upload file size in nginx.conf

Upload_max_file_size Soft Limit

Client_max_body_size Hard Limit

Nginx upload module-nginx upload module-

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.