smichaelsen
smichaelsen

Reputation: 168

Why does nginx return 403 on my PHP file download?

I use nginx 1.14.1 with PHP and I have problems with a call that should result in a file download.

The PHP application produces a response with status code 200 and the file contents are dumped in the body, but from nginx I get the following answer (head)

HTTP/2 403 
server: nginx/1.14.1
date: Tue, 23 Apr 2019 07:19:19 GMT
content-type: text/html; charset=UTF-8
vary: Accept-Encoding
x-powered-by: PHP/7.2.17
strict-transport-security: max-age=31536000

The headers my PHP application produces are not included in the nginx response.

Firefox yields the error "/Users/myuser/Downloads/NBPKD9pN.mp4.part could not be saved, because the source file could not be read. Try again later, or contact the server administrator."

The file that should be downloaded is about 1 GB in size.

The rest of the PHP application works fine (does not result in 403s).

Does it help to provide my full nginx config?

When I put the same file into the web root and call it directly (without PHP) the download works fine.

Edit: The PHP code roughly looks like this:

header('Content-Type: video/mp4');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Length: ' . $filesize));
readfile($file);
die();

Upvotes: 0

Views: 469

Answers (1)

smichaelsen
smichaelsen

Reputation: 168

The problem was that the PHP application has used ob_start() at some earlier point so the output of readfile() landed in the memory, which exceeded its limit.

Upvotes: 1

Related Questions