File upload with a 500 response

Maxim Dounin mdounin at
Fri Jun 18 16:01:38 MSD 2010


On Fri, Jun 18, 2010 at 07:04:07AM -0400, woodsmar wrote:

> I'm seeing some unusual behaviour in one specific client interaction
> with nginx and wonder if anyone has seen anything similar or can offer
> any explanation as to why this might be happening. I'm running nginx
> version 0.7.65.
> >From a curl command line I'm issuing a POST request to nginx over https.
> The POST request has a content-type of multipart/form-data and I'm using
> it to send some data and upload a file of just under 1Mb. 
> nginx is being used as a proxy for upstream web servers built with
> mochiweb. Under normal conditions, everything works fine. The file is
> uploaded successfully and stored in the back-end database. During
> testing I'm deliberately causing a 500 Internal Server Error to be
> returned by the upstream web servers to nginx, and this is where I'm
> seeing some unusual behaviour.
> The 500 status code is indeed returned but there is something strange
> going on with the client/nginx/upstream web server interaction. The
> whole interaction takes about 10 seconds. Curl rapidly receives the
> response headers with the 500 status code but then waits. It never
> receives the response body sent by the client - in this case the
> content-length is only 200 bytes and the client receives this length in
> the Content-Length response header. Curl reports an error code 18,
> meaning that it expected a response body of (in this case) 200 bytes but
> received less than this (zero in fact as curl reports that the transfer
> was closed with 200 bytes remaining to read).
> If I reduce the size of the file uploaded by this method, the problem
> disappears and curl receives the entire response body just fine. Also,
> everything is just fine with other web services where I deliberately
> introduce a 500 response code. It's just for the file upload of a
> significantly sized file (about 1Mb) that I see this behaviour.

Please produce debug log.  See here for details:

If I'm right in my assumption that your backend returns 500 before 
actually reading request body - the following patch may help:

diff --git a/src/http/ngx_http_upstream.c b/src/http/ngx_http_upstream.c
--- a/src/http/ngx_http_upstream.c
+++ b/src/http/ngx_http_upstream.c
@@ -2003,10 +2003,12 @@ ngx_http_upstream_send_response(ngx_http

     u->header_sent = 1;

+#if 0
     if (r->request_body && r->request_body->temp_file) {
         ngx_pool_run_cleanup_file(r->pool, r->request_body->temp_file->file.fd);
         r->request_body->temp_file->file.fd = NGX_INVALID_FILE;

     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);

Maxim Dounin

More information about the nginx mailing list