你好,我希望用户能够从我配置有nginx PHP的Windows服务器下载PDF文件。这是我的nginx.conf(服务器块):
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.php;
}
location /download {
internal;
alias /protected;
}
}
}
......以及 PHP 文件(标头部分)
$file = '/download/real-pdf-file.pdf'; //this is the physical file path
$filename = 'user-pdf-file.pdf'; //this is the file name user will get
header('Cache-Control: public, must-revalidate');
header('Pragma: no-cache');
header('Content-Type: application\pdf');
header('Content-Length: ' .(string)(filesize($file)) );
header('Content-Disposition: attachment; filename='.$filename.'');
header('Content-Transfer-Encoding: binary');
header('X-Accel-Redirect: '. $file);
文件是通过这样的URL调用的:
download.php?id=a2a9ca5bd4a84294b421fdd9ef5e438ded7fcb09
我尝试了这里的几个示例/解决方案,但目前没有一个有效。该文件很大(每个文件大小在250到400MB之间),每个用户可以下载4个文件。
使用PHP下载部分没有问题,只有nginx配置似乎不起作用。未检测到任何错误日志。
/download/real-pdf-file.pdf
确实是你的文件的绝对路径吗?在你的 PHP 脚本中,filesize($file)
的输出是什么?如果你的 PHP 确实正确,那么你的 nginx 配置就有问题。你告诉它:“如果通过X-Accel-Redirect
头请求 URI/download/real-pdf-file.pdf
,则提供文件/protected/read-pdf-file.pdf
”。除非有你的文件的副本(或同名文件或链接),否则这是行不通的。 - Carsten