盗链是指服务提供商自己不提供服务的内容,通过技术手段绕过其它有利益的最终用户界面(如广告),直接在自己的网站上向最终用户提供其它服务提供商的服务内容,骗取最终用户的浏览和点击率。受益者不提供资源或提供很少的资源,而真正的服务提供商却得不到任何的收益。
常见的是小站盗用大站的图片,音乐,视频,软件等资源,通过盗链可以减轻自己服务器的负担,因为真实的空间和流量均是来自别人的服务器
Referer
– nginx模块ngx_http_referer_module 用于阻挡来源非法的域名请求
– nginx指令valid_referers,全局变量$invalid_referer
– 语法: valid_referers none | blocked | server_names | string …;
none:缺少“Referer”请求头;
blocked:“Referer” 请求头存在,但是它的值被防火墙或者代理服务器删除;这些值都不以“http://” 或者 “https://” 字符串作为开头;
server_names:“Referer” 请求头包含某个虚拟主机名;
string …:任意字符串定义一个服务器名和可选的URI前缀。服务器名允许在开头或结尾使用“*”符号。 当nginx检查时,“Referer”请求头里的服务器端口将被忽略。
举个例子,修改一下配置文件nginx.conf
location ~* \.(gif|jpg|png|swf|flv|bmp)$ {
valid_referers none blocked *.liguoqi.site liguoqi.site;
if ($invalid_referer) {
return 403;
}
}
当然这个referer还是可以模拟的,所以要更绝情一点的办法就是使用加密,需要使用一个第三方的加密模块HTTP Access Key模块
location /download {
accesskey on;
accesskey_hashmethod md5;
accesskey_arg "key";
accesskey_signature "mypass$remote_addr";
}
<VirtualHost *:80>
DocumentRoot "/data/www"
ServerName www.test.com
ErrorLog "logs/test.com_error_log"
CustomLog "logs/test.com_access_log" combined
SetEnvIfNoCase Referer "^http://.*\.test\.com" local_ref # 表示只有 test.com 和 abc.com 的访问才允许,其他的都拒绝
SetEnvIfNoCase Referer ".*\. abc\.com" local_ref
<filesmatch "\.(txt|doc|mp3|zip|rar|jpg|gif|png)">
Order Allow,Deny
Allow from env=local_ref
Deny from all
</filesmatch>
</VirtualHost>