From 9a806632cb78118d86e075be5c45693fd6561b3c Mon Sep 17 00:00:00 2001 From: An Phan Date: Wed, 16 Dec 2015 23:42:11 +0800 Subject: [PATCH] Add X-Accel-Redirect stream support --- .htaccess | 8 ----- app/Http/Controllers/API/SongController.php | 17 +++++----- app/Http/Streamers/XAccelRedirectStreamer.php | 8 ++++- nginx.conf.example | 34 +++++++++---------- 4 files changed, 32 insertions(+), 35 deletions(-) diff --git a/.htaccess b/.htaccess index e1a3feb6..9c6ad25c 100644 --- a/.htaccess +++ b/.htaccess @@ -31,11 +31,3 @@ # Disable deflation for media files. SetEnvIfNoCase Request_URI "^/api/play/" no-gzip dont-vary - - - # Set a MOD_X_SENDFILE_ENABLED env variable for PHP to use later. - - XSendFile On - SetEnv MOD_X_SENDFILE_ENABLED 1 - - diff --git a/app/Http/Controllers/API/SongController.php b/app/Http/Controllers/API/SongController.php index 51dc09cf..c21b436b 100644 --- a/app/Http/Controllers/API/SongController.php +++ b/app/Http/Controllers/API/SongController.php @@ -4,6 +4,7 @@ namespace App\Http\Controllers\API; use App\Http\Streamers\PHPStreamer; use App\Http\Streamers\XSendFileStreamer; +use App\Http\Streamers\XAccelRedirectStreamer; use App\Models\Song; class SongController extends Controller @@ -16,16 +17,14 @@ class SongController extends Controller */ public function play($id) { - if (env('MOD_X_SENDFILE_ENABLED')) { - (new XSendFileStreamer($id))->stream(); - - return; + switch (env('STREAMING_METHOD')) { + case 'x-sendfile': + return (new XSendFileStreamer($id))->stream(); + 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; } /** diff --git a/app/Http/Streamers/XAccelRedirectStreamer.php b/app/Http/Streamers/XAccelRedirectStreamer.php index 75cc50ee..bafdc752 100644 --- a/app/Http/Streamers/XAccelRedirectStreamer.php +++ b/app/Http/Streamers/XAccelRedirectStreamer.php @@ -16,7 +16,13 @@ class XAccelRedirectStreamer extends BaseStreamer implements StreamerInterface */ 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-Disposition: inline; filename="'.basename($this->song->path).'"'); diff --git a/nginx.conf.example b/nginx.conf.example index e1a8d13c..3fc80f85 100644 --- a/nginx.conf.example +++ b/nginx.conf.example @@ -1,8 +1,8 @@ server { - listen *:8080; - server_name koel.dev; - root /Users/an/www/koel; - index index.php; + listen *:8080; + server_name koel.dev; + root /var/www/koel; + index index.php; # Deny access to dotfiles location ~ /\. { @@ -18,27 +18,27 @@ server { internal; # 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; - #error_log /var/log/nginx/koel.error.log; + #access_log /var/log/nginx/koel.access.log; + #error_log /var/log/nginx/koel.error.log; } location / { - try_files $uri $uri/ /index.php?$args; + try_files $uri $uri/ /index.php?$args; } 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_TRANSLATED $document_root$fastcgi_path_info; - fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + fastcgi_param PATH_INFO $fastcgi_path_info; + fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; - fastcgi_pass 127.0.0.1:9000; - fastcgi_index index.php; - fastcgi_split_path_info ^(.+\.php)(/.+)$; - fastcgi_intercept_errors on; - include fastcgi_params; + fastcgi_pass 127.0.0.1:9000; + fastcgi_index index.php; + fastcgi_split_path_info ^(.+\.php)(/.+)$; + fastcgi_intercept_errors on; + include fastcgi_params; } }