<div dir="ltr"><div><span style="font-size:13px;font-family:Arial">vbart,</span></div><div><span style="font-size:13px;font-family:Arial"><br></span></div><div><span style="font-size:13px;font-family:Arial">Thanks for the advice, but I’m afraid this is not the case. When i remove the 9902 upstream and curl again, it works properly and print 90833 at the end with only calling </span><span style="font-size:13px;font-family:Arial;color:rgb(35,35,35)">file_get_contents once</span><span style="font-size:13px;font-family:Arial">.</span><br>
<span style="font-size:13px;font-family:Arial"></span><br>
<span style="font-size:13px;font-family:Arial">In addition, in the error.log with debug mode, i found the following lines,</span><br>
<span style="font-size:13px;font-family:Arial"></span><br>
<span style="font-size:13px;font-family:Arial">………….</span><br>
<span style="font-size:13px;font-family:Arial"></span><br>
<span style="font-size:13px;font-family:Arial">2014/06/17 21:57:48 [debug] 61130#0: *9 http upstream request: "/index.php?"</span><br>
<span style="font-size:13px;font-family:Arial">2014/06/17 21:57:48 [debug] 61130#0: *9 http upstream send request handler</span><br>
<span style="font-size:13px;font-family:Arial">2014/06/17 21:57:48 [debug] 61130#0: *9 http upstream send request</span><br>
<span style="font-size:13px;font-family:Arial">2014/06/17 21:57:48 [debug] 61130#0: *9 chain writer buf fl:0 s:584 </span><br>
<span style="font-size:13px;font-family:Arial">2014/06/17 21:57:48 [debug] 61130#0: *9 chain writer buf fl:0 s:32768 <— here</span><br>
<span style="font-size:13px;font-family:Arial">2014/06/17 21:57:48 [debug] 61130#0: *9 chain writer buf fl:0 s:8 </span><br>
<span style="font-size:13px;font-family:Arial">2014/06/17 21:57:48 [debug] 61130#0: *9 chain writer buf fl:0 s:32768 <— here</span><br>
<span style="font-size:13px;font-family:Arial">2014/06/17 21:57:48 [debug] 61130#0: *9 chain writer buf fl:0 s:8 </span><br>
<span style="font-size:13px;font-family:Arial">2014/06/17 21:57:48 [debug] 61130#0: *9 chain writer buf fl:0 s:25297 <— here</span><br>
<span style="font-size:13px;font-family:Arial">2014/06/17 21:57:48 [debug] 61130#0: *9 chain writer buf fl:0 s:15 </span><br>
<span style="font-size:13px;font-family:Arial"></span><br>
<span style="font-size:13px;font-family:Arial">………….</span><br>
<span style="font-size:13px;font-family:Arial"></span><br>
<span style="font-size:13px;font-family:Arial">2014/06/17 21:57:49 [error] 61130#0: *9 upstream prematurely closed connection while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "PUT /index.php HTTP/1.1", upstream: "fastcgi://<a href="http://127.0.0.1:9902">127.0.0.1:9902</a>", host: "127.0.0.1:8080”</span><br>
<span style="font-size:13px;font-family:Arial">…………..</span><br>
<span style="font-size:13px;font-family:Arial"></span><br>
<span style="font-size:13px;font-family:Arial">2014/06/17 21:57:49 [debug] 61130#0: *9 http next upstream, 2</span><br>
<span style="font-size:13px;font-family:Arial">2014/06/17 21:57:49 [debug] 61130#0: *9 http upstream request: "/index.php?"</span><br>
<span style="font-size:13px;font-family:Arial">2014/06/17 21:57:49 [debug] 61130#0: *9 http upstream send request handler</span><br>
<span style="font-size:13px;font-family:Arial">2014/06/17 21:57:49 [debug] 61130#0: *9 http upstream send request</span><br>
<span style="font-size:13px;font-family:Arial">2014/06/17 21:57:49 [debug] 61130#0: *9 chain writer buf fl:0 s:584</span><br>
<span style="font-size:13px;font-family:Arial">2014/06/17 21:57:49 [debug] 61130#0: *9 chain writer buf fl:0 s:32768 <— here</span><br>
<span style="font-size:13px;font-family:Arial">2014/06/17 21:57:49 [debug] 61130#0: *9 chain writer buf fl:0 s:8</span><br>
<span style="font-size:13px;font-family:Arial">2014/06/17 21:57:49 [debug] 61130#0: *9 chain writer buf fl:0 s:65536 <— here</span><br>
<span style="font-size:13px;font-family:Arial">2014/06/17 21:57:49 [debug] 61130#0: *9 chain writer buf fl:0 s:8</span><br>
<span style="font-size:13px;font-family:Arial">2014/06/17 21:57:49 [debug] 61130#0: *9 chain writer buf fl:0 s:90833 <— here</span><br>
<span style="font-size:13px;font-family:Arial">2014/06/17 21:57:49 [debug] 61130#0: *9 chain writer buf fl:0 s:15</span><br>
<span style="font-size:13px;font-family:Arial">…………..</span><br>
<span style="font-size:13px;font-family:Arial"></span><br>
<span style="font-size:13px;font-family:Arial">Before 9902 failed, the request body was sent correctly in bufs sized 32768, 32768 and 25297(totally 90833), but after 9902 failed, the upstream module retried sending body with size 32768, 65536 and 90833. Maybe something was wrong here.</span><br>
<span style="font-size:13px;font-family:Arial"></span><br>
<span style="font-size:13px;font-family:Arial">I guess after receiving the former 32768 bytes of the second buf(65536), php-fpm tries to locate a new fastcgi header but failed, for the data right behind is the body data of the image but a pre-constructed fastcgi header. So, it mislead php-fpm to think of receiving the data end.</span><br>
<span style="font-size:13px;font-family:Arial"></span><br>
<span style="font-size:13px;font-family:Arial">Refer to the source code, i also found why this happen.</span><br>
<span style="font-size:13px;font-family:Arial"></span><br>
<span style="font-size:13px;font-family:Arial;color:rgb(4,46,238)"><u><a href="https://github.com/nginx/nginx/blob/v1.5.12/src/http/ngx_http_upstream.c#L1441">https://github.com/nginx/nginx/blob/v1.5.12/src/http/ngx_http_upstream.c#L1441</a></u></span><br>
<span style="font-size:13px;font-family:Arial"></span><br>
<span style="font-size:13px;font-family:Arial">In the ngx_http_upstream_reinit function, cl->buf->file_pos are all reset to 0 for every buf in the output chain. But i think file_post should be reset to 0, 32768, 65536 instead.</span><br>
<span style="font-size:13px;font-family:Arial"></span><br>
<span style="font-size:13px;font-family:Arial">PS: the output chain is initiated here with buf->file_pos = 0, 32768, 65536...</span><br>
<span style="font-size:13px;font-family:Arial;color:rgb(4,46,238)"><u><a href="https://github.com/nginx/nginx/blob/v1.5.12/src/http/modules/ngx_http_fastcgi_module.c#L1085">https://github.com/nginx/nginx/blob/v1.5.12/src/http/modules/ngx_http_fastcgi_module.c#L1085</a></u></span><br>
<span style="font-size:13px;font-family:Arial"></span><br>
<span style="font-size:13px;font-family:Arial">Is this the reason that cause the problem?</span> <br></div><div><br></div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<span style="font-family:arial,sans-serif;font-size:13px">Date: Tue, 17 Jun 2014 20:50:13 +0400<br></span><span style="font-family:arial,sans-serif;font-size:13px">From: "Valentin V. Bartenev" <</span><a href="mailto:vbart@nginx.com" style="font-family:arial,sans-serif;font-size:13px" target="_blank">vbart@nginx.com</a><span style="font-family:arial,sans-serif;font-size:13px">><br>
</span><span style="font-family:arial,sans-serif;font-size:13px">To: </span><a href="mailto:nginx-devel@nginx.org" style="font-family:arial,sans-serif;font-size:13px" target="_blank">nginx-devel@nginx.org<br></a><span style="font-family:arial,sans-serif;font-size:13px">Subject: Re: Only 64k sent when the first upstream failed in<br>
</span><span style="font-family:arial,sans-serif;font-size:13px"> fastcgi_pass<br></span><span style="font-family:arial,sans-serif;font-size:13px">Message-ID: <1532980.8VxNd81dtW@vbart-</span><span style="font-family:arial,sans-serif;font-size:13px">workstation><br>
</span><span style="font-family:arial,sans-serif;font-size:13px">Content-Type: text/plain; charset="utf-8"</span><br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">On Tuesday 17 June 2014 23:50:19 Jing Ye wrote:<br>
</span><span style="font-family:arial,sans-serif;font-size:13px">> Hello all,<br></span><span style="font-family:arial,sans-serif;font-size:13px">><br></span><span style="font-family:arial,sans-serif;font-size:13px">> I have encountered the following possible bug in Nginx when using the<br>
</span><span style="font-family:arial,sans-serif;font-size:13px">> fastcgi_pass directive to put a file larger than 64k. I?m not sure if this<br></span><span style="font-family:arial,sans-serif;font-size:13px">> is a bug or I missed something in the config file.<br>
</span><span style="font-family:arial,sans-serif;font-size:13px">><br></span><span style="font-family:arial,sans-serif;font-size:13px">> My nginx version is 1.5.12, and the problem remains when i switched to the<br>
</span><span style="font-family:arial,sans-serif;font-size:13px">> newest 1.7.2.<br></span><span style="font-family:arial,sans-serif;font-size:13px">><br></span><span style="font-family:arial,sans-serif;font-size:13px">><br>
</span><span style="font-family:arial,sans-serif;font-size:13px">> Here is my nginx.conf file,<br></span><span style="font-family:arial,sans-serif;font-size:13px">><br></span><span style="font-family:arial,sans-serif;font-size:13px">> upstream *</span><a href="http://api.php.com/" style="font-family:arial,sans-serif;font-size:13px" target="_blank">api.php.com</a><span style="font-family:arial,sans-serif;font-size:13px"> <</span><a href="http://api.php.com/" style="font-family:arial,sans-serif;font-size:13px" target="_blank">http://api.php.com</a><span style="font-family:arial,sans-serif;font-size:13px">>* {<br>
</span><span style="font-family:arial,sans-serif;font-size:13px">> server </span><a href="http://127.0.0.1:9902/" style="font-family:arial,sans-serif;font-size:13px" target="_blank">127.0.0.1:9902</a><span style="font-family:arial,sans-serif;font-size:13px"> max_fails=3 fail_timeout=30s;<br>
</span><span style="font-family:arial,sans-serif;font-size:13px">> server </span><a href="http://127.0.0.1:9901/" style="font-family:arial,sans-serif;font-size:13px" target="_blank">127.0.0.1:9901</a><span style="font-family:arial,sans-serif;font-size:13px"> max_fails=3 fail_timeout=30s;<br>
</span><span style="font-family:arial,sans-serif;font-size:13px">> }<br></span><span style="font-family:arial,sans-serif;font-size:13px">> server {<br></span><span style="font-family:arial,sans-serif;font-size:13px">> listen 8080;<br>
</span><span style="font-family:arial,sans-serif;font-size:13px">> server_name localhost;<br></span><span style="font-family:arial,sans-serif;font-size:13px">> location ~ \.php$ {<br></span><span style="font-family:arial,sans-serif;font-size:13px">> root html;<br>
</span><span style="font-family:arial,sans-serif;font-size:13px">> fastcgi_pass *</span><a href="http://api.php.com/" style="font-family:arial,sans-serif;font-size:13px" target="_blank">api.php.com</a><span style="font-family:arial,sans-serif;font-size:13px"> <</span><a href="http://api.php.com/" style="font-family:arial,sans-serif;font-size:13px" target="_blank">http://api.php.com</a><span style="font-family:arial,sans-serif;font-size:13px">>*;<br>
</span><span style="font-family:arial,sans-serif;font-size:13px">> fastcgi_param SCRIPT_FILENAME<br></span><span style="font-family:arial,sans-serif;font-size:13px">> /usr/local/nginx/html/$</span><span style="font-family:arial,sans-serif;font-size:13px">fastcgi_script_name;<br>
</span><span style="font-family:arial,sans-serif;font-size:13px">> fastcgi_index index.php;<br></span><span style="font-family:arial,sans-serif;font-size:13px">> include fastcgi_params;<br>
</span><span style="font-family:arial,sans-serif;font-size:13px">> }<br></span><span style="font-family:arial,sans-serif;font-size:13px">><br></span><span style="font-family:arial,sans-serif;font-size:13px">><br>
</span><span style="font-family:arial,sans-serif;font-size:13px">><br></span><span style="font-family:arial,sans-serif;font-size:13px">> And the index.php file,<br></span><span style="font-family:arial,sans-serif;font-size:13px">><br>
</span><span style="font-family:arial,sans-serif;font-size:13px">> <?php<br></span><span style="font-family:arial,sans-serif;font-size:13px">> /* simplify get the request body and print its length. */<br></span><span style="font-family:arial,sans-serif;font-size:13px">> $raw_post_data = file_get_contents('php://</span><span style="font-family:arial,sans-serif;font-size:13px">input', 'r');<br>
</span><span style="font-family:arial,sans-serif;font-size:13px">> print(strlen($raw_post_data));<br></span><span style="font-family:arial,sans-serif;font-size:13px">> sleep(3); /* waiting for the server to be reset after 1<br>
</span><span style="font-family:arial,sans-serif;font-size:13px">> second */<br></span><span style="font-family:arial,sans-serif;font-size:13px">> ?><br></span><span style="font-family:arial,sans-serif;font-size:13px">><br>
</span><span style="font-family:arial,sans-serif;font-size:13px">><br></span><span style="font-family:arial,sans-serif;font-size:13px">[..]<br></span><span style="font-family:arial,sans-serif;font-size:13px">><br></span><span style="font-family:arial,sans-serif;font-size:13px">> The response status is 200 OK, but only 65536 bytes(64k) received.<br>
</span><span style="font-family:arial,sans-serif;font-size:13px">> Is this a bug or have i made something wrong in the config file?<br></span><span style="font-family:arial,sans-serif;font-size:13px">> I?m really confusing and hope if someone could help me figure it out.<br>
</span><span style="font-family:arial,sans-serif;font-size:13px">></span><br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">In the FastCGI protocol the data is transferred using "records". The maximum<br>
</span><span style="font-family:arial,sans-serif;font-size:13px">size of one record is 64k. So you're probably getting only the first record<br></span><span style="font-family:arial,sans-serif;font-size:13px">by calling file_get_contents() once.</span><br style="font-family:arial,sans-serif;font-size:13px">
<span style="font-family:arial,sans-serif;font-size:13px"> wbr, Valentin V. Bartenev</span><br style="font-family:arial,sans-serif;font-size:13px"><br><br>On Tue, Jun 17, 2014 at 11:50 PM, Jing Ye <span dir="ltr"><<a href="mailto:yejingx@gmail.com" target="_blank">yejingx@gmail.com</a>></span> wrote:<br>
<span style="font-size:13px;font-family:Arial">Hello all,</span><br><span style="font-size:13px;font-family:Arial">I have encountered the following possible bug in Nginx when using the fastcgi_pass directive to put a file larger than 64k. I’m not sure if this is a bug or I missed something in the config file.</span><br>
<span style="font-size:13px;font-family:Arial">My nginx version is 1.5.12, and the problem remains when i switched to the newest 1.7.2.</span><br><br><span style="font-size:13px;font-family:Arial">Here is my nginx.conf file,</span><br>
<span style="font-size:13px;font-family:Arial"> server <a href="http://127.0.0.1:9902" target="_blank">127.0.0.1:9902</a> max_fails=3 fail_timeout=30s;</span><br><span style="font-size:13px;font-family:Arial"> server <a href="http://127.0.0.1:9901" target="_blank">127.0.0.1:9901</a> max_fails=3 fail_timeout=30s;</span><br>
<span style="font-size:13px;font-family:Arial"> }</span><br><span style="font-size:13px;font-family:Arial"> server {</span><br><span style="font-size:13px;font-family:Arial"> listen 8080;</span><br><span style="font-size:13px;font-family:Arial"> server_name localhost;</span><br>
<span style="font-size:13px;font-family:Arial"> location ~ \.php$ {</span><br><span style="font-size:13px;font-family:Arial"> root html;</span><br><span style="font-size:13px;font-family:Arial"> fastcgi_pass </span><span style="font-size:13px;font-family:Arial;color:rgb(4,46,238)"><u><a href="http://api.php.com" target="_blank">api.php.com</a></u></span><span style="font-size:13px;font-family:Arial">;<br>
</span><span style="font-size:13px;font-family:Arial"> fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/$fastcgi_script_name;</span><br><span style="font-size:13px;font-family:Arial"> fastcgi_index index.php;</span><br>
<span style="font-size:13px;font-family:Arial"> include fastcgi_params;</span><br><span style="font-size:13px;font-family:Arial"> }</span><br><br><span style="font-size:13px;font-family:Arial">And the index.php file,</span><br>
<span style="font-size:13px;font-family:Arial">/* simplify get the request body and print its length. */</span><br><span style="font-size:13px;font-family:Arial">$raw_post_data = file_get_contents('php://input', 'r');</span><br>
<span style="font-size:13px;font-family:Arial">print(strlen($raw_post_data));</span><br><span style="font-size:13px;font-family:Arial">sleep(3); /* waiting for the server to be reset after 1 second */</span><br>
<span style="font-size:13px;font-family:Arial">?></span><br><span style="font-size:13px;font-family:Arial">We need two php-fpm servers listen on port 9901 and 9902, and importantly, the server listen on 9902 should be somehow reset to make the upstream module choose the next server listen on 9901. I made this by setting the request_terminate_timeout argument to 1s.</span><br>
<br><br><span style="font-size:13px;font-family:Arial">Send a put request, test.png is a file sized 90833 bytes(>64K)</span><br><span style="font-size:13px;font-family:Arial">* About to connect() to 127.0.0.1 port 8080 (#0)</span><br>
<span style="font-size:13px;font-family:Arial">* Trying 127.0.0.1...</span><br><span style="font-size:13px;font-family:Arial">* Adding handle: conn: 0x7fda7b803a00</span><br><span style="font-size:13px;font-family:Arial">* Adding handle: send: 0</span><br>
<span style="font-size:13px;font-family:Arial">* Adding handle: recv: 0</span><br><span style="font-size:13px;font-family:Arial">* Curl_addHandleToPipeline: length: 1</span><br><span style="font-size:13px;font-family:Arial">* - Conn 0 (0x7fda7b803a00) send_pipe: 1, recv_pipe: 0</span><br>
<span style="font-size:13px;font-family:Arial">* Connected to 127.0.0.1 (127.0.0.1) port 8080 (#0)</span><br><span style="font-size:13px;font-family:Arial">> PUT /index.php HTTP/1.1</span><br><span style="font-size:13px;font-family:Arial">> User-Agent: curl/7.30.0</span><br>
<span style="font-size:13px;font-family:Arial">> Host: <a href="http://127.0.0.1:8080" target="_blank">127.0.0.1:8080</a></span><br><span style="font-size:13px;font-family:Arial">> Accept: */*</span><br><span style="font-size:13px;font-family:Arial">> Content-Length: 90833</span><br>
<span style="font-size:13px;font-family:Arial">> Expect: 100-continue</span><br><span style="font-size:13px;font-family:Arial">></span><br><span style="font-size:13px;font-family:Arial">< HTTP/1.1 100 Continue</span><br>
<span style="font-size:13px;font-family:Arial">* We are completely uploaded and fine</span><br><span style="font-size:13px;font-family:Arial">< HTTP/1.1 200 OK</span><br><span style="font-size:13px;font-family:Arial">* Server nginx/1.5.12 is not blacklisted</span><br>
<span style="font-size:13px;font-family:Arial">< Server: nginx/1.5.12</span><br><span style="font-size:13px;font-family:Arial">< Date: Tue, 17 Jun 2014 13:55:30 GMT</span><br><span style="font-size:13px;font-family:Arial">< Content-Type: text/html</span><br>
<span style="font-size:13px;font-family:Arial">< Transfer-Encoding: chunked</span><br><span style="font-size:13px;font-family:Arial">< Connection: keep-alive</span><br><span style="font-size:13px;font-family:Arial">< X-Powered-By: PHP/5.4.24</span><br>
<span style="font-size:13px;font-family:Arial"><</span><br><span style="font-size:13px;font-family:Arial">* Connection #0 to host 127.0.0.1 left intact</span><br><span style="font-size:13px;font-family:Arial">65536% </span><br>
<span style="font-size:13px;font-family:Arial">The response status is 200 OK, but only 65536 bytes(64k) received. </span><br><span style="font-size:13px;font-family:Arial">Is this a bug or have i made something wrong in the config file? </span><br>
<span style="font-size:13px;font-family:Arial">I’m really confusing and hope if someone could help me figure it out. </span><br> <br>Many thanks!
<br></blockquote>
<div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div dir="ltr">
<span style="font-size:13px;font-family:Arial"></span>
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><span style="font-size:13px;font-family:Arial"> upstream </span><span style="font-size:13px;font-family:Arial;color:rgb(4,46,238)"><u><a href="http://api.php.com" target="_blank">api.php.com</a></u></span><span style="font-size:13px;font-family:Arial"> {<br>
</span></blockquote>
<span style="font-size:13px;font-family:Arial"></span>
<span style="font-size:13px;font-family:Arial"></span>
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><span style="font-size:13px;font-family:Arial"><?php<br></span></blockquote>
<span style="font-size:13px;font-family:Arial"></span>
<span style="font-size:13px;font-family:Arial"></span>
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><span style="font-size:13px;font-family:Arial">$ curl -T test.png </span><span style="font-size:13px;font-family:Arial;color:rgb(4,46,238)"><u><a href="http://127.0.0.1:8080/index.php" target="_blank">http://127.0.0.1:8080/index.php</a></u></span><span style="font-size:13px;font-family:Arial"> -v<br>
</span></blockquote>
<span style="font-size:13px;font-family:Arial"></span>
<span style="font-size:13px;font-family:Arial"></span></div>
</blockquote></div><br></div></div>