nginx secure link rewrite the MP4/FLV File Link
Nginx is Fast web server software, look it in www.nginx.org
if you used secure link mod
the link like is
http://www.test.com/media/000.mp4?s=oJ-RnINtL028POIlzeLXGA&e=1381153610
but we like that
1 |
http://www.test.com/oJ-RnINtL028POIlzeLXGA/1381153610/media/000.mp4 |
How To Get The rewrite clean link?
this examples nginx.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
location ~ \.mp4$ { rewrite ^(.*)/(.*)/(.*)/media/(.+)\.mp4$ $1/media/$4.mp4?s=$2&e=$3 last; secure_link $arg_s,$arg_e; secure_link_md5 test$uri$arg_e; if ($secure_link = "") { return 403; } if ($secure_link = "0") { return 403; } mp4; mp4_buffer_size 5m; mp4_max_buffer_size 50m; } |
This PHP Test code
1 2 3 4 5 6 7 8 9 10 11 12 |
$cdn = "http://www.test.com"; $path = "/media/000.mp4"; $secret = "test"; $expire = time() + 3600; # Link 1 hours expire 1小时过期 $md5 = base64_encode(md5($secret . $path . $expire, true)); $md5 = strtr($md5, '+/', '-_'); $md5 = str_replace('=', '', $md5); $url = $cdn.$path."?s=".$md5."&e=".$expire; echo $url."<br>"; $seo = $cdn."/".$md5."/".$expire.$path; echo $seo; |
now your can get two link
the $url is STD URL like
http://www.test.com/media/000.mp4?s=oJ-RnINtL028POIlzeLXGA&e=1381153610
the $seo is url friend link like
1 |
http://www.test.com/oJ-RnINtL028POIlzeLXGA/1381153610/media/000.mp4 |
Good luck!!!
hello , can you help me solve this please .
server {
listen 80;
server_name _;
root /home/ongoing;
error_log /var/log/nginx/error.log;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ \.mp4$ {
rewrite ^(.*)/(.+)\.mp4$ $1/$4.mp4?s=$2&e=$3 last;
secure_link $arg_s,$arg_e;
secure_link_md5 test$uri$arg_e;
if ($secure_link = “”) {
return 403;
}
if ($secure_link = “0”) {
return 403;
}
mp4;
mp4_buffer_size 5m;
mp4_max_buffer_size 50m;
}
}
my folder path is like this title/titlemovie.mp4 (title has diffrent name)
how to make the rewrite rules ?