valid_referers
解决方案有效,但对我个人而言,很难以这种方式维持长长的黑名单。另一种解决方案是使用ngx_http_map_module
模块。在 ubuntu 14.04 nginx 发行版下,您将创建一个 /etc/nginx/blacklist.conf 文件:
# /etc/nginx/blacklist.conf
map $http_referer $bad_referer {
hostnames;
default 0;
# Put regexes for undesired referers here
"~social-buttons.com" 1;
"~semalt.com" 1;
"~kambasoft.com" 1;
"~savetubevideo.com" 1;
"~descargar-musica-gratis.net" 1;
"~7makemoneyonline.com" 1;
"~baixar-musicas-gratis.com" 1;
"~iloveitaly.com" 1;
"~ilovevitaly.ru" 1;
"~fbdownloader.com" 1;
"~econom.co" 1;
"~buttons-for-website.com" 1;
"~buttons-for-your-website.com" 1;
"~srecorder.co" 1;
"~darodar.com" 1;
"~priceg.com" 1;
"~blackhatworth.com" 1;
"~adviceforum.info" 1;
"~hulfingtonpost.com" 1;
"~best-seo-solution.com" 1;
"~googlsucks.com" 1;
"~theguardlan.com" 1;
"~i-x.wiki" 1;
"~buy-cheap-online.info" 1;
"~Get-Free-Traffic-Now.com" 1;
}
然后将其包含在您的 /etc/nginx/nginx.conf 文件中:
# /etc/nginx/nginx.conf
http {
# ...
include blacklist.conf;
# ...
}
完成后,您可以$bad_referer
在您的 nginx 站点中检查条件:
# /etc/nginx/sites-enabled/mysite.conf
server {
# ...
if ($bad_referer) {
return 444;
}
# ...
}
为了确保这些东西正常工作,您可以在 shell 中执行类似的命令:
$ curl --referer http://www.social-buttons.com https://example.org
...这应该给你:
curl: (52) Empty reply from server
我在这里写了一篇关于这个解决方案的博客文章https://fadeit.dk/blog/2015/04/22/nginx-referer-spam-blacklist/。