Forum: NGINX Content Truncating

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
michael (Guest)
on 2009-04-08 00:04
(Received via mailing list)
Hi,

I am having a weird issue where nginx is truncating a few bytes of
content from the end of files. The files are being served from PHP
(FastCGI via php-fpm) using PHP's readfile(). The PHP application sends
out a Content-Length header using filesize() and then spits out the
contents of the file to nginx using readfile().

The content however is coming short by a few bytes. nginx sends the
correct Content-Length, but closes the connection a few bytes short of
the Content-Length number of bytes. This doesn't happen for all files
and I can't seem to find a pattern either.

Any ideas?

Thanks,
-Michael

Posted at Nginx Forum: http://forum.nginx.org/read.php?2,891,891#msg-891
michael (Guest)
on 2009-04-08 00:06
(Received via mailing list)
Sorry, forgot to mention, I'm using nginx/0.6.35

-Michael

Posted at Nginx Forum: http://forum.nginx.org/read.php?2,891,892#msg-892
michael (Guest)
on 2009-04-08 02:42
(Received via mailing list)
Yes, I'm in the process of doing that -- have it already in testing...
But it would seem kinda odd that it would occur at all...

-Michael

Posted at Nginx Forum: http://forum.nginx.org/read.php?2,891,898#msg-898
Michael S. (Guest)
on 2009-04-08 02:59
(Received via mailing list)
I would look at leveraging x-accel-redirect instead. why have php keep
spoonfeeding the file when you can offload it to the webserver? :)
Michael S. (Guest)
on 2009-04-08 03:30
(Received via mailing list)
agreed.

although according to maxim if i understand it you won't even need to
calculate content-length yourself, nginx will do it for you. so it
saves you that step too.
Maxim D. (Guest)
on 2009-04-08 04:34
(Received via mailing list)
Hello!

On Tue, Apr 07, 2009 at 03:54:36PM -0400, michael wrote:

> Hi,
>
> I am having a weird issue where nginx is truncating a few bytes of content from the end 
of files. The files are being served from PHP (FastCGI via php-fpm) using PHP's 
readfile(). The PHP application sends out a Content-Length header using filesize() and 
then spits out the contents of the file to nginx using readfile().
>
> The content however is coming short by a few bytes. nginx sends the correct 
Content-Length, but closes the connection a few bytes short of the Content-Length number 
of bytes. This doesn't happen for all files and I can't seem to find a pattern either.
>
> Any ideas?

There were some reports of strange things happending with php's
readfile().  Personally I believe it's php issue (probably
configuration/programming one - e.g. memory limit reached due to
output buffering activated and script terminated or something like
this).  But I've never seen any debug info to prove this point.

If you are able to reproduce it - could you please build debug log
and post it?

Of course it's much better to use X-Accel-Redirect instead as
already suggested.

Maxim D.
michael (Guest)
on 2009-04-08 21:07
(Received via mailing list)
When will I not need to calculate Content-Length? When using X-Accel or
when using PHP readfile()...? With X-Accel, yes, I should not need to
calculate the Content-Length... What about when/if using readfile()

-Michael

Posted at Nginx Forum: http://forum.nginx.org/read.php?2,891,902#msg-902
Michael S. (Guest)
on 2009-04-08 21:24
(Received via mailing list)
On Tue, Apr 7, 2009 at 4:56 PM, michael <removed_email_address@domain.invalid> 
wrote:
> When will I not need to calculate Content-Length? When using X-Accel or when using PHP 
readfile()...? With X-Accel, yes, I should not need to calculate the Content-Length... 
What about when/if using readfile()

when you use x-accel.

when you use readfile i would expect you have to provide -all- the
headers. :)

Right now this is all I have for mine and it works like a charm:

header("Content-Disposition: attachment;
filename=\"".urldecode(basename($file))."\";");
header("Content-Type: application/force-download");
header("X-Accel-Redirect: $file_uri");
This topic is locked and can not be replied to.