Add X-Accel-Redirect stream support

This commit is contained in:
An Phan 2015-12-16 23:42:11 +08:00
parent ff78b4a9f1
commit 9a806632cb
4 changed files with 32 additions and 35 deletions

View file

@ -31,11 +31,3 @@
# Disable deflation for media files. # Disable deflation for media files.
SetEnvIfNoCase Request_URI "^/api/play/" no-gzip dont-vary SetEnvIfNoCase Request_URI "^/api/play/" no-gzip dont-vary
</IfModule> </IfModule>
<IfModule mod_xsendfile.c>
# Set a MOD_X_SENDFILE_ENABLED env variable for PHP to use later.
<Files *.php>
XSendFile On
SetEnv MOD_X_SENDFILE_ENABLED 1
</Files>
</IfModule>

View file

@ -4,6 +4,7 @@ namespace App\Http\Controllers\API;
use App\Http\Streamers\PHPStreamer; use App\Http\Streamers\PHPStreamer;
use App\Http\Streamers\XSendFileStreamer; use App\Http\Streamers\XSendFileStreamer;
use App\Http\Streamers\XAccelRedirectStreamer;
use App\Models\Song; use App\Models\Song;
class SongController extends Controller class SongController extends Controller
@ -16,16 +17,14 @@ class SongController extends Controller
*/ */
public function play($id) public function play($id)
{ {
if (env('MOD_X_SENDFILE_ENABLED')) { switch (env('STREAMING_METHOD')) {
(new XSendFileStreamer($id))->stream(); case 'x-sendfile':
return (new XSendFileStreamer($id))->stream();
return; case 'x-accel-redirect':
return (new XAccelRedirectStreamer($id))->stream();
default:
return (new PHPStreamer($id))->stream();
} }
(new PHPStreamer($id))->stream();
// Exit here to avoid accidentally sending extra content at the end of the file.
exit;
} }
/** /**

View file

@ -16,7 +16,13 @@ class XAccelRedirectStreamer extends BaseStreamer implements StreamerInterface
*/ */
public function stream() public function stream()
{ {
header('X-Accel-Redirect: '.str_replace(Setting::get('media_path'), '/media/', $this->song->path)); $relativePath = str_replace(Setting::get('media_path'), '', $this->song->path);
// We send our media_path value as a 'X-Media-Root' header to downstream (nginx)
// It will then be use as `alias` in X-Accel config location block.
// See nginx.conf.example.
header('X-Media-Root: '.Setting::get('media_path'));
header('X-Accel-Redirect: /media/'.$relativePath);
header("Content-Type: {$this->contentType}"); header("Content-Type: {$this->contentType}");
header('Content-Disposition: inline; filename="'.basename($this->song->path).'"'); header('Content-Disposition: inline; filename="'.basename($this->song->path).'"');

View file

@ -1,8 +1,8 @@
server { server {
listen *:8080; listen *:8080;
server_name koel.dev; server_name koel.dev;
root /Users/an/www/koel; root /var/www/koel;
index index.php; index index.php;
# Deny access to dotfiles # Deny access to dotfiles
location ~ /\. { location ~ /\. {
@ -18,27 +18,27 @@ server {
internal; internal;
# A 'X-Media-Root' should be set to media_path settings from upstream # A 'X-Media-Root' should be set to media_path settings from upstream
alias $upstream_http_x_media_root; alias $upstream_http_x_media_root;
#access_log /var/log/nginx/koel.access.log; #access_log /var/log/nginx/koel.access.log;
#error_log /var/log/nginx/koel.error.log; #error_log /var/log/nginx/koel.error.log;
} }
location / { location / {
try_files $uri $uri/ /index.php?$args; try_files $uri $uri/ /index.php?$args;
} }
location ~ \.php$ { location ~ \.php$ {
try_files $uri $uri/ /index.php?$args; try_files $uri $uri/ /index.php?$args;
fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000; fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php; fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_intercept_errors on; fastcgi_intercept_errors on;
include fastcgi_params; include fastcgi_params;
} }
} }