小编Fad*_*aad的帖子

使用PHP和nginx X-Accel-Redirect服务大文件

嗨,我希望用户能够从我的服务器(Windows)下载配置了nginx PHP的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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

..和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);
Run Code Online (Sandbox Code Playgroud)

该文件是从URL调用,如下所示:

download.php?id=a2a9ca5bd4a84294b421fdd9ef5e438ded7fcb09
Run Code Online (Sandbox Code Playgroud)

我从这里尝试了一些示例/解决方案,但到目前为止还没有工作.文件很大(每个250到400 MB),每个用户可以下载4个文件.

使用PHP下载部件没有问题,只有看起来不起作用的nginx配置.未检测到错误日志.

php nginx

6
推荐指数
1
解决办法
1万
查看次数

标签 统计

nginx ×1

php ×1