nginx的proxy_pass,腐败的PDF文件时传回?

我有一个非常恼人的问题,而PDF的我试图从网站下载得到腐败。 他们生成的很好(我可以看到,如果我通过SFTP从他们被放置在临时文件夹下载)。

location ~ \.cgi$ { #try_files $uri =404; gzip off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header Host $host; proxy_pass http://127.0.0.1:8181; } 

Perl代码非常简单(我不会打扰把PDF生成代码,以及工作正常);

 my $filepath = qq|/home/fatpeter/web/site.com/cgi-bin/hotels/admin/tmp_pdf/| . CORE::time() . q|.pdf|; $pdf->to_file($filepath); $file_name =~ s/\-/_/sig; my $size = -s $filepath; my $file = \do { local *FH; *FH }; open $file, "<$filepath" or die "Unable to open file '$filepath': $!"; binmode $file; binmode STDOUT; print $IN->header($IN->file_headers( filename => $file_name, mimetype => 'application/pdf', inline => 0, size => $size )); { local $\; while (read($file, my $chunk, 4096)) { print $chunk; } } 

所以基本上nginx将请求传递给Apache2.4,然后Perl运行并创buildPDF。 该脚本然后将PDF传回给他们。

[tue Apr 25 13:41:36.810922 2017] [:error] [pid 2901]:Apache2 IO写入:(104)通过/home/fatpeter/web/site.com/cgi-bin/hotels/invoices .cgi第285行

第285行是while () { }循环:

  while (read($file, my $chunk, 4096)) { 

我不知道为什么它会在下载之前切断它。 有任何想法吗? 对于它的价值,我使用的testing文件是在363 KB(372,596字节),所以不是很大! 我的主要nginx.conf文件中的代理设置是:

proxy_redirectclosures; proxy_set_header主机$主机; proxy_set_header X-Real-IP $ remote_addr; proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for; proxy_pass_header Set-Cookie; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffers 32 4k;

这是脚本发送的标题:

 Content-type: application/pdf Content-Disposition: attachment; filename="foo.pdf"; size=372596 Content-Length: 372596 

更新:有趣的是,我现在下载时在Firefox中得到这个错误信息:

C:\ Users \ Andy \ AppData \ Local \ Temp \ Jy4tj5pr.pdf.part无法保存,因为无法读取源文件。

稍后再试,或联系服务器pipe理员。

我现在也在访问日志中得到这个:

 127.0.0.1 - - [25/Apr/2017:15:29:38 +0000] "GET /cgi-bin/hotels/invoices.cgi?action=download;id=60d90acf677e9c81;invoice=1055;t=french HTTP/1.0" 200 372861 "https://www.example.com/cgi-bin/hotels/invoices.cgi?t=french" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0" 81.174.134.133 - - [25/Apr/2017:15:29:38 +0000] "GET /cgi-bin/hotels/invoices.cgi?action=download;id=60d90acf677e9c81;invoice=1055;t=french HTTP/2.0" 200 372616 "https://www.example.com/cgi-bin/hotels/invoices.cgi?t=french" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0" 

更新3:好的,所以我在这里接近一点。 这个问题似乎与脚本中的“块”有关:

 while (read($file, my $chunk, 4096)) { 

如果我改变这个,例如:

 while (read($file, my $chunk, 409600)) { 

(大于有问题的文件大小),它工作! 但是,问题是,我怎么处理大文件?

所以这里是工作代码:

 { local $\; while (read($file, my $chunk, -s $filepath)) { print $chunk; } } 

我只是不喜欢不能发送回来的想法。 这样做有什么不利吗?