php nginx 文件授权下载原理

php 页面使用下面代码访问内部授权文件
header("X-Accel-Redirect: '物理绝对路径');
nginx 在location内配置internal标记表示内部访问,这样就禁止了http直接文件下载
示例代码:

php代码

$filePath = "/gofile/test.mp3";
 header("Content-type: application/octet-stream");
 header("Content-Disposition: attachment; filename='test.mp3'");
 header("X-Accel-Redirect: $filePath);

nginx配置

保护HTTP真实资源目录

location ^~ /files/ {
     #拒绝访问
     deny all;
 }

虚拟目录授权下载

location ^~ /gofile/ {
     # 仅限内部调用访问
     internal;
     # 软链链接真实目录
     alias /var/www/files/;
     # 下载1m后再限速
     limit_rate_after 1m;
     limit_rate 256k;
 }

其它发挥你的想象力,用PHP代码逻辑去控制。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注