我正在尝试configurationnginx来支持file upload。 我正在使用这里描述的方法。 我的nginx.conf如下所示:
location /upload { limit_except POST { deny all; } client_body_temp_path /tmp; client_body_in_file_only on; client_body_buffer_size 128K; client_max_body_size 20G; return 201 $request_body_file; }
这里的想法是通过打开client_body_in_file_only ,每个请求被直接保存到磁盘。 而且它保存的文件名也被返回给客户端。
我可以用cURL上传文件,并看到它们正在全部转移:
$ curl -i -F "[email protected]" http://server/upload > out.txt % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 3053k 0 0 100 3053k 0 7874k --:--:-- --:--:-- --:--:-- 7889k
该文件是3 MB,并确保足够的3 MB被转移。 答复是:
$ cat out.txt HTTP/1.1 100 Continue HTTP/1.1 201 Created Server: nginx/1.13.4 Date: Tue, 22 Aug 2017 04:21:48 GMT Content-Type: text/plain Content-Length: 0 Connection: keep-alive
请注意,我没有回应响应正文中的文件名。 然后在服务器上/tmp/下没有任何东西。 上传的文件去了哪里?
我能够使用ngx_echo模块得到这个工作。 关键是echo_read_request_body指令,顾名思义就是显式读取请求体。
删除线路
return 201 $request_body_file;
并replace它
echo_read_request_body; echo $request_body_file;
诀窍了!