NGINX防盗链 伪静态重写 nginx secure link rewrite the MP4/FLV File Link

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

http://www.test.com/oJ-RnINtL028POIlzeLXGA/1381153610/media/000.mp4

How To Get The rewrite clean link?

this examples nginx.conf

	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

$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

http://www.test.com/oJ-RnINtL028POIlzeLXGA/1381153610/media/000.mp4

Good luck!!!

One thought on “NGINX防盗链 伪静态重写 nginx secure link rewrite the MP4/FLV File Link

  1. 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 ?

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注