【问题标题】:php nginx X-Accel-Redirect headersphp nginx X-Accel-Redirect 标头
【发布时间】:2015-11-23 11:00:11
【问题描述】:

我有一个关于使用 nginx 保护包含静态文件的文件夹的问题 所以基本上我在 nginx 设置上有根文件夹:

/home/rise/rises/wwwdir

安全文件夹是:

/home/rise/rises/videop

如我们所见,我将该文件夹移到根文件夹之外,以防止/仅允许特定条件下查看

当我在发布之前第一次进行搜索时,我读到了一些要访问根目录外的 videop 文件夹的想法,我需要像我这样在 nginx conf 中创建别名 并访问内部

location /videop {
        root /home/rise/rises/;
         internal;
        }

但是我在 php 端加载视频时遇到问题...

$aliasedFile = '/videop/5_.m3u8';
$filename = '5_.m3u8';
header("Content-Description: File Transfer");
header("Content-Type application/x-mpegURL ");
header('Content-Disposition: attachment; filename='.$filename.'');
header('X-Accel-Redirect: '. $aliasedFile);
readfile($aliasedFile);

我错过了什么?

【问题讨论】:

  • PHP 如何知道/videop 的根文件夹?也许您应该将绝对路径传递给 readfile 而不是 URL。
  • 不工作... $aliasedFile = '/videop/5_.m3u8'; $realFile = '/home/rise/rises/videop/5_.m3u8' $filename = '5_.m3u8'; header("内容描述:文件传输"); header("Content-Type: application/x-mpegURL"); header('Content-Disposition: attachment; filename='.$filename.''); header('X-Accel-Redirect: '.$aliasedFile);读取文件($realFile);没有运气..,不工作
  • 任何人,请我现在被困 5 天......

标签: php nginx http-headers x-accel-redirect


【解决方案1】:

您的root 指令有一个尾随/,后面是URL 的前导/,因此请使用:

location /videop {
  root /home/rise/rises;
  internal;
}

您的 PHP 标头格式错误,应在 Content-Type 之后包含一个 :

PHP 不应包含正文。 readfile 是错误的。 PHP 的全部目的是发出一个由nginx 接收的内部重定向。所以 PHP 应该只返回 headers。

【讨论】:

  • 有:在 content-type 查看我以前的帖子之后,我更改了指令 /,我对 nginx 和这个新别名感到头疼,就像现在当我删除别名并放入 videop作为 wwwdir 之后的目录通常都可以正常工作,我只是想保护那个文件夹,所以我从 root 中创建了但是结合 alias 没有成功超过 5 天了,我感到非常非常累......,你可以pm 吗?
  • 当您说“一切正常”时 - 使用您的 PHP 脚本和X-Accel-Redirect 是否可以正常工作?如果是这样,您的新位置块可能没有正确加载到nginx。或者,nginx 用户的文件权限错误。如果您删除internal;,一切都应该继续工作,除了您将能够直接浏览到该文件 - 消除 PHP 脚本(以帮助进一步隔离问题)。
  • 不,这是当前工作代码,但根文件夹中有 videop 目录 192.168.1.5:9000/videop/5_.m3u8";标头(“位置:$文件”); $fd = fopen($file, "r"); while(!feof($fd)) { echo fread($fd, 1024 * 5); ob_flush();冲洗(); if (connection_aborted()){ 中断; } } fclose ($fd);出口();死(); ?>
  • 为什么你的 PHP 中有header("location: $file");?当然,该标头应该只用于重定向。
  • 因为这个 php 页面控制对视频的登录访问,如果没问题,则重定向到文件..
猜你喜欢
  • 1970-01-01
  • 2018-02-02
  • 2014-08-21
  • 1970-01-01
  • 2023-03-04
  • 1970-01-01
  • 2013-04-17
相关资源
最近更新 更多