以下教程主要是通过nginx,禁止指定来源网站的链接访问自己的网站,因为经常在统计中看到广告站来路,很影响获取真实的统计信息,所以就想着屏蔽那些广告域名的来路。
1.对于来路为*.xxxx.com,全部返回404,代码如下:
- if ($http_referer ~* .*.xxxx.com){
- return 404;
- }
2.对于来路为xxxx.com的链接来路,通过路径转发全部返回到其首页
- if ($http_referer ~* xxxx.com) {
- rewrite ^/ http://xxxx.com/;
- }
以上代码可以将它们丢到location ~ 1.php(/|$) {}中,案例如下:
- location ~ [^/].php(/|$) {
- if ($http_referer ~* .*.xxxx.com){
- return 404;
- }
- }
添加代码后只要重启nginx,就可以生效。
可以F12添加超链接模拟来路测试。