Use of Nginx upload module

Source: Internet
Author: User
Tags form post

Now the site, there will always be a little interaction with the user features, such as allowing users to upload avatar, upload photos, upload attachments such as. PHP writes a program that is not very efficient for uploading files.
Fortunately, Nginx has a module called upload to solve this problem. There are already many articles on the upload module on the network, but most of them are about compiling and installing these, for some details are not very clear, so they wrote this article.
Thanks to a lot of other people's documentation, see the reference Documentation section for more information.

The working principle of upload module
Nginx upload module modules through the Nginx server to accept user-uploaded files, it automatically analyzes the client upload request, the uploaded files to the directory location pointed to by Upload_store.
Then the file information will be removed from the original request, re-assemble the upload parameters and then transferred to the driven by Upload_pass designated location to process, so that the backend can arbitrarily process the uploaded files.
Each uploaded file field value will be replaced by the value specified by Upload_set_form_field.
The uploaded file can be accessed through the $upload_tmp_path variable.
After the uploaded file has been processed, the condition specified by Upload_cleanup controls the removal cleanup.

The above expression may not be intuitive enough, let us give an example, if we use the following page as the upload page:

[email protected] wwwroot]# cat index.html
<title>test upload</title>
<form name= "Upload" method= "POST" enctype= "Multipart/form-data" action= "/upload" >
<input type= "File" Name= "File1" ><br>
<input type= "File" Name= "File2" ><br>
<input type= "Submit" name= "Submit" value= "Upload" >
<input type= "hidden" name= "test" value= "value" >

Then we select two files, click on the "Upload" button, the Nginx Upload module will receive the file, the uploaded files to the upload_store point to the directory location.
The uploaded file information will be removed from the original request, re-assemble the upload parameters according to the configuration in nginx.conf, and then go to the post driven by upload_pass the specified location to process.
If we upload two files named: Picture 1.png and picture 2.png, then upload module receives the file, it will be passed to the back end like a set of parameters such as below:

"File1.path" = "/tmp/0000123458",
"File2.path" = "/tmp/0000123459",
"File1.content_type" = "Image/png",
"File2.content_type" = "Image/png",
"" = "Picture 1.png",
"" = "Picture 2.png",

In this way, the backend PHP code can use the $_post variable to get these parameters to handle the uploaded files. Below is a simple example, just show the value of the corresponding variable, and then based on the MD5 value of the file, take the first character as the corresponding one-level directory name, take the last character as the second-level directory name, and then move the uploaded file to the appropriate directory.

$temppath = $_post["File1_path"];
$name = $_post["File1_name"];
$MD 5 = $_post["File1_md5"];
$f _dir = substr ($md 5,0,1);
$s _dir = substr ($md 5,-1);
$final _file_path = "/". $f _dir. " /". $s _dir." /". $name;

echo $temppath. " <br/> ";
echo $name. " <br/> ";
echo $MD 5. " <br/> ";
echo $f _dir. " <br/> ";
Echo $s _dir. " <br/> ";
echo $final _file_path;
Rename ($temppath, $final _file_path);

Since Nginx upload module has done the most time-consuming MIME parsing work, the back-end PHP code simply needs to move the file to the right location. Because the upload module is written in C, it is much more efficient than PHP for parsing and therefore greatly improves the efficiency of file uploads.

Ii. brief description of configuration parameters of upload module

Below is a description of some configuration parameters:

Upload_pass refers to the PHP address that needs to be processed later
Upload_cleanup if PHP appears with 400 404 499 500-505 errors, delete the uploaded file
Upload_store Upload file storage address
Upload_store_access access to uploaded files, user:r refers to user-readable
Upload_limit_rate upload speed limit, if set to 0 means no limit
Upload_pass_form_field parameters that go from the form to the back end, and can be represented by a regular expression
The official example is 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 "^.*$";

Upload_set_form_field Several variables that can be used
$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 received after the generation of
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

Iii. steps to compile and install

This is very simple, there are many people have written a good article, in this journal record.

1. Preparing the Environment for compilation---this part is not needed, because I'm going to use nginx-lua-module, so it's listed here.

[LUA 5.1]
Tar xvzf lua-5.1.5.tar.gz
CD LUA-5.1.5/SRC
Make Linux
Make install

The default installation location here is/usr/local, if you need to install to another location:
Export Lua_lib=/path/to/lua/lib
Export Lua_inc=/path/to/lua/include

In addition, the current version of Lua-ngix-module is incompatible with Lua 5.2, do not choose Lua 5.2

[Pcre 8.30]
Tar xvzf pcre-8.30.tar.gz

2. Compiling Nginx

[Chaoslawful-lua-nginx-module]---This part does not need

Tar xvzf nginx_upload_module-2.2.0.tar.gz

[Nginx 1.0.15]
Tar xvzf nginx-1.0.15.tar.gz
CD nginx-1.0.15
./configure--prefix=/usr/local/nginx--with-pcre=.. /pcre-8.30--add-module=. /nginx_upload_module-2.2.0--add-module=. /chaoslawful-lua-nginx-module-8d28785--without-mail_pop3_module--without-mail_imap_module--without-mail_smtp_ Module--with-http_ssl_module--with-http_stub_status_module--with-http_gzip_static_module
Make && make install

Iv. some common problems

Variable name for question 1.upload_set_form_field

I the following setup in my conf file
Upload_set_form_field $upload "$upload _file_name";
But I want the change chosen param name to:
Upload_set_form_field ($upload _field_name+ "[Name]") "$upload _file_name";
So I can get the "attachment[name]" but this doesn ' t work. I would is very happy if someone could help me with merging variables with string in nginx config file:).

Nginx does not has a concatenation character, rather it ' s based on valid and invalid characters, for instance in the dire Ctive:
Try_files $uri $uri/@fallback;
$uri is the variable and/is a string to append Since/cannot are in a variable name.
Similarly you should try
$upload _field_name[name] "$upload _file_name";
If this doesn ' t work and then try.
Set $foo [name];
$upload _field_name$foo "$upload _file_name";
I cannot say if the upload module would even allow this, though. Minor syntax errors might also be present.

Doubt 2.upload module does not support input arrays

Nginx upload module does not support input array for PHP
Please refer below email,
Jacky Zhang wrote:
> Hi Valery,
> It ' s fine if I using Nginx upload module to upload a Signle file.
> But It look incorrect so when I use a input like this:

<input type= "File" Name= "userfiles[]"/>

> Do I have any suggestion?

No, arrays won ' t work and you have to list every file input field individually.
Best regards,
Valery Kholodkov

Question 3: How to limit the size of uploaded files


PHP.ini to set the

File_uploads on whether to allow the switch to upload files over HTTP. The default is on, which is open
upload_tmp_dir– files are uploaded to the server where temporary files are stored, and if not specified, the system default Temp folder is used
Upload_max_filesize 8m business, that is, the maximum size allowed to upload files. Default is 2M
Post_max_size 8m refers to the maximum value that can be received by a form post to PHP, including all values in the form. Default is 8M

Generally, set up the above four parameters, in the network normal situation, upload <=8m files is not a problem
However, if you want to upload a large volume of >8m files, only set the above four items will certainly be able to pass. Unless your network really has a 100m/s upload speed, you still have to set the following parameters.
Max_execution_time 600 Maximum time value per php page (seconds), default 30 seconds
Max_input_time 600 maximum time per PHP page to receive data, default 60 seconds
Memory_limit 8m max memory eaten per PHP page, default 8M

Also make the settings in the nginx.conf.
Upload_max_file_size <size> This is a soft limit.
Client_max_body_size This is a hard limit (default 1m)

Appendix: Configuration of Nginx.conf

# Generic startup file.
User Nginx;
Worker_processes 4;
Worker_rlimit_nofile 65536;


# keeps the logs free of messages on being able to bind ().
#daemon off;

Events {
Use Epoll;
Worker_connections 10240;

# Rewrite_log on;

Include Mime.types;
Default_type Application/octet-stream;
Sendfile on;
# Tcp_nopush on;
Keepalive_timeout 3;
# tcp_nodelay on;

gzip on;
Gzip_http_version 1.1;
Gzip_comp_level 2;
Gzip_types Text/plain Text/css
Application/x-javascript Text/xml
Application/xml Application/xml+rss
Client_max_body_size 13m;
Index index.php index.html index.htm;

server {
Listen 80;
Index index.html;

Listen 80;

# uploading the page will upload the file to this location.
Location/upload {
# File upload to the backend PHP code to handle

# temporary storage location of uploaded files
# Note here that I put the temp file directory in the TMPFS, for speed, but there is a risk of losing data!

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

Upload_cleanup 400 404 499 500-505;

# location @test {
# Proxy_pass;
#        }

Location ~ \.php {
Fastcgi_index index.php;
Include fastcgi.conf;


Reference Documentation:





Use of 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: 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.