I'm trying to serve up a protected mp4 file using nginx X-Accel via PHP but I'm having trouble with configuration, as I am getting a 404 error. Here's my nginx.conf server block:
server {
listen 80;
server_name localhost;
root /var/www/example.com;
location / {
try_files $uri /index.php?$args;
}
location /protected {
internal;
alias /var/www/uploads;
}
}
And here's my php file:
$aliasedFile = '/protected/' . $filename; //this is the nginx alias of the file path
$realFile = "/var/www/uploads/" . $filename; //this is the physical file path
header('Cache-Control: public, must-revalidate');
header('Pragma: no-cache');
header('Content-Type: video/mp4');
header('Content-Length: ' .(string)(filesize($realFile)) );
header('Content-Transfer-Encoding: binary');
header('X-Accel-Redirect: ' . $aliasedFile);
I'm sure I've just made a simple syntax error, but I can't see it. I have tried adding trailing /
s to the location and alias to no avail. Any nginx experts out there?
UPDATE Here's the remainder of my server block:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/example.com;
index index.php index.html index.htm;
server_name server_domain_or_IP;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}