Skip to content

Commit

Permalink
MDL-26028 implement Nginx X-Sendfile directory aliases setting
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Apr 30, 2012
1 parent 7e9f1b6 commit f7d26a0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
10 changes: 10 additions & 0 deletions config-dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,16 @@
// $CFG->xsendfile = 'X-Sendfile'; // Apache {@see https://tn123.org/mod_xsendfile/}
// $CFG->xsendfile = 'X-LIGHTTPD-send-file'; // Lighttpd {@see http://redmine.lighttpd.net/projects/lighttpd/wiki/X-LIGHTTPD-send-file}
// $CFG->xsendfile = 'X-Accel-Redirect'; // Nginx {@see http://wiki.nginx.org/XSendfile}
// If your X-Sendfile implementation (usually Nginx) uses directory aliases specify them
// in the following array setting:
// $CFG->xsendfilealiases = array(
// '/dataroot/' => $CFG->dataroot,
// '/cachedir/' => '/var/www/moodle/cache', // for custom $CFG->cachedir locations
// '/tempdir/' => '/var/www/moodle/temp', // for custom $CFG->tempdir locations
// '/filedir' => '/var/www/moodle/filedir', // for custom $CFG->filedir locations
// );
//
//
//
// This setting will prevent the 'My Courses' page being displayed when a student
// logs in. The site front page will always show the same (logged-out) view.
Expand Down
21 changes: 16 additions & 5 deletions lib/xsendfilelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,30 @@ function xsendfile($filepath) {
return false;
}

$filepath = realpath($filepath);

$aliased = false;
if (!empty($CFG->xsendfilealiases) and is_array($CFG->xsendfilealiases)) {
foreach ($CFG->xsendfilealiases as $alias=>$dir) {
$dir = realpath($dir).PATH_SEPARATOR;
if (strpos($filepath, $dir) === 0) {
$filepath = $alias.substr($filepath, strlen($dir));
$aliased = true;
break;
}
}
}

if ($CFG->xsendfile === 'X-LIGHTTPD-send-file') {
// http://redmine.lighttpd.net/projects/lighttpd/wiki/X-LIGHTTPD-send-file says 1.4 it does not support byteserving
header('Accept-Ranges: none');

} else if ($CFG->xsendfile === 'X-Accel-Redirect') {
// http://wiki.nginx.org/XSendfile
// Nginx is using relative path to protected folder, please note you can not use cache dir, tempdir and file pool outside of dataroot!
$filepath = realpath($filepath);
$dataroot = realpath($CFG->dataroot);
if (strpos($filepath, $dataroot) !== 0) {
// Nginx requires paths relative to aliases, you need to specify them in config.php
if (!$aliased) {
return false;
}
$filepath = substr($filepath, strlen($dataroot));
}

header("$CFG->xsendfile: $filepath");
Expand Down

0 comments on commit f7d26a0

Please sign in to comment.