nginx upload试用
好多博客与这个参考网址内容雷同,随便放个参考链接,也不想管出处在哪里了。
参考网址:nginx上传模块nginx_upload_module和nginx_uploadprogress_module模块进度显示,如何传递GET参数等。
新手上路,先按upload的标准用网页来一遍,后面准备看看能不能直接用代码post,再把上传的后续处理也砍掉。
之前nginx安装略过(默认安装在了/usr/local/nginx)
nginx upload 模块:github源码下载 并解压(/home/dyan/OpenSource/workspace/nginx-upload-module-2.2)
暂时不知道怎么动态添加模块,nginx源码重新编译一下 (nginx源码目录/home/dyan/OpenSource/nginx-1.4.2),在该目录下编译
$ ./configure --add-module=/home/dyan/OpenSource/workspace/nginx-upload-module-2.2
$ sudo make install
nginx已经安装到/usr/local/nginx下
然后修改/usr/local/nginx/conf/nginx.conf(修改前记得先备份),结合前面的nginx upload模块 与博客 在http字段server字段中添加如下配置
location /upload { upload_pass /index.php?c=uploader&a=upload_server; upload_cleanup 400 404 499 500-505; upload_store /home/dyan/OpenSource/workspace/nginx1.4.2/data; upload_store_access user:rw; upload_limit_rate 128k; # 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$"; } # Pass altered request body to a backend location ~ .*\.php?$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; }
没做过网页,不知道html和php怎么工作的,直接抄过来。
修改/usr/local/nginx/html/index.html(备份)为
<html> <head> <title>Test upload</title> </head> <body> <h2>Select files to upload</h2> <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="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="submit" value="Upload"> <input type="hidden" name="test" value="value"> </form> </body> </html>
添加一个/usr/local/nginx/html/index.php
<?php $temppath = $_POST["file1_path"]; $name = $_POST["file1_name"]; $orgFileName = "/home/dyan/OpenSource/workspace/nginx1.4.2/data/".$name; rename($temppath,$orgFileName); ?>
这时确认新的配置没有问题后重载nginx配置
$ sudo /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful $ ps -ef|grep nginx root 9111 3070 0 8月01 ? 00:00:00 nginx: master process sbin/nginx dyan 28862 9111 0 22:57 ? 00:00:00 nginx: worker process dyan 30167 6058 0 23:50 pts/19 00:00:00 grep --color=auto nginx
$ sudo kill -HUP 9111
这时打开浏览器地址栏输入:localhost
然后选择上传的文件,点击upload,不出意外是这样的
文件是上传上去了,但是后续处理出错了上传的文件文件也被删除。
因为我们的配置文件中说明了用index.php 和fastcgi来处理,所以还需要安装php和fastcgi
没有接触过php,先看看nginx fastcgi 怎么说。
然后安装php-cgi,因为是Ubuntu16.04,所以apt安装的版本应该是7.0
$ sudo apt install php-cgi
根据nginx fastcgi的说法,新开个终端运行php -b 127.0.0.1:9000,但是php 根本就没有-b选项,反而是php-cgi有这个选项,似乎正式我们需要的
$ php-cgi -h ... -b <address:port>|<port> Bind Path for external FASTCGI Server mode ...
运行(这里注意127.0.0.1:9000与nginx.conf中fastcgi_pass对应的)
$ php-cgi -b 127.0.0.1:9000
再次打开网页localhost,上传一个文件,看一下upload_store指定的文件夹下确实是我们上传的文件(要正确显示文件可能需要将nginx.conf文件开头默认被注释的user nobody改为运行nginx的用户名)