Nginx的mp4 hotlink保护不起作用

我试图保护mp4文件的hotlink,但不适合我。

这是我正在使用的代码:

location ~* \.(gif|jpg|jpeg|png|wmv|avi|mpg|mpeg|mp4|htm|html|js|css)$ { valid_referers none blocked http://188.226.192.56; if ($invalid_referer) { return 403; } root /usr/share/nginx/html; expires 1d; } 

适用于.jpg,.png等,但不适用于mp4。 我究竟做错了什么?

这是我的nginx conf:

 server { listen 80; server_name ipaddress; #charset koi8-r; #access_log logs/host.access.log main; location / { root /usr/share/nginx/html; index index index.php index.html index.htm; } error_page 404 /404.html; location = /404.html { root /usr/share/nginx/html; } # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name; include fastcgi_params; } location /usr/share/nginx/html/video/ { # activate flv/mp4 parsing for pseudostreaming flv; mp4; mp4_buffer_size 4M; mp4_max_buffer_size 10M; } location ~ .mp4$ { gzip off; gzip_static off; mp4; limit_rate_after 10m; limit_rate 1m; } client_max_body_size 80m; location ~* \.(gif|jpg|jpeg|png|wmv|avi|mpg|mpeg|mp4|htm|html|js|css)$ { valid_referers none blocked http://188.226.192.56; if ($invalid_referer) { return 403; } root /usr/share/nginx/html; expires 1d; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} 

}

谢谢!

原因是你在你的configuration中有一个单独的location ~ .mp4$ block,nginx在发送文件时使用。 因此,您的其他location阻止热连接的块不被应用。

解决这个问题的最简单的方法是将热链接预防function包括到MP4位置块中,如下所示:

 location ~ \.mp4$ { valid_referers none blocked http://188.226.192.56; if ($invalid_referer) { return 403; } gzip off; gzip_static off; mp4; limit_rate_after 10m; limit_rate 1m; } 

另外,你的location /usr/share/nginx/html/video/ directive很可能是没用的。 除非使用http://example.com/usr/share/nginx/html/video/videofile.mp4如url)访问您的video,否则不会使用该指令。 你应该删除它。 location指令总是需要一个URI(URL中的域之后的部分)进行匹配。

您的configuration中的另一个问题是,您在location块内使用root指令。 您只能在server级别使用root ,然后如果您需要为某些URL位置指定其他path,请在这些位置内使用alias指令。